Saturday, September 18, 2010

rSync-ing files from your local to a remote (Ubuntu) host

Prerequisites:
1. On the local computer, Ubuntu 10.04 desktop installed.
2. On the remote, Ubuntu 10.04.1 Server edition installed. Configured simply as an OPENSSH server.

Objective:
Backup my Thunderbird files located at /home/user/.thunderbird to the remote host.
Use the fantastically fast rsync command to quickly sync folders between my local machine and remote host. Connect securely by ssh.

Steps:
1. Create ssh keys for my desktop & server, so my ssh connection won't need to ask for passwords everytime I connect to the server.
On my desktop Ubuntu terminal window, generate the public/private key pair. Accept default values, hitting Enter for all the prompts asked. This would mean accepting the default file name for the key, and having an empty passphrase:
ssh-keygen -t dsa
Move the public key over to the remote host:
ssh-copy-id username@remotehost
replacing username for your actual user name, and remotehost with the IP address of the server, like
ssh-copy-id lavezarez@10.42.43.10
A possible pitfall to avoid - ensure you have the same user name on BOTH your local and server machines.
Login to the server via ssh, and ensure correct permissions to the public key.
ssh lavezarez@10.42.43.10
chmod 600 .ssh/authorized_keys

2. Create the backup destination folder in the server.
You may use Nautilus to connect via SFTP to the server. Press Ctrl-L to open up the location bar, and type sftp://username@remotehost (e.g. sftp://lavezarez@10.42.43.10)
Open up the extra pane (on the View menu of Nautilus) or hotkey F3. That way you can see folders on your local and server machines at the same time, much like winscp for Windows.

You may want to change permissions for the new folder using chmod. Octal 750 is a usual permission value.

3. Finally the rsync. Here's a sample executable script -
#!/bin/sh
rsync --progress -avhe ssh --delete /home/lavezarez/.thunderbird/ lavezarez@10.42.43.10:emailbackup/
echo -n Press return to continue...
read ans

---
An initial rsync for a 3gb-sized folder took about 4 minutes to transfer to the server, running on a typical 100mbps LAN connection.

Running the rsync after that completes the transfer process much faster, under 5 seconds, 10 seconds, etc. depending on what files changed in the source.

---
Changing ssh servers:
Should you change one server with another, while maintaining the same IP address, you must clear your /home/<user>/.ssh/known_hosts file before attempting to ssh to that server.

No comments:

Post a Comment