From 934060e0ca4f05c538dbe7e8856d5e0833d57be4 Mon Sep 17 00:00:00 2001 From: Sven Hoexter Date: Sun, 21 Apr 2013 13:27:10 +0200 Subject: [PATCH] - rename clean_log() -> cleanup() - Use cleanup() as general purpose cleanup function which also writes mails in case of errors and always removes the lockfile. - Set ERRSTAT = 10 in case the mysql backup isn't finished and report subsequently as a failed backup. - Add .log suffix for the logfile. --- hetzner/hetznerbackupv2.sh | 49 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/hetzner/hetznerbackupv2.sh b/hetzner/hetznerbackupv2.sh index 2ceb473..0ffc0ec 100755 --- a/hetzner/hetznerbackupv2.sh +++ b/hetzner/hetznerbackupv2.sh @@ -18,12 +18,16 @@ PROTO=ssh ROOTMAIL="root@localhost" LOCKDIR="/var/lock/backup" LOCKFILE="$LOCKDIR/hetznerbackup" -LOGFILE="/var/log/hetznerbackup" +LOGFILE="/var/log/hetznerbackup.log" SESSIONLOG=$(mktemp /var/tmp/hetznerbackup.XXXXXXXXX) ERRSTAT=0 ## Backup job for main job_main() { + echo "" >> $SESSIONLOG + echo "Backing up main `date`" >> $SESSIONLOG + echo "" >> $SESSIONLOG + 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 @@ -31,7 +35,7 @@ job_main() { 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 + duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/home >> $SESSIONLOG 2>&1 || ERRSTAT=1 duplicity cleanup --extra-clean --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/home >> $SESSIONLOG 2>&1 || ERRSTAT=1 echo "Going for /var/www" >> $SESSIONLOG @@ -39,6 +43,11 @@ job_main() { duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/var/www >> $SESSIONLOG 2>&1 || ERRSTAT=1 duplicity cleanup --extra-clean --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/var/www >> $SESSIONLOG 2>&1 || ERRSTAT=1 + echo "Going for /var/lib/tomcat6" >> $SESSIONLOG + duplicity $BMODE $dry /var/lib/tomcat6 $PROTO://$FTP_USER@$FTP_HOST/main/tomcat6 >> $SESSIONLOG 2>&1 || ERRSTAT=1 + duplicity remove-all-but-n-full 5 --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/tomcat6 >> $SESSIONLOG 2>&1 || ERRSTAT=1 + duplicity cleanup --extra-clean --force $dry $PROTO://$FTP_USER@$FTP_HOST/main/tomcat6 >> $SESSIONLOG 2>&1 || ERRSTAT=1 + 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 >> $SESSIONLOG 2>&1 || ERRSTAT=1 @@ -47,15 +56,23 @@ job_main() { else echo "WARNING: It seems that the last mysql backup is still runing" >> $SESSIONLOG echo "Skipping mysql backup - please re-schedule" >> $SESSIONLOG + ERSSTAT=10 fi echo "" >> $SESSIONLOG } -## Append the session log to the main logfile and remove it -clean_log() { +# Append the session log to the main logfile and remove log and lock +cleanup() { + ENDDATE=$(date) + if [ $ERRSTAT -eq 0 ]; then + echo "Backup finished successfully $ENDDATE" >> $SESSIONLOG + else + echo "ERROR Backup FAILED $ENDDATE" >> $SESSIONLOG + cat $SESSIONLOG|mail -s "ERROR Backup FAILED $ENDDATE with ERRCODE $ERRSTAT" $ROOTMAIL + fi cat $SESSIONLOG >> $LOGFILE - rm $SESSIONLOG + rm $SESSIONLOG $LOCKFILE } ### check for other runing instances of this script @@ -65,16 +82,11 @@ if [ -e $LOCKFILE ]; then exit 5 else if [ ! -d $LOCKDIR ]; then - mkdir -m 700 $LOCKDIR + mkdir -m 700 $LOCKDIR fi touch $LOCKFILE fi -### Starting - echo "" >> $SESSIONLOG - echo "Starting Backup `date`" >> $SESSIONLOG - echo "" >> $SESSIONLOG - ### Set the duplicity backup mode we would like to use if [[ $(LANG=C; date +%A) =~ Sunday ]]; then echo "Runing in full mode" >> $SESSIONLOG @@ -86,17 +98,6 @@ fi ### Start the backup jobs job_main - ### 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 +cleanup +exit $ERRSTAT -- 2.39.5