Save time (and money) on data transfers between hosts withrsync, a synchronization tool that allows easy, efficient replication offiles between different locations. Sync up, now!
The first (and most basic) thing you can do with rsync involves using it as a more intelligent copy command on your local system. So, let's suppose that I have the following directory tree in my home area on my Linux box,
As with a regular "cp" command, rsync needs to know the source and destination for the copy operation. Once it has this information, it compares the files in the two locations and updates the destination to an exact replica of the source. Let's see if it worked as advertised:
[me@olympus] $ ls /mnt/zip/backup/
public_html
Yup, it did - as you can see, my backup folder now contains a copy of my Web pages.
You can specify more than one source directory to be copied as well - let me add my "mail" directory to the list and run the command again.
[me@olympus] $ rsync --verbose --stats --recursive public_html mail
/mnt/zip/backup/Number of files: 181Number of files transferred: 161Total file size: 1043209 bytesTotal transferred file size: 1043209 bytesLiteral data: 0 bytesMatched data: 1043209 bytesFile list size: 3513Total bytes written: 15637Total bytes read: 12066wrote 15637 bytes read 12066 bytes 18468.67 bytes/sectotal size is 1043209 speedup is 37.66
Next, let's make a few changes to the original files, and see if rsync can detect them and selectively update the destination the next time I sync up.
[me@olympus] $ touch public_html/a.dat
[me@olympus] $ ls > public_html/a.dat[me@olympus] $ touch public_html/b.dat[me@olympus] $ rm public_html/cache/test.html[me@olympus] $ vi public_html/error.php
As you can see, I've added a couple of new files, deleted one old file and made changes to one PHP script. Let's sync up again and see what happens.
Pretty cool, huh? rsync added the two extra files to my backup, and identified and copied the modified file as well. However, the single file I deleted from the source is still present in the backup - obviously, rsync didn't delete it.
A quick look at the rsync documentation clears that one up - by default, rsync doesn't delete files from the destination when synchronizing directories. This default behaviour can be overridden by adding the "--delete" parameter to the rsync command line.
And now my destination is an exact copy of my source.
[me@olympus] $ ls /mnt/zip/backup/public_html/cache/
index.php m2_h.gif m2_n.gif tmp.gif
It should be noted that the "--delete" option can cause substantial damage if used unwisely - the rsync manual suggests always performing a dry run first when using this option.