Automating a Daily rsync Backup
To make things even easier, we can set up an automatic rsync and place the files into daily folders.
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.
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.
You can name the script anything you like and save it to any folder. In the following example, I name the script file "backup" and save in the directory "/home/backup/bin". If you name it something else or save it in a different directory, make sure to make the corresponding change when you edit the cron job.
On the backup machine, create a file called 'backup'. In the example this is placed it in the home 'bin' directory:
Once done, add the following:
#!/bin/bash dest=/backup/demo/`date +%A` mkdir -p $dest rsync -e 'ssh -p 30000' -avl --delete --stats --progress firstname.lastname@example.org:/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'.
Notice that backticks (`) are used in the variable rather than 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.
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:
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.
Although we went into detail, the basics of writing a script and setting up a simple cron task are straight forward.
It saves time by automating essential, but repetitive, tasks and frees the sysadmin (you) for more important things. Like playing.
--Kelly Koehn 13:29, 2 April 2009 (CDT)
© 2015 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
See license specifics and DISCLAIMER