From 771c3ab297575afb75b6682fb3c071a03007c486 Mon Sep 17 00:00:00 2001 From: Sven Hoexter Date: Fri, 18 Feb 2011 16:34:36 +0100 Subject: [PATCH] First try to make this a bit more robust and add email reporting in case of error --- hetzner/hetznerbackupv2.sh | 65 +++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/hetzner/hetznerbackupv2.sh b/hetzner/hetznerbackupv2.sh index 86c7ede..570ace5 100755 --- a/hetzner/hetznerbackupv2.sh +++ b/hetzner/hetznerbackupv2.sh @@ -1,7 +1,9 @@ #!/bin/bash ### Halt on errors -set -e +#set -e +### Halt on unset variables +set -u ### Do not inherit the following variables # Safety net just in case some calls this @@ -17,33 +19,47 @@ export PASSPHRASE=s export FTP_PASSWORD=s FTP_USER=uxxx FTP_HOST=uxxx.your-backup.de -LOCKFILE="/var/lock/backup/hetznerbackup" PROTO=ssh +ROOTMAIL="root@localhost" +LOCKFILE="/var/lock/backup/hetznerbackup" +LOGFILE="/var/log/hetznerbackup" +SESSIONLOG=$(mktemp /var/tmp/hetznerbackup.XXXXXXXXX) +ERRSTAT=0 ## Backup job for main job_main() { - echo "Backing up main" + echo "" >> $SESSIONLOG + echo "Backing up main `date`" >> + echo "" >> $SESSIONLOG - echo "Going for /etc" - duplicity $BMODE $dry /etc $PROTO://$FTP_USER@$FTP_HOST/main/etc - duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/etc + echo "Going for /etc" >> $SESSIONLOG + duplicity $BMODE $dry /etc $PROTO://$FTP_USER@$FTP_HOST/main/etc >> $SESSIONLOG 2>&1 || ERRSTAT=1 + duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/etc >> $SESSIONLOG 2>&1 || ERRSTAT=1 - echo "Going for /home" - duplicity $BMODE $dry /home $PROTO://$FTP_USER@$FTP_HOST/main/home - duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/home + echo "Going for /home" >> $SESSIONLOG + duplicity $BMODE $dry /home $PROTO://$FTP_USER@$FTP_HOST/main/home >> $SESSIONLOG 2>&1 || ERRSTAT=1 + duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/home >> $SESSIONLOG 2>&1 || ERRSTAT=1 - echo "Going for /var/www" - duplicity $BMODE $dry /var/www $PROTO://$FTP_USER@$FTP_HOST/main/var/www - duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/var/www + echo "Going for /var/www" >> $SESSIONLOG + duplicity $BMODE $dry /var/www $PROTO://$FTP_USER@$FTP_HOST/main/var/www >> $SESSIONLOG 2>&1 || ERRSTAT=1 + duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/var/www >> $SESSIONLOG 2>&1 || ERRSTAT=1 - echo "Going for /var/lib/automysqlbackup" + echo "Going for /var/lib/automysqlbackup" >> $SESSIONLOG if [ -e /var/lib/automysqlbackup/backupdone ]; then - duplicity $BMODE $dry /var/lib/automysqlbackup $PROTO://$FTP_USER@$FTP_HOST/main/automysqlbackup - duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/automysqlbackup + duplicity $BMODE $dry /var/lib/automysqlbackup $PROTO://$FTP_USER@$FTP_HOST/main/automysqlbackup >> $SESSIONLOG 2>&1 || ERRSTAT=1 + duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/automysqlbackup >> $SESSIONLOG 2>&1 || ERRSTAT=1 else - echo "WARNING: It seems that the last mysql backup is still runing" - echo "Skipping mysql backup - please re-schedule" + echo "WARNING: It seems that the last mysql backup is still runing" >> $SESSIONLOG + echo "Skipping mysql backup - please re-schedule" >> $SESSIONLOG fi + + echo "" >> $SESSIONLOG +} + +# Append the session log to the main logfile and remove it +clean_log() { + cat $SESSIONLOG >> $LOGFILE + rm $SESSIONLOG } ### check for other runing instances of this script @@ -67,5 +83,16 @@ fi ### Start the backup jobs job_main -### remove the lockfile -rm $LOCKFILE +### Cleanup and reporting +ENDDATE=$(date) +if [ $ERRSTAT -eq 0 ]; then + echo "Backup finished successfully $ENDDATE" >> $SESSIONLOG + clean_log + rm $LOCKFILE + exit 0 +else + echo "ERROR Backup FAILED $ENDDATE" >> $SESSIONLOG + cat $SESSIONLOG|mail -s "ERROR Backup FAILED $ENDDATE" $ROOTMAIL + clean_log + exit 1 +fi -- 2.39.5