Using unison+inotify to realize bidirectional real-time synchronization of data
Environment: Centos 6.5 64-bit
server1:192.168.1.201
server2:192.168.1.250
Requirements software: OCaml Unison inotify
I am all installed using Yum, if you like to use the source code compiled installation can download the source package.
Yum Install OCaml unison inotify
The first step : To ensure that the two servers can be accessed via SSH without password, in order to secure, you need to create a normal user:
The same steps are performed on both sides:
Useradd Unison
passwd Unison
Su-unison
Ssh-keygen-t RSA (direct three-time return)
mkdir. SSH
SCP. Ssh/id_rsa.pub 192.168.1.250:/home/unison/.ssh/authrized_keys
SCP. Ssh/id_rsa.pub 192.168.1.201:/home/unison/.ssh/authrized_keys
chmod. SSH
Step two: Create a shell script to perform the synchronization
On the Server1.
vim/home/unison/inotify.sh
#! /bin/bash Ip2= "192.168.1.250" Src2= "/home/unison/test" Dst2= "/home/unison/test" /usr/bin/inotifywait-mrq-e Create,delete,modify,move $SRC 2 | while read line; Do /usr/bin/unison-batch $src 2 ssh://$ip 2//$DST 2 Echo-n "$line" >>/home/unison/inotify.log Echo ' date| Cut-d '-f1-4 ' >>/home/unison/inotify.log Done |
To create a script on Server2:
#! /bin/bash ip1= "192.168.1.201"
src1= "/home/unison/test" dst1= "/home/unison/test" /usr/bin/inotifywait-mrq-e Create,delete,modify,move $SRC 1 | while read line; Do /usr/bin/unison-batch $src 1 ssh://$ip 1//$DST 1 Echo-n "$line" >>/home/unison/inotify.log Echo ' date| Cut-d '-f1-4 ' >>/home/unison/inotify.log Done |
Finally, the script can be executed on two servers respectively.
If you want to start the boot, you can add to the/etc/rc.d/rc.local.
This article from the "Shallow faint" blog, please be sure to keep this source http://cuixiang.blog.51cto.com/8204722/1678737
Using unison+inotify to realize bidirectional real-time synchronization of data