#!/bin/bash
# $Id: sys_bakcfg_real.bash 2 2023-09-15 09:19:17Z bertrand $
# Ce script permet de lancer une sauvegarde de configuration système.
datetime=$(date +%Y%m%d-%H%M%S)
src_path="/etc"
dst_path="/root/backup"
arch_path="$dst_path/archives"
dst_file_path_u="$dst_path/sysbakcfg.tar"
dst_file_path_c="$dst_file_path_u.gz"
arch_file_path="$arch_path/sysbakcfg-$datetime.tar.gz"
mail_rcpt="serveurcfi@cfi15.fr"
arch_tool=$(which tar)
comp_tool=$(which gzip)
mutt_tool=$(which mutt)
echo "Backup $src_path to $dst_file_path_c"
if [[ ! -d $dst_path ]]; then
	mkdir $dst_path
fi
if [[ -f $dst_file_path_c ]]; then
	rm -f $dst_file_path_c
fi
$arch_tool --absolute-names -c $src_path > $dst_file_path_u
$comp_tool -9 $dst_file_path_u
ls -lh $dst_file_path_c
if [[ ! -d $arch_path ]]; then
	mkdir $arch_path
fi
cp $dst_file_path_c $arch_file_path
#ls $arch_path
if [[ ! -z $mutt_tool ]]; then
	hostname=$(hostname -f)
	echo "See attached file for system backup configuration of $hostname." | $mutt_tool -a $dst_file_path_c -s "System backup config for $hostname" -- $mail_rcpt
else
	echo "mutt not found (mail cannot be sent)"
fi
