I have backup scripts that run periodically. The Backup Dragon moved off-site and is now running. Every hour it makes a request to DrQue.net that registers the IP address. SSH on a non-standard port allows the Data Dragon to connect to it and do backups. With the backups running, one last item I wanted to make sure was working was e-mails in case of a failure. I found the program
msmtp which simply allows me to send e-mail from the command line.
#!/bin/bash
ip=`ssh used@webServer "cat /directory/ipAddress.txt"`
now=`date`
echo "Sync date: $now" > /path/backupReport.txt
echo "Remote IP: $ip" >> /path/backupReport.txt
start=`date +%s.%N`
rsync -arv -e 'ssh -p <port>' --exclude-from /path/exclude-list /path/to/backup/ used@$ip:/path/to/destination/ >> /path/backupReport.txt 2> /path/backupReportErrors.txt
if [ $? != 0 ]; then
# If there was an error, send an e-mail detailing what went wrong.
errorMessage=`cat /path/backupReportErrors.txt`
echo "ERROR: Backups failed"
echo -e "Subject: ERROR: Backups failed.\n\n$errorMessage" | msmtp username@domain
fi
end=`date +%s.%N`
delta=`awk '{print $1-$2}' <<< "$end $start"`
now=`date`
echo "Finished: $now ($delta seconds)" >> /path/backupReport.txt
This script first logs into the webserver to fetch the saved IP address of the remote machine. Then it creates a backup report file, notes the current time and the IP address of the remote machine. Then
rsync is used over SSH to synchronize the two servers. Output is logged to the backup report, and errors are logged to a separate log file. If
rsync runs into any problems, an e-mail is composed with the contents of the error report and sent using
msmtp. Lastly the time it took to run the backup is calculated and written to the backup report.
The script should run on any Linux system and only requires
rsync and
msmtp. I use a similar script for the daily in-house backups from the Snow Dragon to the Red Dragon.