Rsync is one of those core programs you will find in almost every distribution of Linux. If it isn’t in there on the install, it can certainly be installed from the distribution’s repositories. In all seriousness entire books could be written about how to use rsync in many different ways. I am going to show a very simple example of how to use this terrific tool. If you are not familiar with rsync visit the Wiki page for a bit of overall history and examples or go to the main website here.
To get started open a terminal window and type man rsync . This will open the manual pages for rsync and is recommended reading to fully utilize this tool. For a quick start enter rsync –help or rsync -h . This will give you the short list, if I may call it that, of the available options and switches. In this example I am only going to use a few of these options. There are plenty to use and customizing a job with rsync can produce many different combinations and outcomes. That is what makes it so versatile and robust.
This is one of my small scripts that I run manually after I deal with or pay my favorite Uncle, the IRS. We have had an on going relationship for many years and for some reason he wants more of my money all the time. Anyway here is the example and then we will break it down:
rsync -vruhi –del –log-file=/home/johnny/logs/sync_log /home/johnny/IRS/ johnny@bigserver:~/private/finance/IRS
It starts of course with the command and is followed by the switches or options. The options I am using are:
-v verbose – I like to see what is happening on a manually run command. This may or may interest you but at least in the testing or building phase is very useful.
-r recursive – Notice first how you do not have to add the – before this option. I have them grouped together. This option tells rsync to follow the directory structure.
-u update – Tells rsync to only copy files that are newer, changed or not already in the destination folder. Keeps it simple and tidy not copying files more than needed.
-h human readable output – I bet you thought I meant help but with other options the lower case h becomes a different switch. By itself -h displays help as I mentioned above. This is mostly usable for reading the output on screen and in logging.
–del deletes files – This option should be used carefully and done with the option for testing first (-n or –dry-run). There are several options in this category and all will delete files based on certain criteria. This option I have chosen deletes files in the destination that no longer exists in the source. Test first before including any of the deletion options since they cannot be undone.
-log-file a log – This is telling rsync to log the events and where to write the log.
/home/johnny/IRS – This is our source for rsync. This just happens to be a local folder but network locations work equally well.
johnny@bigserver – User account and server or computer for the destination. I should mention that at this point I am asked for a password to johnny’s user account on bigserver. There are plenty of ways to do this without this prompt but since I use it infrequently I have chosen to omit those types of options. See the man pages for more information on using passwords and keys.
~/private/finance/IRS – This is the destination folder. After the script or command is run, it will be a mirrored image of the original using the options I have given it.
So now you can see a very simple example of using rsync to copy a folder and its contents to a remote computer while logging the event. Remember this is a very simple example of rsync usage. With A few simple modifications this could be all you need to backup your entire /home directory. And if added to a cron job you could have it run automatically. I would like to hear about your experiences with rsync and how you use this for backing up folders and files.
~Jraz
