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
--------------------------------------------------
rdiff-backup
rdiff-backup

 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 (http://rdiff-backup.nongnu.org/features.html)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):
https://busylog.net/2012/04/03/configure-ssh-for-password-less-ssh-login/

#2 script for rdiff-backup

Download complete source and templates of script here.
The script is backup.sh (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
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)

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

From: BigGnomo@home.it
To: my.name@gmail.com
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 (backup.sh) with backup date

From: BigGnomo@home.it
To: my.name@gmail.com
Subject: !!!ERROR!!! on Backup <DATE>

2.4 Backup script

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

#Config
 LOCAL_PATH=/mnt/usbstorage/shared/0UnderBackup/
 REMOTE_PATH=/mnt/disk1/share/0backupBigGnomo/
 REMOTE_IP=192.168.2.2
 SCRIPT_PATH=/mnt/usbstorage/shared/script/
 FROM=BigGnomo@home.it
 TO=my.name@gmail.com
 SERVER=smtp.server.it

Script : backup.sh

#!/bin/sh

#Config
LOCAL_PATH=/mnt/usb_storage/shared/0_UnderBackup/
REMOTE_PATH=/mnt/disk1/share/0_backupBigGnomo/
REMOTE_IP=192.168.2.2
SCRIPT_PATH=/mnt/usb_storage/shared/script/
FROM=BigGnomo@home.it
TO=my.name@domtest.priv
SERVER=out.server.it

#Backup
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
fi

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/backup.sh

Posts