Thursday, 7 June 2012

Ssh and Rsync

Backups. How can I easily mirror the contents of a folder to a remote disk? Use rsync.

Rsync is a tool to synchronise the contents of folders and is something that I use daily, whether it be a manually typed command or an entry in my cronttab. In its simplest form, rsync is used like this:

rsync -r /local/directory /target/directory 

This would make a copy of the local directory into the target directory on the local machine. The -r option is required in order to recurse into directories and copy the contents. If you wanted to mirror the directory to a remote host you would need to add a username in order to log into the remote machine:

rsync -r /local/directory user@remotehost:/target/directory

When you execute this command you will be asked to provide the password for the user on the remote host. The command itself isn't very secure, if you want to encrypt the rsync then you need to send it via ssh. You could set up a key pair if you want to automate this and have everything run via a cron job without the need to enter passwords.