Date created: Sunday, June 19, 2011 12:18:34 PM. Last modified: Sunday, January 27, 2019 11:45:57 AM

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;


mailSubject="Backup Successful"
echo "Rsync was successfull" > $mailMessage

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 user@"/Volumes/Audio\ HD1" /backup/ | tee -a $logFile`

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

# 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