Monday, January 28, 2013

2 Way Synchronization with Unison

2 Way directory Synchronization

I found unison is a better option than rsync if the files in both locations frequently change, or if you want to synchronise between more than 2 locations. and you need to keep all locations synchronised.

yum install unison 
on all machines that you want to synchronise between.

for password-less sync make sure you setup ssh private/public key
as described in this previous post ssh public/private key

create a file sync.sh

vim sync.sh

copy and paste the following into your file


#!/bin/bash
# set paths / dirs
_paths="/home/cgerada/directory_to_sync"

# binary file name
_unison=/usr/bin/unison

# server names 
# sync local +server1 with server2 and server3
_rserver="server1.clive.com server2.clive.com server3.clive.com"

# sync it
for r in ${_rserver}
do
        for p in ${_paths}
        do
                ${_unison} -batch -force newer -times "${p}"  "ssh://${r}/${p}"
        done
done

save the file and give it execute rights

chmod + x sync.sh

to run the script on a cronjob every half an hour and output details into a log file

crontab -e

and add the following

*/30 * * * * /path/to/sync.sh &>/var/log/sync.sh.log

save and you are done.

~                                                                          
~                                                  

No comments: