unison双向同步
安装unison
wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.1.tar.gz
wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.40.63.tar.gz


tar -xf ocaml-3.12.1.tar.gz
cd ocaml-3.12.1
。/configure
make world opt
make install


tar -xf unison-2.40.63.tar.gz
cd unison-2.40.63
make UISTYLE=text THREADS=true STATIC=true
cp unison /usr/local/bin/

unison的使用

本地使用unison
unison /test1 /test2
Contacting server...
Looking for changes
Reconciling changes

test1          test2             
new dir  ---->            filetest2  [f] f

Proceed with propagating updates? [] y
Propagating updates
UNISON 2.40.63 started propagating changes at 21:56:42.55 on 05 Apr 2012
[BGN] Copying filetest2 from /test1 to /test2
[END] Copying filetest2
UNISON 2.40.63 finished propagating changes at 21:56:42.55 on 05 Apr 2012
Saving synchronizer state
Synchronization complete at 21:56:42  (1 item transferred, 0 skipped, 0 failed)
[root@localhost test1]# cd /test2/filetest
filetest1  filetest2/

远程使用unison
[root@localhost ~]# unison /test1 ssh://root@192.168.3.129//mnt
Contacting server...
Connected [//localhost.localdomain//mnt -> //localhost.localdomain//test1]
Looking for changes
  Waiting for changes from server
Reconciling changes

local          localhost...      
changed  ---->            filetest1  [f] f

Proceed with propagating updates? [] y
Propagating updates


UNISON 2.40.63 started propagating changes at 22:08:56.33 on 05 Apr 2012
[BGN] Updating file filetest1 from /test1 to //localhost.localdomain//mnt
[END] Updating file filetest1
UNISON 2.40.63 finished propagating changes at 22:08:56.34 on 05 Apr 2012


Saving synchronizer state
Synchronization complete at 22:08:56  (1 item transferred, 0 skipped, 0 failed)

通过配置文件来使用unison
cat default.prf
# Unison preferences file
root = /test1
root = ssh://root@192.168.3.129//mnt
#force = /test1
path = test11
path = test22
ignore = Path test33/
batch = true
maxthreads = 300
owner = true
group = true
perms = -1
fastcheck=false
rsync =false
sshargs = -C
xferbycopying = true
confirmbigdel = false
log = true
logfile = /root/.unison/test.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 //同步修改时间
-path xxx 参数 //只同步 -path 参数指定的子目录以及文件,而非整个目录,-path 可以多次出现。