inotify+unison双向同步环境部署

/ 0评 / 0

安装unison
1.首先安装ocaml,版本至少为3.07或更高
下载地址:http://caml.inria.fr/pub/distrib/ocaml-3.10/

yum install ocaml

2.安装unison

下载地址:http://www.seas.upenn.edu/~bcpierce/unison//download/releases/unison-2.13.16/

注意有些版本打包有问题,选择这个版本:

wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/unison-2.13.16/unison-2.13.16.tar.gz

tar -zxvf unison-2.13.16.tar.gz

cd unison-2.13.16

cp unison /usr/local/bin

cd ..

3. 安装inotify

下载地址:http://inotify-tools.sourceforge.net

(Fedora的话,执行 yum install inotify-tools )

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar -zxvf inotify-tools-3.14.tar.gz

cd inotify-tools-3.14

./configure
make
make install
cd ..

4. 测试同步:

unison -batch /home/images/ ssh://192.168.1.158//home/images/

5.创建同步脚本

touch /root/inotify.sh

chmod a+x /root/inotify.sh

vim /root/inotify.sh

文件内容如下:

#/bin/bash
ip2="192.168.1.158"
src2="/home/server1/"
dst2="/home/server2/"
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line;

do /usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2
    echo -n "$line " >> /var/log/inotify.log
    echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done

 

#/bin/bash 
ip1="192.168.1.157"
src1="/home/server2/"
dst1="/home/server1/"
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src1 | while read line;

do /usr/local/bin/unison -batch $src1 ssh://$ip1/$dst1
   echo -n "$line " >> /var/log/inotify.log
   echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done

 

后台运行同步脚本: 

nohup /root/inotify.sh &

 

https://my.oschina.net/luckyperson2012/blog/362018

https://my.oschina.net/u/188625/blog/117597

http://blog.chinaunix.net/uid-24963302-id-3391631.html

网站效率:0.505秒内查询了23次数据库