Linux下使用unison+inotify实现双向实时同步

作者:Garany 发布于:2015-08-31 分类:破万卷书

Unison是windows和unix平台下都可以使用的文件同步工具,它能使两个文件夹(本地或网络上的)保持内容的一致。
Unison有文字界面和图形界面,这里只介绍如何在文字界面下使用.
unison拥有其它一些同步工具或文件系统的相同特性,但也有自己的特点:
跨平台使用;
对内核和用户权限没有特别要求;
unison是双向的,它能自动处理两分拷贝中更新没有冲突的部分,有冲突的部分将会显示出来让用户选择更新策略;
只要是能连通的两台主机,就可以运行unison,可以直接使用socket连接或安全的ssh连接方式,对带宽的要求不高,使用类似rsync的压缩传输协议。

一、配置ssh key信任 
建议通过普通用户进行操作,通过root操作本身就危险,免密码登陆的root就更危险了。 

在两台服务器上创建unison用户 
[root@server1 ~]# useradd -m unison 
[root@server1 ~]# passwd unison 

[root@server2 ~]# useradd -m unison 
[root@server2 ~]# passwd unison 

在server1上创建key并配置server2的信任 
[root@server1 ~]# su – unison 
[unison@server1 ~]$ ssh-keygen -t rsa 
在提示保存私钥(key)和公钥(public key)的位置时,使用默认值; 
在提示是否需要私钥密码(passphrase)时,直接敲回车,即不使用私钥密码。 
之后,将生成一对密钥,id_rsa(私钥文件)和id_rsa.pub(公钥文件),保存在/home/unison/.ssh/目录下。 

将公钥添加到server2的 authorized_keys 文件中 (假设server2主机IP为192.168.10.2) 
[unison@server2 ~]$ mkdir .ssh 
[unison@server2 ~]$ chmod 700 .ssh 
[unison@server2 ~]$ scp unison@192.168.10.1:/home/unison/.ssh/id_rsa.pub ./
[unison@server2 ~]$ mv ./id_rsa.pub .ssh/authorized_keys 

同理,在server2上创建key并配置server1的信任 
[root@server2 ~]# su – unison 
[unison@server2 ~]$ ssh-keygen -t rsa 
将文件上传到server1(假设server1主机IP为192.168.10.3) 
[unison@server1 ~]$ scp unison@192.168.10.4:/home/unison/.ssh/id_rsa.pub ./
[unison@server1 ~]$ mv ./id_rsa.pub .ssh/authorized_keys 

重启SSH服务 
[root@server1 ~]# /etc/init.d/sshd restart 
[root@server2 ~]# /etc/init.d/sshd restart 
二、安装unison
unison各种版本下载地址:
http://www.seas.upenn.edu/~bcpierce/unison//download.html
unison编译器下载地址:
http://caml.inria.fr/pub/distrib/ocaml-3.10

Linux下通过源码包编译安装Unison时,需要用到Objective Caml compiler。 
通过以下方式安装 
[root@server1 ~]# wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.0.tar.gz 
[root@server1 ~]# tar -xzvf ocaml-3.12.0.tar.gz 
[root@server1 ~]# cd ocaml-3.12.0 
[root@server1 ocaml-3.12.0]# ./configure 
[root@server1 ocaml-3.12.0]# make world opt 
[root@server1 ocaml-3.12.0]# make install 
编译安装Unison 

[root@server1~]#wget \
http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.40.63.tar.gz
[root@server1 ~]# tar -xzvf unison-2.40.63.tar.gz
[root@server1 ~]# cd unison-2.40.63
[root@server1 unison-2.40.63]# make UISTYLE=text
[root@server1 unison-2.40.63]# make install

在执行make install的过程中,可能会出现以下错误提示: 
mv: cannot stat '/root/bin//unison': No such file or directory 
make: [doinstall] Error 1 (ignored) 
cp unison /root/bin/ 
cp: cannot create regular file '/root/bin/': Is a directory 
make: *** [doinstall] Error 1 

出现错误的原因在与Unison默认是将文件Copy到/root/bin目录,但Linux默认是没有该目录的,因此我们需要将生成的可执行文件unison复制到系统的PATH目录。 
[root@server1 unison-2.40.63]# cp unison /usr/local/bin 

将可执行文件unison上传到远程主机(假设远程本机IP为192.168.10.3) 

[root@server2 ~]#scp -P22 root@192.168.10.3:/usr/local/bin/unison /usr/loca/bin/ 
三、Unison的配置与使用 
在两台服务器上创建test目录,用于测试 
[root@server1 ~]# su - unison 
[unison@server1 ~]$ mkdir test 

[root@server2 ~]# su - unison 
[unison@server2 ~]$ mkdir test 

在两台服务器上分别执行一次unison,如果出现提示确认,则直接敲回车选择默认值 
[unison@server1~]$ unison /home/unison/test/ ssh://unison@192.168.10.4//home/unison/test/ 
[unison@server2~]$ unison /home/unison/test/ ssh://unison@192.168.10.3//home/unison/test/ 

