Rsync over SSH Backup Script

The purpose here is to setup a daily cronjob that will backup a server using RSYNC.

This script will be placed into the server that needs to be backup.

We will start by creating a CronJob calling our script, and sending a resumé by email (as you can see the cronjob starts each day at 2:00 am | do not forget to edit email@domain.com with your email):

1
vim /etc/cron.d/RSYNCbackup
1
00 02 * * * root sh /root/RsyncBackupScript.sh | mail -s "== RSYNC BACKUP SCRIPT (Server: ) ==" email@domain.com

Now create and edit the script itself:

1
vim /root/RsyncBackupScript.sh

 

Copy the content below and replace “[USER@DESTHOST]:/your/backup/path/.” parts according to your needs.

1
2
3
#!/bin/bash

rsync -avzH -e ssh --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / [USER@DESTHOST]:/your/backup/path/.

 

For example:

1
2
3
#!/bin/bash

rsync -avzH -e ssh --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / root@192.168.200.10:/backup/.

 

This script will use Rsync over SSH protocol to sync your local server to a remote one, excluding folders that are not needed for your backup, any other folder will be saved.