Using rsync in Linux

Instructions to help securely synchronize files and folders between computers using rsync

One thing I do often is copy files and folders between my computers. At first I used Samba, but I found it to be slow, partially due to the protocol and partially because every part of every file was transferred. Now I use rsync because it is faster, requires very little setup, only transfers changed parts of files and includes automatic compression. Much better!

The syntax for synchronizing two directories on the same computer is:

rsync -avz --progress --stats --delete [sourcedir] [targetdir]

It also works well with OpenSSH, so I recommend you follow the instructions in my OpenSSH post first. Assuming OpenSSH is installed and set up, a single command can be used to completely synchronize a directory between my PC and my server:

rsync -avz --progress --stats --delete [sourcedir] [remoteusername]@[remotehostname]:[targetdir]

For example, to synchronize my ~/Projects directory on both computers:

rsync -avz --progress --stats --delete ~/Projects/ [email protected]:~/Projects

Excluding Files and Folders

There are certain files I don't want copied over to my server, though, such as *.pyc files. This can easily be fixed by adding the --exclude '*.pyc' argument:

rsync -avz --progress --stats --delete --exclude '*.pyc' ~/Projects/ [email protected]:~/Projects

Of course, as you find more files and directories you don't want copied, this command will become quite long and prone to mistakes. If this is the case, you can simply tell rsync to read a list of patterns for files and directories you don't want copied from another file. So I made a file called rsync.exclude and put the following lines in it:

*.pyc
logs/
local*

This will tell rsync to not transfer my *.pyc files, my logs directory and anything beginning with local. The new rsync command would be:

rsync -avz --progress --stats --delete --exclude-from 'rsync.exclude' ~/Projects/ [email protected]:~/Projects

When you get used to rsync and realize its power, you can see how good it would be as a backup tool. I use it for everything, from backups to updating my website from my development environment to the live server, even just moving things around between computers. There's a lot more you can do with rsync, but I hope I've covered the basics to get you started!

Posted by Christopher Wassall on Sat, 10 Oct 2009 23:43:52 GMT

Comments

There are no comments yet.

Post a Comment

You must be logged in to post a comment