Unison – file/folder sync for Centos/RHEL 6

Unison is a file-synchronization tool similar to rsync. The main difference is that unison tracks changes on both directions so you don’t to have two separate rsync to pull and push data on both folders. This means files changed on server1 will be replicated to server2 and vice versa. It’s also easier to setup since no scripting necessary.

Setup ssh keys for password less log-in

Edit /etc/ssh/sshd_config file on both server.

nano /etc/ssh/sshd_config

To activate root login, modify the below line to yes.
PermitRootLogin yes

Save and exit. Restart sshd to take effect:
service sshd restart

Now generate keys on both server: (just press enter and do not enter a passphrase)

[[email protected]~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):

[[email protected]~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):

On server1 copy the public key of server2 to the authorized_keys:

[[email protected]~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAvR4TBHPYjqet8H1xA8zmHorHz

h2OPIqYWIqYavRm0x23CtYmz84uhoRYy2F93C2rln7vx8HqHGkOECsdO82xmEdh

/o4TLPP/tCYQwbG5VOBcwEmSJYeCawm2h5cZNJHR0IRIgOdQdZRuP1ugl

== [email protected]

 

Copy the above string to authorizred_keys of server1.

[[email protected]~]# nano .ssh/authorized_keys

Save and exit, and set the permission to 600:

[[email protected]~]# chmod 600 .ssh/authorized_keys

Now to the same on server2. Copy the public keys of server1 to the authorized_keys on server2.

[[email protected]~]# cat .ssh/id_rsa.pub
ssh-rsa ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAupfkXZxJkusm1OqPai3sByuwo5wtqIqsc8

yMoKFTjiHjPdpkAdI2XdCHdkPaHX7eEQ6lFZoZHvhs0rYI4ROw/jt+A4r

UcFlKeUQPgnorRjeXlleeSciEgEK1xt5vUffpHZ6uVmJL2zv1QOV1U/

N/f8Hgjr/q8NJMOOO3EdM4ZtvsZwy5TiqE34yr96yBCyU8D2/Y== [email protected]

 

Copy the above string to authorizred_keys of server2.

[[email protected]~]# nano .ssh/authorized_keys

Save and exit, and set the permission to 600:

[[email protected]~]# chmod 600 .ssh/authorized_keys

Now SSH root login on both servers should be passwordless.

 

Install unison on both servers

Activate the Epel reposiroty on centos

yum install -y  epel-release

You should see the the epel repository

[[email protected] ~]# yum repolist Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile  * base: centosv4.centos.org  * epel: mirror.wanxp.id  * extras: centoso4.centos.org  * updates: centosi3.centos.org  repo id              repo name                                        status  base                 CentOS-6 - Base                                  6,575  epel                 Extra Packages for Enterprise Linux 6 - x86_64   12,245  extras               CentOS-6 - Extras                                62  updates              CentOS-6 - Updates                               1,607  repolist: 20,489

Search and install the unison package:

yum search unison
yum install -y unison240-txt

Create the configuration for unison on server1 only:

[[email protected]~]# mkdir ~/.unison/
[[email protected]~]# nano ~/.unison/default.prf

Put the following config file. Make changes on the local and remote folders on the first two lines below:

# Roots of the synchronization
 root = /var/www/upload/images
 root = ssh://server2//var/www/upload/images

# Paths to synchronize
 #path = current
 #path = common
 #path = .netscape/bookmarks.html

# Some regexps specifying names and paths to ignore
 #ignore = Path stats ## ignores /var/www/stats
 #ignore = Path stats/* ## ignores /var/www/stats/*
 #ignore = Path */stats ## ignores /var/www/somedir/stats, but not /var/www/a/b/c/stats
 #ignore = Name *stats ## ignores all files/directories that end with "stats"
 #ignore = Name stats* ## ignores all files/directories that begin with "stats"
 #ignore = Name *.tmp ## ignores all files with the extension .tmp

# When set to true, this flag causes the user interface to skip
 # asking for confirmations on non-conflicting changes. (More
 # precisely, when the user interface is done setting the
 # propagation direction for one entry and is about to move to the
 # next, it will skip over all non-conflicting entries and go
 # directly to the next conflict.)
 auto=true

# When this is set to true, the user interface will ask no
 # questions at all. Non-conflicting changes will be propagated;
 # conflicts will be skipped.
 batch=true

# !When this is set to true, Unison will request an extra
 # confirmation if it appears that the entire replica has been
 # deleted, before propagating the change. If the batch flag is
 # also set, synchronization will be aborted. When the path
 # preference is used, the same confirmation will be requested for
 # top-level paths. (At the moment, this flag only affects the
 # text user interface.) See also the mountpoint preference.
 confirmbigdel=true

# When this preference is set to true, Unison will use the
 # modification time and length of a file as a `pseudo inode
 # number' when scanning replicas for updates, instead of reading
 # the full contents of every file. Under Windows, this may cause
 # Unison to miss propagating an update if the modification time
 # and length of the file are both unchanged by the update.
 # However, Unison will never overwrite such an update with a
 # change from the other replica, since it always does a safe
 # check for updates just before propagating a change. Thus, it is
 # reasonable to use this switch under Windows most of the time
 # and occasionally run Unison once with fastcheck set to false,
 # if you are worried that Unison may have overlooked an update.
 # The default value of the preference is auto, which causes
 # Unison to use fast checking on Unix replicas (where it is safe)
 # and slow checking on Windows replicas. For backward
 # compatibility, yes, no, and default can be used in place of
 # true, false, and auto. See the section "Fast Checking" for more
 # information.
 fastcheck=true

# When this flag is set to true, the group attributes of the
 # files are synchronized. Whether the group names or the group
 # identifiers are synchronizeddepends on the preference numerids.
 group=true

# When this flag is set to true, the owner attributes of the
 # files are synchronized. Whether the owner names or the owner
 # identifiers are synchronizeddepends on the preference
 # extttnumerids.
 owner=true

# Including the preference -prefer root causes Unison always to
 # resolve conflicts in favor of root, rather than asking for
 # guidance from the user. (The syntax of root is the same as for
 # the root preference, plus the special values newer and older.)
 # This preference is overridden by the preferpartial preference.
 # This preference should be used only if you are sure you know
 # what you are doing!
 prefer=newer

# When this preference is set to true, the textual user interface
 # will print nothing at all, except in the case of errors.
 # Setting silent to true automatically sets the batch preference
 # to true.
 silent=true

# When this flag is set to true, file modification times (but not
 # directory modtimes) are propagated.
 times=true

Save and exit the file.

 

If there are multiple Unison profiles, where in you want to sync other directories or servers, you need to create separate profiles for each. (e.g., sync1.prf, sync2.prf) in Unison directory.

Each profile will then be called as an argument to the unison command;

[[email protected]~]# unison sync1

Now we’ll try unison and the folders should be synced, we’ll create a new file and run the unison command:

[[email protected]~]# echo "test file" > /var/www/upload/images/test.txt
[[email protected]~]# unison

See if the test file is now synced on server2 with the same time stamp

[[email protected]~]# ls -l /var/www/upload/images/test.txt

Now we can add the unison command on crontab and have it run every minute to synchronize the two folders.

[[email protected]~]# nano /etc/cron.d/unison
* * * * * root /usr/bin/unison &> /dev/null

Save and exit the crontab file. Now every minute each folder should be updated and synced.

 

 

 

————————————-

masterkenneth

Leave a Reply

Your email address will not be published. Required fields are marked *