您的位置:首页 > 运维架构 > Linux

unison+inotify实现文件实时双向同步部署步骤

2014-11-21 14:03 726 查看
一.编译安装unison
unison各版本下载地址 http://www.seas.upenn.edu/~bcpierce/unison//download.html unison编译器下载地址 http://caml.inria.fr/pub/distrib (版本至少3.0.7)

1.环境说明 (在其他地方部署时修改下IP与同步目录即可)
host1 192.168.42.241 同步目录 /root/test
host2 192.168.42.243 同步目录 /root/test

2. 安装Objective Caml compiler(若安装的unison的是二进制程序,则不用安装。因二进制程序版本都太老,这里选择编译安装)
[root@241 ~]# cd /usr/src/
[root@241 src]# wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.0.tar.gz [root@241 src]# tar xf ocaml-3.12.1+rc1.tar.gz
[root@241 src]# tar xf ocaml-3.12.0.tar.gz
[root@241 src]# cd ocaml-3.12.0
[root@241 ocaml-3.12.0]# ./configure
[root@241 ocaml-3.12.0]# make world opt
[root@241 ocaml-3.12.0]# make install

3.编译安装Unison
[root@241 ~]# wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.40.102.tar.gz [root@241 src]# tar xf unison-2.40.102.tar.gz
[root@241 src]# cd unison-2.40.102
[root@241 unison-2.40.102]# make UISTYLE=text THREADS=true STATIC=true
[root@241 unison-2.40.102]# cp unison /usr/local/bin/ #将生成的二进制程序复杂到系统的PATH路径里
[root@241 unison-2.40.102]# scp unison root@192.168.42.243:/usr/local/bin/unison #将unison二进制程序复制到host2

二.配置双机ssh信任
[root@241 ~]# ssh-keygen
[root@243 ~]# ssh-keygen
[root@243 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.42.241
[root@241 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.42.243

三.unison配置
1.编辑unison配置文件
[root@241 ~]# vim .unison/default.prf

# Unison preferences file
#root = /root/test #两个root表示要同步的文件夹(下面的同步脚本定义了同步目录,所以这里注释掉,否则会冲突)
#root = ssh://192.168.42.243//root/test
#path = /root/test #两个path表示只同步指定的子目录及文件,而非整个目录
#path = /root/test
#ingnore = PATH test/txt #ingnore = Path 表示忽略/root/test 下面的test/txt
batch = true #表示全自动模式,接受默认动作
#maxthreads = 300
#repeat = 1
#retry = 3
owner = true #owner group perms表示保持原来的属主 属组 权限
group = true
perms = -1
fastcheck = true #true表示同步时通过文件的创建时间来比较两地文件 false表示比较两地文件的内容
rsync = false
#debug = verbose
sshargs = -C #表示使用ssh的压缩方式传输
xferbycopying = true
confirmbigdel = false #true表示当需要同步的两个目录一个为空时,unison将停止 false表示同步的某个目录为空时不会停止运转
log = true
logfile = /root/.unison/unison.log

#host2 配置文件一样
四.安装inotify
1.查看系统是否支持inotify
[root@241 ~]# ls -l /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Nov 11 11:25 max_queued_events
-rw-r--r-- 1 root root 0 Nov 11 11:25 max_user_instances
-rw-r--r-- 1 root root 0 Nov 11 11:25 max_user_watches

2.安装inotify
[root@241 src]# tar xf inotify-tools-3.14.tar.gz
[root@241 src]# cd inotify-tools-3.14
[root@241 inotify-tools-3.14]# ./configure
[root@241 inotify-tools-3.14]# make && make install

echo 30000000 > /proc/sys/fs/inotify/max_user_watches (注意:重启机器后此设置会消失,如果监控的文件数目不是特别多,可以不设置)
3.编写inotify实时监控脚本
[root@241 ~]# vim unison.sh

#!/bin/bash
DESTHOST=192.168.42.243
DESTHOSTDIR=/root/test
SRCDIR=/root/test
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e create,delete,modify,move $SRCDIR | while read line; do
/usr/local/bin/unison $SRCDIR ssh://$DESTHOST/$DESTHOSTDIR
done

chmod +x unison.sh

4.按同样方法在host2安装inotify和编写同步脚本

5.分别在host1 host2 让脚本运行于后台
[root@241 ~]# nohup ./unison.sh &
[root@243 ~]# nohup ./unison.sh &
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux unison inotify