Home > Uncategorized, mysql, php, server, sync > Keeping directories in sync with unison

Keeping directories in sync with unison

As opposed to rsync, unison is a two way file synchronizer. Similar to MySQL’s master to master replication. To set it up in Ubuntu (Intrepid) keep on reading…

1. Install unison on both servers

apt-get install unison

2. Create a script to add to crontab to call unison periodically

unison -numericids -owner -silent -times -contactquietly -batch -auto -log -logfile /var/log/unison.log profile_file 2>&1

3. Create the above mentioned profile file on ~/.unison/profile_file.prf

root = ssh://server_ip//path/to/root/dir
root = /path/to/root/dir
ignore = Regex .*(cache|Cache|te?mp|logs?).* # ignore log and cache files

That’s it! Now add the following line to your crontab to execute it every 5min

*/5 * * * * /path/to/my_unison_script.sh

After a few days of running this I found that sometimes one unison execution would overlap another. To resolve this I found a little tool called lockrun

To get it to work I did:

1. Download the source code from the official lockrun website

wget http://unixwiz.net/tools/lockrun.c

2. Build/install as shown in http://unixwiz.net/tools/lockrun.html

gcc lockrun.c -o lockrun
sudo cp lockrun /usr/local/bin/

3. Change the previous crontab line to use lockrun

*/5 * * * * /usr/local/bin/lockrun --lockfile=/tmp/hosting.lockrun -- /path/to/my_unison_script.sh

All done!!!

http://blog.melimato.com/wp-content/plugins/sociofluid/images/digg_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/reddit_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/stumbleupon_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/delicious_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/technorati_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/google_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/myspace_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/facebook_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/yahoobuzz_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/twitter_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/meneame_24.png
Categories: Uncategorized, mysql, php, server, sync Tags: , , ,
  1. May 10th, 2009 at 20:38 | #1

    Thanks for article. Everytime like to read you.

  2. September 9th, 2009 at 13:54 | #2

    I had this running nicely as it was from the command line, but saw /var/log/unison.log that it was bombing out when run from crontab with the following error:

    Sys_error(“Broken pipe”)

    My fix was to use nohup in the crontab:

    [code]
    */5 * * * * nohup /usr/local/bin/lockrun --lockfile=/tmp/hosting.lockrun -- /root/bin/my_unison_script.sh > /dev/null 2>&1
    [/code]

    This seems to have done the trick.

  3. September 9th, 2009 at 14:20 | #3

    I also recommend considering having “CONFLICT” resolution in the profile.

    e.g. When both ends have been modified, prefer the local copy.

    [code]
    # Resolve conflicts in favor of local root
    prefer=/path/to/root/dir
    [/code]

    In particular this is good when you are sync-ing two directories which have already been duplicated. Unison makes a big song and dance of flagging all of the files as “CONFLICTS” in the log, which gets very large, if you don’t start with a master at only one end.

    Alternatively, be sure to start with an empty directory at one end and then the conflicts will be genuine issues.

    A big thanks for this article pablo, it is a great way to get started!

  1. No trackbacks yet.

Comment moderation is enabled. Your comment may take some time to appear.

Additional comments powered by BackType