Backup Raspian using rsync

Create a folder on external disk, for example:


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)


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/

Add next lines:

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

Make it executable:

chmod u+x /home/<username>/scripts/

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/ >> /home/<username>/logs/cron.log 2>&1
@monthly mail -s "cron monthly log report" <your-gmail-account> < /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.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s