Unison+Inotify 实时双向同步
Unison+Inotify可以实现,无论客户端还是服务器,只有发生变化全部进行同步,前置条件,两台服务器必须互相建立SSH密钥对验证,其次服务器和客户端必须同时安装以下软件,做相同的配置。
1.安装Inotify
[root@localhost ~]# wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
[root@localhost ~]# tar -xzvf inotify-tools-3.13.tar.gz
[root@localhost ~]# cd inotify-tools-3.13/
[root@localhost ~]# ./configure
[root@localhost ~]# make && make install
2.配置内核优化
[root@localhost ~]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost ~]# sysctl -p
3.安装Unison
a)安装依赖
[root@localhost ~]# wget http://caml.inria.fr/pub/distrib/ocaml-4.06/ocaml-4.06.0.tar.gz
[root@localhost ~]# tar -xzvf ocaml-4.06.0.tar.gz
[root@localhost ~]# cd ocaml-4.06.0/
[root@localhost ~]# ./configure
[root@localhost ~]# make world opt && make install
b)安装Unison
[root@localhost ~]# wget https://codeload.github.com/bcpierce00/unison/tar.gz/v2.51.2
[root@localhost ~]# tar -xzvf unison-2.51.2.tar.gz
[root@localhost ~]# cd unison-2.51.2/
[root@localhost ~]# make UISTYLE=text THREADS=true static=true
[root@localhost ~]# cp -a src/unison /usr/local/bin/ #只需要此主程序
[root@localhost ~]# chmod 755 /usr/local/bin/unison #赋予执行权限
3.服务器和客户端同时运行脚本
!/bin/bash
date="inotifywait -mrq -e create,delete /本地目录"
sync="/usr/local/bin/unison -batch /本地目录/ ssh://root@远程同步IP//同步到远程目录"
$date | while read DIRECTORY EVENIS FILE
do
$sync
done