Backup Raspian using rsync

Create a folder on external disk, for example:

/media/usbhdd/backup/rsync

Create exclude list, to avoid loop and unnecessary files:

sudo nano /media/usbhdd/backup/rsync-exclude.txt

Add next lines: (Note: new rule – new line)

/proc/*
/sys/*
/dev/*
/boot/*
/tmp/*
/run/*
/mnt/*
/media/*
/swap

Test and backup using next command:

sudo rsync -av --progress --delete --exclude-from=/media/usbhdd/backup/rsync-exclude.txt / /media/usbhdd/backup/rsync/

Lets make backup runs automatically and send an email after execution:

(Note: Setup your mail using Gmail)

Create a script:

nano /home/<username>/scripts/rsync-backup.sh

Add next lines:

#!/bin/bash
sudo rsync -av --delete --exclude-from=/media/usbhdd/backup/rsync-exclude.txt / /media/usbhdd/backup/rsync/
if [ "$?" -eq "0" ]
then
   echo "rsync backup OK - $(date)" | mail -s "rsync backup status" <your-gmail-account>@gmail.com
else
   echo "Error while running rsync. Exit code: $? - $(date)" | mail -s "rsync backup status" <your-gmail-account>@gmail.com
fi

Make it executable:

chmod u+x /home/<username>/scripts/rsync-backup.sh

Login as root:

sudo su

Edit crontab file:

crontab -e

And add next lines:

#every wednesday at 12:00
00 12 * * 3 /home/<username>/scripts/rsync-backup.sh >> /home/<username>/logs/cron.log 2>&1
#
#monthly
@monthly mail -s "cron monthly log report" <your-gmail-account>@gmail.com < /home/<username>/logs/cron.log

*The last three lines are for monthly report on crontab jobs. They are not necessary for backup. They are just there for monthly check if everything in crontab went smoothly.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s