I’ve been doing some programming on my laptop recently and then finding myself without my work when I’m on my desktop. I remembered someone telling me that they use rsync to keep their source directories in-sync between their home and work machines, so I figured I’d do the same.
I thought it would be trivial to accomplish a file transfer using rsync between 2 machines on a local network, but I ended up spending an hour more than I wanted to.
So, first of all you need to have rsync on both machines (the server and the client). In my situation, I’m running Ubuntu 8.04 LTS on my desktop and Windows XP w/ Cygwin on my laptop; rsync comes pre-installed in Ubuntu and as an install option in Cygwin.
If you’ve already got Cygwin but rsync is not installed, you need to run the Cygwin setup.exe again. Then, rsync will be under the Net directory in the package choosing menu.
On the server machine you need to set up a daemon to run in the background and host the rsync services. First – before you start the daemon – you need to create an rsync daemon configuration file. To do this in Ubuntu, create a file named rsyncd.conf in the /etc directory, i.e.
you@your-computer:~$ sudo gedit /etc/rsyncd.conf
Now enter the following information into the rsyncd.conf file:
motd file = /etc/rsyncd.motd
[workspace]
path = /home/username/workspace
comment = This is the path to my Eclipse workspace (on the server)
uid = username
gid = username
read only = false
auth users = username
secrets file = /etc/rsyncd.scrt
Now replace all occurrences of username with your username on your server (not the username on your client machine!). Since I set this up for keeping my Eclipse workspaces in-sync, I used “workspace” for the path name, but you can use anything (so change “[workspace]” to “[whatever_you_want]“). I also set the path equal to my Eclipse workspace, but once again, this path can go anywhere you want to sync your files to.
Notice there are two other files mentioned: /etc/rsyncd.motd and /etc/rsyncd.scrt. You need to create these the same way you created the /etc/rsyncd.conf file.
The /etc/rsyncd.motd is the Message Of The Day file. The contents of this file will be displayed by the server when a client machine connects.
The /etc/rsyncd.scrt file contains username and password pairs. For example,
username:whatever_password_you_want
As before, username should be your username on your server.
Now you should have all the configuration information necessary, all that’s left to do is open the rsync port and start the daemon.
To open the port, open the /etc/default/rsync file, i.e.,
you@your-computer:~$ sudo gedit /etc/default/rsync
and set RSYNC_ENABLE=true.
Now to start the daemon,
you@your-computer:~$ sudo /etc/init.d/rsync restart
There you have it, your rsync server should be up and running!
Now you probably want to know how to copy files from your remote machine to your server. In Cygwin,
$ rsync -vr SRC username@server ip-address::workspace/DEST
The -vr flags are for verbose output and recursive (so entire directory structures can be copied). The SRC is the root directory of your source files. If you wanted to sync your entire Eclipse workspace with the workspace on your server, this would be the workspace directory; the recursive flag would then go through all the projects within your workspace directory and copy everything. The username should match the username in the /etc/rsyncd.conf file. The “workspace” after “::” should match your path name in the /etc/rsyncd.conf file, i.e., the “[workspace]“. DEST is then appended to the path from the /etc/rsyncd.conf file, i.e., /home/username/workspace/DEST.
When you enter this command you should see the contents of the /etc/rsyncd.motd file appear and you should be prompted for the password corresponding to the username provided. This username/password pair should match the contents of the /etc/rsyncd.scrt file. Once you enter the password correctly, the sync will begin!
Here’s an example with the username on the server “nick” and the server ip-address is “192.168.0.8″:
/etc/rsyncd.conf:
motd file = /etc/rsyncd.motd
[workspace]
path = /home/nick/workspace
comment = My Eclipse workspace directory.
uid = nick
gid = nick
read only = false
auth users = nick
secrets file = /etc/rsyncd.scrt
/etc/rsyncd.motd:
Welcome to my rsync server!
/etc/rsyncd.scrt:
nick:mybirthday
To sync the “Java2D” projects in the workspaces of the server and client machines (entered in Cygwin on client/remote machine):
$ rsync -vr /Documents and Settings/Nick/workspace/Java2D/ nick@192.168.0.8::workspace/Java2D
Welcome to my rsync server!
Password: mybirthday