unison+inotify实现文件数据双向实时同步
Unison是一款跨平台的文件同步工具,不仅支持本地对本地同步,
也支持通过SSH、RSH和Socket等网络协议进行同步。更棒的是,
Unison支持双向同步操作,你既可以从A同步到B,也可以从B同步到A,这些都不需要额外的设定。
系统:CentOS7.4 两台
server1:192.168.10.10
server2:192.168.10.11
一.安装Objective Caml compiler
Objective Caml compiler (version 3.11.2 or later) 官网地址:http://caml.inria.fr/
cd /opt
wget http://caml.inria.fr/pub/distrib/ocaml-4.03/ocaml-4.03.0.tar.gz
tar -zxvf ocaml-4.03.0.tar.gz
cd ocaml-4.03.0
./configure
make configure
make world opt
make install
二.安装unison
如果需要单向同步到远程目录,则远程机器也需要安装unison。
(例如只需要A机器远程实时同步文件到B机器,B不需同步到A。则两台机器都需要安装unison.)
yum -y install ctags-etags # 缺少此安装包时下面make步骤会报错
cd /opt
wgethttp://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.4.tar.gz
mkdir unison-2.48.4 && cd unison-2.48.4
tar -zxvf /opt/unison-2.48.4.tar.gz
cd src
make UISTYLE=text THREADS=true
cp unison /usr/local/bin/
unison -version # 有版本信息出现,则安装成功
三.安装inotify
inotify官方地址:https://en.wikipedia.org/wiki/Inotify
yum -y install inotify-tools (yum源安装)
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar xvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
四.双机ssh信任
server1上执行
ssh-keygen -t rsa 一直打回车就行了
ssh-copy-id [email protected] #回车输入11的密码
server2上执行
ssh-keygen -t rsa 一直打回车就行了
ssh-copy-id [email protected] #回车输入10的密码
#注 !如果非22端口,如2222端口,如下:
ssh-keygen -t rsa 一直打回车
ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 2222 [email protected]"
五.unison的使用
unison的用法非常灵活和简单,可以通过如下三种方式调用unison。
第一种方式:”unison profile_name [options]”
unison默认会读取~/.unison目录下的配置文件”default.prf”
第二种方式:”unison profile root1 root2 [options]”
root1、root2分别表示要执行同步的两个路径。这两个路径可以是本地目录路径,也可以是远程服务器的路径,如ssh://username@//tmp/test 。由于同步的路径已经在命令行指定了,所以这里无需在profile.prf配置文件中进行root指令的相关设置。
第三种方式:”unison root1 root2 [options]”
这种方式相当于执行”unison default root1 root2”命令,即unison默认读取default.prf的配置。
六.配置双机文件目录同步(如需单向同步则只需要配置server端)
unison默认读取/root/.unison/default.prf的配置.但是安装完成后默认不会创建这个文件.需要手动创建
mkdir -p /root/.unison/
vim /root/.unison/default.prf
配置文件如下:
#Unison preferences file
root = /data/share/
root = ssh://[email protected]//data/share/
#force =
#ignore =
batch = true
maxthreads = 300
#repeat = 1
#retry = 3
owner = true
group = true
perms = -1
fastcheck = false
rsync = false
sshargs = -C
xferbycopying = true
log = true
logfile = /root/.unison/unison.log
七.创建同步脚本
mkdir -p /opt/script
cd /opt/script
vim inotify-unison.sh
#!/bin/bash
##########################################
/usr/bin/inotifywait -mrq /data/share/ --format "%w%f" -e create|while read line
do
echo `date +%F' '%T' '%A`
/usr/local/bin/unison
echo -e ' \n \n '
done
#保存脚本文件
chmod +xinotify-unison.sh
八.编写脚本启动停止服务
vim /etc/init.d/unison_inotify #以下为脚本内容
#!/bin/bash
# intofitywait watch /data/share/
# unison --- sync from /data/share to //remote//data/share
. /etc/rc.d/init.d/functions
use_username=root
log_path=/$use_username/.unison/unison-diy.log
procID=`ps aux |grep inotify-unison.sh |grep -v grep|awk '{print $2}'`start() {
echo -n $"Starting unison_inotify... "
su - $use_username -c "nohup /script/inotify-unison.sh >>$log_path 2>&1 &"
RETVAL=$?
[ $RETVAL = 0 ] && action
}
stop() {
#echo $"Stopping unison_inotify ... "
kill $procID >/dev/null 2>&1
RETVAL=$?
[ $RETVAL = 0 ] && echo "Stopping unison_inotify... `action`" || echo 'unison_inotify(PID) is not run'
}
status() {
check_proc=`ps aux |grep inotify-unison.sh |grep -v grep|awk '{print $2}'|wc -l`
if [ $check_proc -ne 0 ]
then
echo 'unison_inotify is running'
else
echo 'unison_inotify is not run'
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo $"Usage: $prog {start|stop|status}"
RETVAL=2
esac
exit $RETVAL
#保存文件
chmod +x/etc/init.d/unison_inotify
启动|关闭|状态|重启脚本文件
/etc/init.d/unison_inotify start|stop|status|restart
9.inotify优缺点
优点: 监控文件系统事件变化, 通过同步工具实现实时数据同步.
缺点: 并发如果大于200个文件 ( 10-100K ), 同步就会有延迟.
10.注意事项
如果inotify监控文件过大(T级别文件)需要配置
查看系统默认参数值
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances = 128
修改参数:
vim /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
:wq! #保存退出
sysctl -p #生效
参数说明:
max_queued_events #inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches #要同步的文件包含多少目录,可以用:find /data -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/data为同步文件目录)
max_user_instances #每个用户创建inotify实例最大值.