unison配置

来源:互联网 发布:绘声绘色软件怎么使用 编辑:程序博客网 时间:2024/06/04 20:03

在部署双机热备的情况下,要保证两台热备机器上特定目录的文件一致,需要使用文件夹双向同步功能,unison和inotify配合使用,可以实现此效果。

操作系统 CentOS6.4 64位
两台机器地址分别为192.168.2.20 和 192.168.2.71

1、下载unison安装包
http://www.seas.upenn.edu/~bcpierce/unison//download.html

2、下载unison编译器
http://caml.inria.fr/pub/distrib/ocaml-4.02/

3、安装编译器
./configure
make world opt
make install

4、安装unison
make UISTYLE=text
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目录。
cp unison /usr/local/bin

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

在192.168.2.20上执行以下命令
[unison@localhost ~]$ ssh-keygen -t rsa
在提示保存私钥(key)和公钥(public key)的位置时,使用默认值;
在提示是否需要私钥密码(passphrase)时,直接敲回车,即不使用私钥密码。
之后,将生成一对密钥,id_rsa(私钥文件)和id_rsa.pub(公钥文件),保存在/home/unison/.ssh/目录下。

[unison@localhost ~]$ scp .ssh/id_rsa.pub unison@192.168.2.71:/home/unison/ 将公钥拷贝到另外一台机器上

然后在另外一台192.168.2.71机器上执行以下命令
[unison@localhost ~]$ mv id_rsa.pub .ssh/authorized_keys
以root用户重启sshd
[root@localhost ~]# /etc/init.d/sshd restart

192.168.2.20上测试是否配置成功
[unison@localhost ~]$ ssh 192.168.2.71 date 如果不用输入密码直接登录,就说明配置成功

用同样的方式,在192.168.2.71上完成配置

6、修改unison配置
[unison@localhost ~]$ vi .unison/default.prf

Unison preferences file

#
#
root=/home/unison/test
root=ssh://unison@192.168.2.71//home/unison/test
batch=true
fastcheck=true
logfile=/home/unison/.unison/unison.log
repeat=1 #一秒执行一次检查

owner=true #保持同步过来的文件属主

group=true #保持同步过来的文件组信息

注意,owner和group为true的情况下,非unison用户创建的文件同步失败,因为同步过了之后,执行chown会提示无权限
如果要保持属主和组一致,就要用root账户来执行同步

7、设置成开机启动
以root用户将 su - unison -c unison 写入rc.local

这时,通过repeat来重复检查,如果目录下文件太多的话,会导致unison资源占用太多,
设置repeat=0,使用inotify配合使用,可以在文件发生增删改的情况下,启动unison,执行一次同步

8、inotify安装
下载源码 https://sourceforge.net/projects/inotify-tools/files/inotify-tools/
[root@localhost unison]# tar xfv inotify-tools-3.13.tar.gz
[root@localhost inotify-tools-3.13]# ./configure
[root@localhost inotify-tools-3.13]# make
[root@localhost inotify-tools-3.13]# make install

9、编写inotify执行脚本
[root@localhost unison]# vi notify.sh

LOG=/home/unison/inotify.log
inotifywait -mrq -e create,delete,modify,move /home/unison/test | while read line; do
unison
echo -n “line">>LOG
echo date | cut -d " " -f1-4 >> $LOG
done

此处inotifywait -mrq -e create,delete,modify,move表示要监测create,delete,modify,move四类文件操作,可以根据需要配置监测的操作
注意,首先要将unison的配置文件里面的repeat设置为0或者注释掉,这样unison只执行一次就退出了。

10、将inotify脚本添加到开机启动
nohup su - unison -c /home/unison/notify.sh &

原创粉丝点击