linux使用unison+inotify实时双向同步

背景介绍:

平时根据业务需求,跑服务的某个文件夹需要两边互相备份,并且实时同步。

环境:

服务器 IP地址 系统版本
web1 192.168.1.231 CentOS 6.5
web2 192.168.1.239 CentOS 6.5

安装unison和inotify

有两种方式可以安装,源码和rpm包。此处我使用简便的rpm包安装。
每台服务器均需安装。

[root@localhost ~]# rpm -ivh unison-2.32.52-1.el6.rf.x86_64.rpm 
warning: unison-2.32.52-1.el6.rf.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY
Preparing...                ########################################### [100%]
   1:unison                 ########################################### [100%]

[root@localhost ~]# rpm -ivh inotify-tools-3.13-1.el6.rf.x86_64.rpm 
warning: inotify-tools-3.13-1.el6.rf.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY
Preparing...                ########################################### [100%]
   1:inotify-tools          ########################################### [100%]

创建同步用户并设置密码

每台服务器的用户保持一致。
可以不创建同步用户,使用现有用户。不过出于安全考虑,不建议使用root用户做同步(因为需要做SSH信任关系)。

[root@localhost ~]# useradd unison
[root@localhost ~]# passwd unison

服务器生成ssh私钥

每台服务器均需进行此操作

[root@localhost ~]# su - unison
[unison@localhost ~]$ ssh-keygen -t rsa

互相添加ssh信任关系

在web2上进行以下操作(在web1亦可,IP地址输入web2的就行)

[unison@localhost ~]$ cd .ssh/
[unison@localhost .ssh]$ cat id_rsa.pub >> authorized_keys
[unison@localhost .ssh]$ ssh 192.168.1.231 cat /home/unison/.ssh/id_rsa.pub >> authorized_keys
[unison@localhost .ssh]$ chmod 600 authorized_keys
[unison@localhost .ssh]$ scp authorized_keys 192.168.1.231:/home/unison/.ssh/

测试互相通信是否无需输入密码

第一次会提示添加进known_hosts,输入yes,之后便不会再提示

web1
[unison@localhost ~]$ ssh 192.168.1.239 date
web2
[unison@localhost ~]$ ssh 192.168.1.231 date

编写同步脚本

web1上的脚本

#/bin/sh
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
    ip2="192.168.1.239"
    src1="/home/unison/test1/"
    dst2="/home/unison/test2/"
    igdir1="Path apache-tomcat-7.0.55"
    /usr/bin/inotifywait -mrq -e create,delete,delete_self,modify,move $src1 | while read line; do
        /usr/bin/unison -batch -ignore="$igdir1" $src1 ssh://$ip2/$dst2
        echo -n "$line " >> /var/log/inotify.log
        echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
    done
fi

web2上的脚本

#/bin/sh
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
    ip1="192.168.1.231"
    src2="/home/unison/test2/"
    dst1="/home/unison/test1/"
    igdir2="Path apache-tomcat-7.0.55"
    /usr/bin/inotifywait -mrq -e create,delete,delete_self,modify,move $src2 | while read line; do
        /usr/bin/unison -batch -ignore="$igdir2" $src2 ssh://$ip1/$dst1
        echo -n "$line " >> /var/log/inotify.log
        echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
    done
fi

igdir是不要同步的文件夹,格式需要加Path空格目录名,因为跑业务的文件夹里有tomcat的文件夹,这个里面的log日志各自不同,无需同步。

加入crontab定时任务

本来想加入系统自启动的,但是加入后能启动,但是不生效。

[unison@localhost ~]# crontab -e
# web1
* * * * * nohup /home/unison/bin/web1.sh > /dev/null 2>&1 &
# web2
* * * * * nohup /home/unison/bin/web2.sh > /dev/null 2>&1 &

测试是否生效

参考文献

linux利用unison实现双向或多向实时同步

你可能感兴趣的:(linux使用unison+inotify实时双向同步)