Using Unison to Sync Two Folders

I’ve been using archlinux on both my home workstation and my laptop. Usually when I need to do serious work I’ll simply ssh to my workstation so syncing is usually not an issue. Except for one simple thing. I use mpd to play songs on both computers, and I pull lyrics on both machines. As a result I often found the lyrics of my favorite song missing on one machine. Automatic lyrics retrieval doesn’t work so well with my large collection of Jpop songs, even with my custom python script. Therefore I was looking for a way to sync the two lyrics directories on my two computers today.

And I found this little tool called Unison. It should be easy to install from any major linux distribution. The way it works is to set up a profile, which contains the location of the two directories that you want to sync. Then the command

unison profile-name

will automatically sync the two directories. To make this command work, you’ll need unison installed on both computers, and a profile file on the machine that you want to run the command, located in ~/.unison/profile-name.prf. A typical profile looks like (this is the profile I use to sync my lyrics)

# Unison Profile
label = Description of the Profile
root = /home/username/.lyrics
root = ssh://username@ssh.server.address/.lyrics
sshargs = -C -p port-number

After creating this file under ~/.unison/, the only thing you need to do is to run unison profile-name and the two folders will have the same content. When there’s a file conflict, unison will pause and ask for your input, whether to overwrite local file or the remote file, or merge the two files, similar to an svn commit. A good idea is to put this command into your crontab, and run it everyday or so, to keep the folders synced.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: