Rsync System Backup
rsysy-backup = Rsync System Backup
Shell-Script rsysy-backup.sh erstellen
nano /usr/local/bin/rsysy-backup.sh
/usr/local/bin/rsysy-backup.sh
#!/bin/bash
PN="rsysy"
PV="0.4.1"
P="$PN"-v"$PV"
#### Function ####
print_usage() {
echo -e "\e[0m"
cat <<EOF
$P
Usage: $PN [options] [configuration file]
Options:
-h print this help
-n do not compress
-p compress with password (not yet implemented)
EOF
echo -e "\e[0;31m The options -p and -n can not be set at the same time!"
echo -e "\e[0m Example: /usr/local/bin/rsysy -p /home/username/config_file\n"
exit 1
}
clean_rsysy_dir() {
echo -e "\e[32m * Removing backup directory $rsysy_dir"
rm -rf $rsysy_dir
}
check_rsysy_conf() {
echo -e "\e[32m * Creating backup directory $rsysy_dir"
mkdir -p $rsysy_dir
# remove lines beginning with # or whitespaces
echo -e "\e[32m * Reading $rsysy_conf_user"
egrep -v "^\s*$|^#" $rsysy_conf_user > $rsysy_conf
# read $rsys_conf and check if file or directory exist
while read line; do
if [[ "$line" == "+"* || "$line" == "-"* ]]; then
line2=${line:1}
if [ ! -e $line2 ]; then
echo " File or directory does not exist: $line2" >> $rsysy_conf_error
echo " line must be an absolute path in $rsysy_conf_user" >> $rsysy_conf_error
echo " for example: +/etc/dhcpcd" >> $rsysy_conf_error
else
if [[ "$line" == "+"* ]]; then
echo $line2 >> $rsync_include
elif [[ "$line" == "-"* ]]; then
echo $line2 >> $rsync_exclude
fi
fi
else
echo " Syntax failure in $rsysy_conf_user: =>$line" >> $rsysy_conf_error
echo " line can only start with '#', '+' or '-'" >> $rsysy_conf_error
fi
done <$rsysy_conf
# are errors occurd, point the user to the problem and exit
if [ -f $rsysy_conf_error ]; then
echo ""
echo -e "\e[1;31m *ERROR* There are failures in your file $rsysy_conf_user!\n"
cat $rsysy_conf_error
clean_rsysy_dir
echo ""
echo -e "\e[1;35m* Stopped\n"
exit 1
fi
# if nothing to rsync inform the user an exit
if [ ! -f $rsync_include ]; then
echo -e "\e[1;31m *ERROR* Nothing to backup! Check your configuration file $rsysy_conf_user.\n"
clean_rsysy_dir
echo ""
echo -e "\e[1;35m* Stopped\n"
exit 1
fi
# include rsysy.sh and file $rsysy_conf_user in backup
echo "$0" >> $rsync_include
echo "$rsysy_conf_user" >> $rsync_include
}
backup_data() {
mkdir $rsysy_backup
echo -e "\e[1;32m * Starting backup data"
echo -en "\e[1;32m * rsync "
[ -f $rsync_exclude ] && EXCLUDE="--exclude-from=$rsync_exclude"
rsync --archive --recursive --files-from=$rsync_include $EXCLUDE / $rsysy_backup 2>> $rsync_error
echo -e "\e[1;32m finshed"
if [ -s $rsync_error ]; then
cp $rsync_error $rsysy_backup
echo -e "\e[1;31m *ERROR* There are failures from rsync command!"
counter=0
while read line; do
counter=$((counter+1))
echo -e "\e[0m # $line"
[ $counter -eq 10 ] && echo -e "\e[0m # ... and more ..." && break
done <$rsync_error
fi
# tar with compression or no compression
if [[ $COMPRESS = "no" ]]; then
echo -en "\e[1;32m * tar without compression "
tar cf $backup_file -C $rsysy_backup .
else
backup_file="$backup_file".gz
echo -en "\e[1;32m * tar with comrpession "
tar cfz $backup_file -C $rsysy_backup .
#tar --create --file - --posix --gzip -- $rbbackup | openssl enc -e -aes256 -out $rbfile
fi
echo -e "\e[32m finished"
clean_rsysy_dir
echo ""
echo -e "\e[1;32mYour backup file is located as $backup_file\n"
}
#### Main ####
# check for necessary tools
echo ""
echo -e "\e[1;35m* Running $P\n"
if [ ! -x /usr/bin/rsync ]; then
echo -e "\e[1;31m *ERROR* Please install rsync with your package manager!\n"
echo -e "\e[1;35m* Stopped\n"
exit 1
fi
if [ ! -x /bin/egrep ]; then
echo -e "\e[1;31m *ERROR* Please install grep with your package manager!\n"
echo -e "\e[1;35m* Stopped\n"
exit 1
fi
# default settings
COMPRESS="yes"
PASSWORD="no"
rsysy_string=`hostname`_`date +"%Y%m%d_%H-%M-%S"`
# default path configuration
rsysy_dir=/tmp/rsysy
rsysy_backup=$rsysy_dir/backup
rsysy_conf=$rsysy_dir/rsysy_conf
rsysy_conf_error=$rsysy_dir/rsysy_conf_error
rsync_include=$rsysy_dir/rsync_include
rsync_exclude=$rsysy_dir/rsync_exclude
rsync_error=$rsysy_dir/rsync_error
backup_file=/tmp/rsysy-backup_$rsysy_string.tar
# get options and the user configuration for rsysy
while getopts :nph option; do
case "$option" in
n) COMPRESS="no"
;;
p) PASSWORD="yes"
# not supported at the moment, so print usage and exit
print_usage
;;
h) print_usage
;;
\?) echo -e "\e[1;31m *ERROR* Invalid option -$OPTARG!\n"
print_usage
;;
esac
done
shift $(($OPTIND-1))
rsysy_conf_user="$1"
if [[ $COMPRESS == "n" && $PASSWORD == "y" ]]; then
print_usage
fi
if [ -z $rsysy_conf_user ]; then
echo -e "\e[1;31m *ERROR* Please specify a file with your files and directories to backup!\n"
echo -e "\e[1;35m* Stopped\n"
exit 1
elif [ ! -f $rsysy_conf_user ]; then
echo -e "\e[1;31m *ERROR* File $rsysy_conf_user does not exist!\n"
echo -e "\e[1;35m* Stopped\n"
exit 1
fi
check_rsysy_conf
backup_data
echo -e "\e[1;35m* Finsh\n"
die Konfiguartionsdatei erstellen, zum Beispiel im Home-Verzeichnis
nano /home/username/rsysy-backup.conf
/home/username/rsysy-backup.conf
# System +/etc -/etc/X11 # Boot +/boot -/boot/grub/ # Home +/home/useranme/ -/home/username/unimportant_directory
- # comment
- + include in backup
- - exclude from backup
rsysy-backup.sh ausführen
/usr/local/bin/rsysy-backup.sh /home/michael/rsysy-backup.conf
/usr/local/bin/rsysy-backup.sh /home/michael/rsysy-backup.conf
* Running rsysy-v0.4.1 * Creating backup directory /tmp/rsysy * Reading /home/michael/rsysy_backup.conf * Starting backup data * rsync finshed * tar with comrpession finished * Removing backup directory /tmp/rsysy Your backup file is located as /tmp/rsysy-backup_zero_20150602_18-52-07.tar.gz * Finsh