Rsync with Notification

rsync between a source and destination and send an email once completed saying weather the rsync operations were successful. The success of the rsync operation is taken from the exit status of the rsync command;

#!/bin/bash

mailSubject="Backup Successful"
mailTo="[email protected]"
mailMessage="/tmp/backup_tmp"
echo "Rsync was successfull" > $mailMessage

logDir="/rsync/log/dir/"
logFile="$logDir`date +%d-%m-%Y`.log"
set -o pipefail 
# Pipe fail is important here to get the exist status of the whole operation
# when we are piping the output of rsync to another command, otherwise we would just get the 
# exit status of the 'tee' command

echo "Starting sync at: " `date "+%d-%m-%Y %H:%M:%S"` | tee $logFIle
result=`rsync -razv --delete --ignore-errors --exclude-from=/rsync/excludelist [email protected]:"/Volumes/Audio\ HD1" /backup/ | tee -a $logFile`

if [ $? -ne 0 ]; then
        mailSubject="Backup Failure"
        echo "backup failed: " > $mailMessage
        echo "$result" >> $mailMessage
fi

# Clean up old logs
find $logDir -maxdepth 1 -type f -mtime +30 | xargs rm

echo "Finishing sync at: " `date "+%d-%m-%Y %H:%M:%S"` | tee -a $logFile

# Add a list of the log directory to the email so we can see the logs rotating
ls -l $logDir > /tmp/ls_result
cat /tmp/ls_result >> $mailMessage

cat $mailMessage | mail -s "$mailSubject" "$mailTo"

Previous page: Rsync Backup Window
Next page: Split MP3 by Seconds