Hi Team,
Some time back, I need to implement a 2 way sync between 2 servers. I searched in the internet and implemented it. The following link helped me 80% of the work. Any how wanted to write a step by step tutorial. Please let me know any points I missed or any corrections.
Source : Details of two-way sync between two Ubuntu machines | (R news & tutorials)
Environment
Server 1 = 1.2.3.4
Server 2 = 5.6.7.8
Un privilaged user on each server = one
Dir to Sync = /tmp/synctest
Step 1: Implement Password less ssh login between 2 servers
Server 1
Code:one@server1:~> ssh-keygen -t rsaCopy the content of the file “home/one/.ssh/id_rsa.pub” to the following file on server2 “home/one/.ssh/authorized_keys”Code:Generating public/private rsa key pair. Enter file in which to save the key (/home/one/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/one/.ssh/id_rsa. Your public key has been saved in /home/one/.ssh/id_rsa.pub. The key fingerprint is: f6:61:a8:27:35:cf:4c:6d:13:22:70:cf:4c:c8:a0:23 one@server1
Server 2
Code:one@server2:~> ssh-keygen -t rsaCopy the content of the file “home/one/.ssh/id_rsa.pub” to the following file on server1 “home/one/.ssh/authorized_keys”Code:Generating public/private rsa key pair. Enter file in which to save the key (/home/one/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/one/.ssh/id_rsa. Your public key has been saved in /home/one/.ssh/id_rsa.pub. The key fingerprint is: f6:61:a8:27:35:cf:4c:6d:13:22:70:cf:5c:c8:a0:24 one@server2
Now we can ssh between 2 server as one without any password.
Step2: Install lsyncd and Unison on both the servers (as root).
Server 1
Server 2Code:root@server1 # mkdir /tmp/lsyncd root@server1 # cd /tmp/lsyncd root@server1 # wget http://lsyncd.googlecode.com/files/lsyncd-2.0.6.tar.gz root@server1 # tar -zxvf lsyncd-2.0.6.tar.gz root@server1 # cd lsyncd-2.0.6 root@server1 # aptitude install gcc pkg-config lua5.1 liblua5.1-0-dev liblua50-dev liblualib50-dev root@server1 # ./configure root@server1 # make root@server1 # make install root@server1 # aptitude install unison
Step 3: Configure lsyncd (as the user one)Code:root@server2 # mkdir /tmp/lsyncd root@server2 # cd /tmp/lsyncd root@server2 # wget http://lsyncd.googlecode.com/files/lsyncd-2.0.6.tar.gz root@server2 # tar -zxvf lsyncd-2.0.6.tar.gz root@server2 # cd lsyncd-2.0.6 root@server2 # aptitude install gcc pkg-config lua5.1 liblua5.1-0-dev liblua50-dev liblualib50-dev root@server2 # ./configure root@server2 # make root@server2 # make install root@server2 # aptitude install unison
Server 1
Code:one@server1:~$ mkdir /tmp/synctest one@server1:~$ vim lsyncd.confCode:settings = { logfile = "/tmp/lsyncd.log", statusFile = "/tmp/lsyncd.status", maxDelays = 3 } runUnison2 = { maxProcesses = 1, delay = 3, onAttrib = "/usr/bin/unison -batch /tmp/synctest ssh://5.6.7.8//tmp/synctest", onCreate = "/usr/bin/unison -batch /tmp/synctest ssh://5.6.7.8//tmp/synctest", onDelete = "/usr/bin/unison -batch /tmp/synctest ssh://5.6.7.8//tmp/synctest", onModify = "/usr/bin/unison -batch /tmp/synctest ssh://5.6.7.8//tmp/synctest", onMove = "/usr/bin/unison -batch /tmp/synctest ssh://5.6.7.8//tmp/synctest", } sync{runUnison2, source="/tmp/synctest"}
Server 2
Code:one@server2:~$ mkdir /tmp/synctest one@server2:~$ vim lsyncd.confStep 4: Start lsyncd, Monitor the logs make sure all the sync is going goodCode:settings = { logfile = "/tmp/lsyncd.log", statusFile = "/tmp/lsyncd.status", maxDelays = 3 } runUnison2 = { maxProcesses = 1, delay = 3, onAttrib = "/usr/bin/unison -batch /tmp/synctest ssh://1.2.3.4//tmp/synctest", onCreate = "/usr/bin/unison -batch /tmp/synctest ssh://1.2.3.4//tmp/synctest", onDelete = "/usr/bin/unison -batch /tmp/synctest ssh://1.2.3.4//tmp/synctest", onModify = "/usr/bin/unison -batch /tmp/synctest ssh://1.2.3.4//tmp/synctest", onMove = "/usr/bin/unison -batch /tmp/synctest ssh://1.2.3.4//tmp/synctest", } sync{runUnison2, source="/tmp/synctest"}
Server 1
Code:one@server1:~$ lsyncd -log Exec lsyncd.conf one@server1:~$ -ef|grep lsyncCode:livesync 998 1 0 05:37 ? 00:00:00 lsyncd -log Exec lsyncd.conf livesync 4512 529 0 07:05 pts/0 00:00:00 grep lsyncServer2Code:one@server1:~$ tail -f /tmp/lsyncd.log
Code:one@server2:~$ lsyncd -log Exec lsyncd.conf one@server2:~$ ps -ef |grep lsyncdCode:livesync 16939 1 0 21:38 ? 00:00:00 lsyncd -log Exec lsyncd.conf livesync 17261 17241 0 23:10 pts/3 00:00:00 grep lsyncdNote: 1. Never Keep the sync log inside the dir which is going to be synced. It will leads to continuous sync.Code:one@server2:~$ tail -f /tmp/lsyncd.log
2. If you delete all files in any of the server, the Unison will stop working (Its due to the default behaviour).