https://blog.51cto.com/wupengfei/1958257

Unison简介
Unison是windows和unix平台下都可以使用的文件同步工具,它能使两个文件夹(本地或网络上的)保持内容的一致。unison拥有其它一些同步工具或文件系统的相同特性,但也有自己的特点:
     1.跨平台使用;
     2.对内核和用户权限没有特别要求;
     3. unison是双向的,它能自动处理两分拷贝中更新没有冲突的部分,有冲突的部分将会显示出来让用户选择更新策略;
    4.只要是能连通的两台主机,就可以运行unison,可以直接使用socket连接或安全的ssh连接方式,对带宽的要求不高,使用类似rsync的压缩传输协议
unison各种版本下载地址:
http://www.seas.upenn.edu/~bcpierce/unison//download.html
unison编译器下载地址:
http://caml.inria.fr/download.en.html
inotify下载地址
http://inotify-tools.sourceforge.net/

一、编译安装unison
1、#编译安装ocaml
[root@104 ~]# cd /usr/local/src/
[root@104 src]# wget http://caml.inria.fr/pub/distrib/ocaml-4.03/ocaml-4.03.0.tar.gz
[root@104 src]# tar xf ocaml-4.03.0.tar.gz
[root@104 src]# cd ocaml-4.03.0
[root@104 ocaml-4.03.0]# ./configure
[root@104 ocaml-4.03.0]# make world opt
[root@104 ocaml-4.03.0]# make install

2、#编译安装Unison
[root@104 ~]# cd /usr/local/src/
[root@104 src]# wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.4.tar.gz
[root@104 src]# tar xf unison-2.48.4.tar.gz
[root@104 src]# cd src/
[root@104 src]#  yum install -y ctags-etags glibc-static
[root@104 src]# make UISTYLE=text THREADS=true STATIC=true
#使用 ”UISTYLE=text THREADS=true STATIC=true“ 表示使用命令行方式,加入线程支持,以静态模式编译
#在执行完上面的命令后,会在当前目录下生成可执行文件unison,将其复制到系统的PATH路径即可
[root@104 src]# cp unison /usr/local/bin/

二、配置双机ssh信任
unison同步远程目录时要登录到远程服务器,配置互相信任
1,在俩台机器上创建RSA密钥
(1)以root用户登录
(2)在root用户的主目录内创建.ssh目录并设置正确的权限
[root@104 ~]# mkdir ~/.ssh
[root@104 ~]# chmod -R 700 ~/.ssh
(3)使用sshh-keygen命令生产RSA密钥
[root@104 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
75:e4:e8:90:47:3b:c3:60:7a:6d:85:a0:45:db:c1:9e root@104
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|       .*oo.o    |
|       = OoB     |
|      o =.%.o    |
|       . *E+     |
|        S .      |
|                 |
|                 |
|                 |
|                 |
+-----------------+
2、#添加密钥到授权密钥文件中
(1)以root用户登录
(2)在本机执行:
[root@104 ~]# cd ~/.ssh
[root@104 .ssh]# ssh "-p 22" 192.168.9.104 cat /root/.ssh/id_rsa.pub >> authorized_keys
[root@104 .ssh]# ssh "-p 22" 192.168.9.106 cat /root/.ssh/id_rsa.pub >> authorized_keys
[root@104 .ssh]# scp authorized_keys 192.168.9.106:/root/.ssh/
[email protected]'s password:
authorized_keys                                                                               100%  780     0.8KB/s   00:00
[root@104 .ssh]# chmod 600 /root/.ssh/authorized_keys
(3)在106机器上执行
[root@106 ~]# chmod 600 /root/.ssh/authorized_keys
(4)验证测试
[root@104 .ssh]# ssh 192.168.9.104 date
Wed Aug 31 12:10:27 CST 2016
[root@104 .ssh]# ssh 192.168.9.106 date
Wed Aug 31 12:10:46 CST 2016
[root@106 ~]# ssh 192.168.9.104 date
The authenticity of host '192.168.9.104 (192.168.9.104)' can't be established.
RSA key fingerprint is 57:83:da:b9:ee:de:2a:1f:f1:74:da:ec:43:fa:7b:56.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.9.104' (RSA) to the list of known hosts.
Wed Aug 31 12:11:10 CST 2016
[root@106 ~]# ssh 192.168.9.106 date
The authenticity of host '192.168.9.106 (192.168.9.106)' can't be established.
RSA key fingerprint is 67:ff:e7:b0:5f:3b:46:e9:d7:8d:e1:10:21:02:8b:ce.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.9.106' (RSA) to the list of known hosts.
Wed Aug 31 12:11:30 CST 2016
#不需要输入密码就能显示系统日期,说明ssh互相信任配置成功

三、编译安装inotify
#inotify特性需要Linux内核的支持,安装inotify-tools确认系统内核为2.6.13版本以上,
[root@104 ~]# uname -r
2.6.32-431.el6.x86_64
[root@104 ~]# ls -l /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Aug 30 12:28 max_queued_events
-rw-r--r-- 1 root root 0 Aug 30 12:28 max_user_instances
-rw-r--r-- 1 root root 0 Aug 30 12:28 max_user_watches
如果有上面三项输出,表示系统已经默认支持inotify,接着就可以开始安装inotify-tools了。
[root@104 ~]# cd /usr/local/src/
[root@104 src]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@104 src]# tar xf inotify-tools-3.14.tar.gz
[root@104 src]# cd inotify-tools-3.14
[root@104 inotify-tools-3.14]# ./configure
[root@104 inotify-tools-3.14]# make
[root@104 inotify-tools-3.14]# make install
[root@104 inotify-tools-3.14]# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 44287 Aug 31 12:23 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 41377 Aug 31 12:23 /usr/local/bin/inotifywatch
#inotify-tools安装完成后,会生成inotifywait和inotifywatch两个指令,其中,inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。
inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。


四,配置unison,测试同步zabbix的php目录
#104编写脚本
[root@104 ~]# vim /data0/inotify.sh

#/bin/bash
ip2="192.168.9.106"
src2="/data0/a/"
dst2="/data0/a/"
/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
[root@104 ~]# chmod +x  /data0/inotify.sh
[root@104 ~]# nohup /data0/inotify.sh &
#106编写脚本
[root@106 ~]# vim /data0/inotify.sh

#/bin/bash
ip1="192.168.9.104"
src1="/data0/a/"
dst1="/data0/a/"
/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
[root@106 ~]# chmod +x /data0/inotify.sh
[root@106 ~]# nohup /data0/inotify.sh &

五、测试
05-17 10:23