修改两台服务器的unison配置文件,输入以下内容 
[unison@server1 ~]$ vim /home/unison/.unison/default.prf 
view sourceprint?01 #Unison preferences file 
root = /home/unison/test 
root = ssh://unison@192.168.10.4//home/unison/test/ 
#force = 
#ignore = 
batch = true 
#repeat = 1 
#retry = 3 
owner = true 
group = true 
perms = -1 
fastcheck = false 
rsync = false 
sshargs = -C 
xferbycopying = true 
log = true 
logfile = /home/unison/.unison/unison.log 

[unison@server2 ~]$ vim /home/unison/.unison/default.prf 
view sourceprint?01 #Unison preferences file 
root = /home/unison/test 
root = ssh://unison@192.168.10.3//home/unison/test/ 
batch = true 
owner = true 
group = true 
perms = -1 
fastcheck = false 
rsync = false 
sshargs = -C 
xferbycopying = true 
log = true 
logfile = /home/unison/.unison/unison.log 

相关注解如下: 
force表示会以本地所指定文件夹为标准,将该目录同步到远端。
这里需要注意,如果指定了force参数,那么Unison就变成了单项同步了,也就是说会以force指定的文件夹为准进行同步,类似与rsync。 
Unison双向同步基本原理是:假如有A B两个文件夹,A文件夹把自己的改动同步到B,B文件夹也把自己的改动同步到A,最后A B两文件夹的内容相同,是AB文件夹的合集。 
Unison双向同步的一个缺点是,对于一个文件在两个同步文件夹中都被修改时,unison是不会去同步的,因为unison无法判断以那个为准。 
ignore = Path表示忽略指定目录,即同步时不同步它。 
batch = true,表示全自动模式,接受缺省动作,并执行。 
-fastcheck true 表示同步时仅通过文件的创建时间来比较,如果选项为false,Unison则将比较两地文件的内容。 
log = true 表示在终端输出运行信息。 
logfile 指定输出的log文件。 
另外,Unison有很多参数,这里仅介绍常用的几个,详细的请参看Unison手册。 
-auto //接受缺省的动作,然后等待用户确认是否执行。 
-batch //batch mode, 全自动模式,接受缺省动作,并执行。 
-ignore xxx //增加 xxx 到忽略列表中 
-ignorecase [true|false|default] //是否忽略文件名大小写 
-follow xxx //是否支持对符号连接指向内容的同步 
owner = true //保持同步过来的文件属主 
group = true //保持同步过来的文件组信息 
perms = -1 //保持同步过来的文件读写权限 
repeat = 1 //间隔1秒后,开始新的一次同步检查 
retry = 3 //失败重试 
sshargs = -C //使用ssh的压缩传输方式 
xferbycopying = true" 
-immutable xxx //不变目录,扫描时可以忽略 
-silent //安静模式 
-times //同步date.html' target='_blank'>修改时间 
-path xxx 参数 //只同步 -path 参数指定的子目录以及文件,而非整个目录,-path 可以多次出现。 
PS:Windows下的unison配置文件默认位于C:\Documents and Settings\currentuser\.unison目录,默认的配置文件名是default.prf。 


四、测试 
首先分别在server1与server2的/home/unison/test目录下创建文件或目录,然后在server1上执行unison,接着如果在server1与server2上都能看到各自创建的文件,就说明同步成功。 
分别在server1与server2上创建文件 
[unison@server1 ~]$ cd test 
[unison@server1 test]$ touch 1.txt touch 3.txt 
[unison@server2 ~]$ cd test 
[unison@server2 test]$ touch 2.txt touch 4.txt 
在server1上执行unison 
[unison@server1 ~]$ unison 
在server1与server2上查看文件是否同步 
[unison@server1 ~]$ cd test 
[unison@server1 test]$ ls 
1.txt 2.txt 3.txt 4.txt 
[unison@server2 ~]$ cd test 
[unison@server2 test]$ ls 
1.txt 2.txt 3.txt 4.txt 
均看到了“1.txt 2.txt 3.txt 4.txt”所有文件,说明文件同步已经成功! 
注意:第一次SSH连接的时候可能需要输入一次密码,之后就不需要输入了。 
五、定期或实时执行同步 

1.如果想要定期执行,则通过crontab计划任务来实现,例如通过以下方式设置每5分钟执行一次 
[root@server1 ~]# su - unison 
[unison@server1 ~]$ crontab -e 
view sourceprint?1 */5 * * * * /usr/local/bin/unison 
如果想要实时同步,则需要结合inotify-tools来实现,具体实现方式请参考这一篇文章: 
通过rsync+inotify-tools+ssh实现触发式远程实时同步

2.如果想实时同步,可配合inotify使用。
inotify安装
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar -zxf inotify-tools-3.13.tar.gz -C /opt/src
cd /opt/src/inotify-tools-3.13/
./configure --prefix=/usr/local/inotify  && make && make install
ln -s /usr/local/inotify/bin/* /usr/local/bin/
实时同步脚本要在unison配置文件里指定要同步的路径inotify 里指定检测节点目录:
#! /bin/bash
src="/home/ming/test/"
ip="192.168.6.117"
des="/home/ming/test/"
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' -e modify,delete,create,attrib,move ${src} | while read  file;
do
                 /usr/local/bin/unison  -silent -batch
done
/root/inotifyunison.sh &

标签: linux 同步

我来说说