]> git.sven.stormbind.net Git - sven/scripts.git/commitdiff
First try to make this a bit more robust and add email reporting in case of error
authorSven Hoexter <sven@timegate.de>
Fri, 18 Feb 2011 15:34:36 +0000 (16:34 +0100)
committerSven Hoexter <sven@timegate.de>
Fri, 18 Feb 2011 15:34:36 +0000 (16:34 +0100)
hetzner/hetznerbackupv2.sh

index 86c7ede180d60f2536095c6f36812585b1d9c9f3..570ace5f51b061bcb8cf92f21d1929ad815a21bf 100755 (executable)
@@ -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