I was up to maintenance of my Debian box, and I came in need of creating and maintaining multiple, date-based backups of the machine.
Here is the bash script I wrote to accomplish this.
#!/bin/bash #Shell Script to backup desired target verbose="0" tarv="" if test "$1" == "-v"; then verbose="1" tarv="-v" fi if test "$verbose" == "1"; then echo "Starting backup ..." fi #Create month folder path="/backups/"`date +%Y/%m` #logfile="/var/log/sysback" name="backup-"`date +%Y.%m.%d-%H.%M.%S`".tar.gz" fqn=$path"/"$name target="/" admin="admin email address" #Sending email ... echo "Backup started in "$fqn" for the whole system ("`du -sh --exclude=/proc --exclude=/backups /`")." | mail -s "Backup started" $admin -a "From: backups@"`hostname` if test -e $fqn; then # logger -f $logfile -t backup -p daemon.warning "Removing previous backup: "$fqn rm -f $fqn fi if test ! -e $logfile; then echo "" > $logfile fi if test "$verbose" == "1"; then echo "Backing up in "$fqn" ..." fi #logger -f $logfile -t backup -p daemon.notice "Backing up the contents of '"$target"' to "$fqn if test "$verbose" == "1"; then echo "Setting up folders ..." fi mkdir -p /backups/`date +%Y/%m` su root -c "tar $tarv -Pzcpf $fqn --directory=$target --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups --exclude=var/log $target 2> /dev/null" #>(grep -v 'socket ignored' >&2) rm -f /backups/latest ln -s $fqn /backups/latest #logger -f $logfile -t backup -p daemon.notice "Backup completed: "$name if test "$verbose" == "1"; then echo "Backup finished: "`du -sh $fqn` fi echo "Backup was completed for the whole system ("`du -sh $fqn`")." | mail -s "Backup finished" $admin -a "From: backups@"`hostname`
Once executed, this script will backup the target specified by “$target” (the whole filesystem in this case) and create a tar.gz file under “/backups/month/date.tar.gz”. You have to specify an “admin” email account to which notifications will be sent.