Main navigation

User account menu

Server Infrastructure Updates - Part 2: File System Sync

I needed a way to get the code base of the sites (Mostly Drupal 7, a few wordpress, and custom coded sites), upload images and other asorted media in sync between the two servers. I looked at the couple common utilities, I've used RSync in the past to maintain backups on other servers, but it has limitations on two way sync on multiple live servers. Enter Unison, it does exactly what the name implies, unifies multiple copies of the same set of files into 2 (or more) identical copies. Another nice selling feature of unison was that it, like Rsync, works well over SSH, keeping my data safe even over the private network that my production servers share with the others in the data center. The NixCraft site has a decent article on Unison, here. Also helpful to see the user manual on UPenn's website, here.

Here is the (sanitized) code I'm using from my servers.

I'm running it as a shell script, being called every 15 minutes by a cron job. The output of the script is being piped to a log file so I included the "echo $(date)" line at the begining and end so I can track the changes being synced.

The Unison command we can break down into bits.

"-prefer /var/www" sets the server that is calling the unison command (in my case webserver1) as the master in the event of a conflict. Because of this, I try to make any changes to the code base on the source server (webserver1) whenever possible.

"-batch" batch mode: ask no questions at all. Non-conflicting changes will be propagated; conflicts will be skipped, however when coupled with the prefer flag conflicts will setled by webserver1.

"-sshargs '-i /root/.ssh/id_rsa'" allows me to specify any needed arguments for the SSH connection, in this case, my id_rsa file I use for passwordless SSH connections.

"/var/www" local sync source

"ssh://192.168.0.2//var/www" the remote target

Thats pretty much it, this gives me complete file sync of all the files in my /var/www directory. I've also deployed similar scripts to sync user directories (specifically, MailDir) between servers. There are tons of other flags available for Unison, that can control all sorts of custom config settings, although I've found that the defaults work best in most situations.

Discliamer

Legal Stuff

This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.

All data and information provided on this site is for informational purposes only. Benjamin Townsend (BenTownsend.com) makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site & will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) – so keep it polite, please.