#!/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
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
### 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