Backup script with email notification

In this post a simple script which executes rdiff-backup and sends an email notification with the summary. In case of error a second email with proper warning message is sent.  The email notification with the summary (with backup result) reports information such as “new files“, “files deleted… from last backup  :

--------------[ Session statistics ]--------------
StartTime 1375827605.00 (Wed Aug  7 00:20:05 2013)
EndTime 1375828319.55 (Wed Aug  7 00:31:59 2013)
ElapsedTime 714.55 (11 minutes 54.55 seconds)
SourceFiles 41480
SourceFileSize 170554903211 (159 GB)
MirrorFiles 41476
MirrorFileSize 170554855075 (159 GB)
NewFiles 4
NewFileSize 32776 (32.0 KB)
DeletedFiles 0
DeletedFileSize 0 (0 bytes)
ChangedFiles 10
ChangedSourceSize 116752 (114 KB)
ChangedMirrorSize 101392 (99.0 KB)
IncrementFiles 14
IncrementFileSize 5373 (5.25 KB)
TotalDestinationSizeChange 53509 (52.3 KB)
Errors 0

 My use case: Buffalo WZR-HP-AG300H ( OpenWrt Firmware Attitude Adjustment – r29697 ) attached to external usb HD (1TB) works as local NAS in order to share the contents such as photos and documents. As backup tool I’m using rdiff-backup ( is used in order to backup (folder image) from Buffalo contents to a remote NAS.

Here in this post :
 #1 prerequisite
 #2 script for rdiff-backup

#1 prerequisite :

 You have to install (on Buffalo the source of data to backup) :
  rdiff-backup  (download here , using opkg or LuCI openwrt interface)
  mini-sendmail (download here, using opkg or LuCI openwrt interface)
configure ssh for password-less SSH login (ssh login from Buffalo -to-> NAS):

#2 script for rdiff-backup

Download complete source and templates of script here.
The script is (at end of this post) and it is based on 3 files used as templates :

  1. help.txt : static text (included in email notification) with useful notes and examples of rdiff-backup
  2. template.tpl : template of email notification
  3. template_error.tpl : template of email notificationin in case of error

Final email notification will be an email with : statistics + help.txt

email notification

2.1 help.txt (will be included in email as help)

Create a file in <SCRIPT_PATH> named help.txt with this content :
 (cut the  text part and create a file named help.txt — you have to change path in examples)

*) NOTES :
 local directory on router is : /mnt/usb_storage/shared/0_UnderBackup/
 remote directory on NAS   is : /mnt/disk1/share/0_backupBigGnomo/
 Rdiff site :
 *) CHECK : Compare
 rdiff-backup --compare /mnt/usb_storage/shared/0_UnderBackup/
 *) CHECK : All changes from last 2 Days
 rdiff-backup --list-changed-since 2D
 *) CHECK : file status 2 Days ago
 rdiff-backup --list-at-time 2D
 *) BACKUP : Explicit backup
 rdiff-backup --print-statistics /mnt/usb_storage/shared/0_UnderBackup/
 *) REMOVE : information older than 4 weeks
 rdiff-backup --remove-older-than 4W
 *) RESTORE : simple restore
 !!WARNING!! : is compied also rdiff-backup-data
 *) RESTORE : all with last backup
 rdiff-backup -r now <DESTINATION_DIR>/restore
 *) RESTORE : all with situation of 2 days ago
 rdiff-backup -r 2D <DESTINATION_DIR>/restore
 *) RESTORE : single file
 rdiff-backup -r now<FILE_NAME> <DESTINATION_DIR>/<FILE_NAME>

2.2 template.tpl

Create file in <SCRIPT_PATH> named template.tpl with this content :
(cut the  text part and create a file named template.tpl — you have to change from, to …)
Note : <DATE> will be replaced by script ( with backup date

Subject: Backup Statistics <DATE>

2.3 template_error.tpl

Create file in <SCRIPT_PATH> named templateerror.tpl with this content :
(cut the  text part and create a file named template_error.tpl — you have to change from, to …)
Note : <DATE> will be replaced by script ( with backup date

Subject: !!!ERROR!!! on Backup <DATE>

2.4 Backup script

Create file in <SCRIPT_PATH> named with this content :
(cut the  text part and create a file named  — you have to change from, to, path, ip …)
You have to change configuration :


Script :



rdiff-backup --print-statistics $LOCAL_PATH $REMOTE_IP::$REMOTE_PATH >/tmp/last_backup.txt 2>/tmp/last_backup_E.txt 

#Prepare Notify
sed s/\<DATE\>/"`date`"/ $SCRIPT_PATH/template.tpl >/tmp/email.txt
cat /tmp/last_backup.txt >>/tmp/email.txt
cat $SCRIPT_PATH//help.txt >>/tmp/email.txt

#Send notify
cat /tmp/email.txt | sendmail -f$FROM -s$SERVER $TO

#Error : check error looking into --print-statistics file (/tmp/last_backup.txt)
error=`grep -e 'Errors.0' /tmp/last_backup.txt`
if [ -z "$error" ]; then
 sed s/\<DATE\>/"`date`"/ $SCRIPT_PATH/template_error.tpl >/tmp/email_e.txt
 cat /tmp/last_backup_E.txt >>/tmp/email_e.txt
 cat /tmp/email_e.txt | sendmail -f$FROM -s$SERVER $TO

2.5 Add in crontab

edit crontab (on buffalo source of data to backup) with : crontab -e
add this lime : 2 1 * * * /mnt/usb_storage/shared/script/