Unison做数据双向同步

介绍:
由于使用rsync+inotify做单向同步很出色,但是双向同步却很不理想,所以使用unison来做数据的双向同步。Unison是一款跨windows/linux/MAC OS平台的文件同步工具,不仅支持本地对本地同步,也支持通过SSH、RSH和Socket等网络协议进行同步。更棒的是,Unison支持双向同步操作,你既可以从A同步到B,也可以从B同步到A,这些都不需要额外的设定。

环境:
系统:CentOS 5.4
ocaml 3.09.3
unison 2.40.63
A主机10.10.11.85  B主机10.10.11.92 
只需要在其中一台主机安装unison server端即可,这里以A主机为例
1.ocaml下载
wget
2.unison下载最新稳定版
wget ~bcpierce/unison//download/releases/stable/unison-2.40.63.tar.gz
3.Ocaml安装脚本如下:
tar -zxf ocaml-3.09.3.tar.gz
cd ocaml-3.09.3
./configure
make world opt
make install
cd ..
4.unison安装脚本如下:
tar -zxf unison-2.40.63.tar.gz
cd unison-2.40.63
make UISTYLE=text
make install
cp unison /usr/local/bin
scp unison  10.10.11.92:/usr/local/bin/
5.配置双机ssh信任
A主机:
ssh-keygen -t rsa
cd .ssh/
scp id_rsa.pub 10.10.11.92:/root/
B主机--10.10.11.92:
cat id_rsa.pub >>~/.ssh/authorized_keys

B主机:
ssh-keygen -t rsa
cd .ssh/
scp id_rsa.pub 10.10.11.85:/root/
A主机--10.10.11.85:
cat id_rsa.pub >>~/.ssh/authorized_keys

6.通过配置文件来使用unison
使用root安装unison后,配置文件默认生成在/root/.unison/default.prf,可以手动写一个配置文件,运行unison时只需指定此配置文件即可。 【Linux公社 】
下面以同步两个不同主机的/mnt目录为例的配置信息:
只在A主机中配置:
# more /root/.unison/unison_test.prf
root = /mnt
root = ssh://root@10.10.11.92//mnt
#force =/mnt
path = mnt
ignore = Path tmp
#prefer = ssh://root@10.10.11.92//mnt
batch = true
maxthreads = 180
#repeat = 1
#retry = 3
owner = true
group = true
perms = -1
fastcheck=false
rsync =false
#debug=verbose
sshargs = -C
xferbycopying = true
confirmbigdel = false
log = true
logfile = /root/.unison/unison_test.log

7.在A主机上做计划任务,进行每两分钟自动同步
*/2 * * * * /usr/local/bin unison unison_test.prf  >/dev/null 2>&1 &

注各参数详解:
——root表示需要同步的目录
——force表示使用unison单项同步功能,注释掉以便启用双向同步.
——ignore = Path表示同步/mnt目录时不同步tmp。
——batch = true,表示全自动模式,接受缺省动作
——fastcheck  true,表示同步时使用文件的创建时间来比较两地文件,如果这个选项为false,unison则将比较两地文件的内容.建议设置为true
——log = true
——logfile则指定了同时将输出写入log文件。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/134a4596b21c53b6fea1957dc9bc7b07.html