Rsync: how to make backup copies and transfer files
Rsync is a powerful tool to create a backup copy of your
directories on another disk, or to transfer files to/from a remote machine.
It is an excellent, more flexible alternative to scp, tar and
other file-transferring tools. A thorough description can be found in
the rsync
man page.
As always, a few examples are better than one thousand words.
If you want to keep a backup copy of a local directory on another machine: set from = "/net/cherne/scratch/ncaon/ncaon1/"
set to = "/net/guinda/scratch/ncaon_backup/ncaon1/"
rsync -auv ${from} ${to}
The first thing rsync does is to compare the contents of the two directories
above. Then it will transfer all files that are in
$from and not in $to,
as well as those files present in both directories, but that have a more
recent modification date in $from.
This way, directory $to will become a
copy of directory $from, but only
the files required to bring the two dirs into sync are transferred.
Note that it is advisable to terminate the directory names with a slash, in
order to preserve the exact directory correspondence in both
machines.
The meaning of the flags is the following: -a archive mode (see the man page for
details) -v verbose mode -u skip files that are newer on the receiver
If you also add the flag --delete to the command line, it
will delete all files still present in $to but
not any longer in $from (be careful when using this option).
If for some reason the copy is interrupted abruptly, rsync can be started
again and the file transfer will resume from the point it was interrupted.
Suppose we are working at home on our own laptop, but wish
to keep a copy of the work directory on our IAC machine. We could use
a pen-drive to do this.
At home: mount /media/usb1 rsync -auv /scratch/ncaon/project1/ /media/usb1/project1/ umount
/media/usb1
Then we go to the IAC: mount /media/usb1 rsync -auv /media/usb1/project1/ /net/cherne/scratch/ncaon/pendrive/project1/ umount /media/usb1
Again, use the --delete option if you wish to delete files on the receiver which
are not on sender
We can use rsync to transfer files from/to a remote machine: set local_directory = "/net/cherne/scratch/ncaon/project2/" set remote_directory = "/scratch/ncaon/project2/" set remote machine = "donkey.zoology.univ-nebraska.edu" rsync -auvz -e ssh2 ${local_directory} ncaon@${remote_machine}:${remote_directory} -z compress file data during the
transfer. This can be useful for slow connections. -e ssh2 use ssh2 to access the remote
machine.
Consult the rsync man
page for further details and to check many other usages and interesting
options.