Friday, May 15, 2009

Use rsync for backups and file syncing

Rsync is a fantastic and powerful tool for Unix based systems for transferring files between locations. This makes it useful for backups as well as file synchronization. Rsync uses SSH for communications, making it encrypted and secure. Additionally, there is an rsync daemon for automating this activity. Rsync is also incremental, so any files in the originating location already present in the destination will be skipped to save time and bandwidth. Rsync is included with Mac OS X, Linux, and is available for Windows via Cygwin.

Here is the syntax for a typical rsync file transfer:
rsync -avz path/to/local/dir [username]@[remote machine]:/path/to/remote/dir
The -v and -z options make the file transfer verbose and enable the use of compression, respectively. The -a option stands for archive, which is a shortcut for a whole bunch of other options which will make the transfer recursive into directories, copy symlinks as symlinks, preserve file permissions, modification times, owner (if root), group, and device files (if root). If you want, you can have the file transfer occur between two locations on the same computer or between two remote computers.

When I recently transferred my music library between my Mac laptop and Linux desktop, the rsync command I used looked something like this:
rsync -avz --delete --exclude '.DS_Store' ~/Music me@192.168.x.x:~/Music/
The --delete option will delete any files at the destination that are not also present at the original location. The --exclude option will not transfer those file that match the specified pattern. In this case, I didn't want to transfer the annoying Mac .DS_Store files.

I kept getting an error when I tried this going from my Mac for a while. It turns out that the problem was the custom greetings I had set up in my .basrc file on the destination machine. To see if this is causing you problems, run this command:
ssh [username]@[remote machine] echo 2>/dev/null
The result of this command should be blank space. If you are getting any text at all as output, this includes any greetings, you need to disable them.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.