Saturday, 24 September 2005

Backup your data with 'rsync'

Uses of rsync
  • Copying local files.
  • Copying from the local machine to a remote machine and vice versa using a remote shell program as the transport (such as ssh or rsh).
  • Copying from a remote rsync server to the local machine and vice versa.
  • Listing files on a remote machine. This is done the same way as rsync transfers except that you leave off the local destination.
For example, if I want to take a backup of my files in the remote machine called remote_mc, then I will do the following:
$ rsync -avze ssh ravi@remote_mc:/home/ravi/ . 
... where, a sets the archive mode which is a quick way of saying you want recursion and want to preserve almost everything excluding hardlinks which can be included if you specify -H. v is verbose mode, The z option signifies that rsync compresses any data from the files that it sends to the destination machine. This option is useful on slow connections. And e allows you to choose an alternative remote shell program to use for communication between the local and remote copies of rsync. The above command will recursively copy the directory ravi/ from the remote machine to the current directory in my local machine.

Note: If I give ravi@remote_mc:/home/ravi instead of ravi@remote_mc:/home/ravi/ then it will copy all the files and directories from ravi/ directory but will not recreate the ravi directory locally.

The first time I run the above command, it will do a full backup - which might take a while for the copying to complete. But the real beauty of rsync is when I run the same command the next time, when it will check with the source and copy only those parts of files which have been modified after the first backup and will take relatively less time. This is called a differential backup.

You can even sync your backup to mirror the files and directory structure by using the --delete flag.

$ rsync -avz --delete -e ssh ravi@remote_mc:/home/ravi/ .

The --delete flag will delete any files on the receiving side which are not there on the sending side. This flag should be used with care and is advisable to first run the command with the dry run option (-n) to see what files would be deleted to make sure important files aren't listed.

Incremental backups with rsync
You can also create incremental backups with rsync. For example, look at the following command:
$ rsync -avz --backup --backup-dir=/backup --suffix=`date +"%F"`  -e ssh ravi@remote_mc:/home/ravi/ . 
Here, I have denoted that I want a backup with the --backup flag, specified that the backup should be stored in the /backup directory with the --backup-dir flag and the backed-up files should have the current date appended to their names automatically - which I have accomplished using the --suffix flag.

This post dwells briefly on the various uses of rsync. For more details you may read the rsync man page.
And yes, I found this very informative article called Easy Automated Snapshot-Style Backups with Linux and Rsync which explains in detail the numerous ways of taking a backup of your data using rsync and the most efficient of them all.

Also check out ...
Other backup scenarios in GNU/Linux

No comments:

Post a Comment