Automating a Daily rsync Backup



Automating tasks relieves a lot of the repetitive nature from the role of a sysadmin.

The tasks are still important but automation can leave you time to concentrate on other, more dynamic, tasks.

Bash Script

The base of the automatic rsync is a very simple bash script.

It contains just 4 lines which will create seven daily folders on your backup machine (Monday – Sunday), log into the Cloud Server you want to backup and download the files and folders into the predefined location.


On the backup machine, create a file called ‘backup’. In this case I have placed it in my home ‘bin’ directory:

nano /home/backup/bin/backup

Once done, add the following:


dest=/backup/demo/`date +%A`

mkdir -p $dest

rsync -e 'ssh -p 30000' -avl --delete --stats --progress [email protected]:/home/demo $dest/

Short and sweet, but let’s go through it and see what we did:

The first line turns the file into a bash script. Without this line, nothing would occur as the machine would not know what to do with the contents.

The second line defines the variable ‘dest’. It starts with the backup path as ‘backup/demo/’ and then adds the day. So, if you ran this on a Monday, the ‘dest’ variable would be ‘/backup/demo/Monday’.

Do note that backticks (`) are used in the variable and not single quotes (‘).

The third line creates the destination directory as defined above. It used the ‘-p’ option to create recursive directories if they do not exist. So if the script was run on a Friday, it would create ‘/backup/demo/Friday’.

Which brings us nicely to the final line which is the rsync command. Simply place whatever rsync options you want here as they will be called when the script is run.

Note the use of the ‘$dest’ variable. The downloads will be placed in the ‘$dest’ directory.


Adjust the path and name to the file you created above, but that’s all you need to do.

Cron job

Final task is to create a cron job which will run the backup script at specified times.

I’m not going to go into details of cron here (I’ll do a separate article for that) but feel free to search the Interweb if you want to adjust the times I show below.

As the normal user (we don’t need root privileges for this) enter:

crontab -e

Now add the following:

# run rsync at 23.55hrs every day

55 23 * * *     sh /home/backup/bin/backup

A very simple command which you can embellish by adding an email address so the results can be sent to you and so on. I’ll explain more about cron tasks in another article.

Basically, each day at 23.55hrs it runs the script we created earlier. Depending on how you have setup your Slice, you may get internal mail regarding the output.

Views: 2


What is the capital of Egypt? ( Cairo )