PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 服务器 > Linux教程 > 正文

    linux中inotify+unison实现数据双向同步备份

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:文章以一个例子来给各位介绍关于linux中inotify+unison实现数据双向同步备份配置 方法,希望此例子能帮助各位深入的理解inotify+unison配置...

    文章以一个例子来给各位介绍关于linux中inotify+unison实现数据双向同步备份配置 方法,希望此例子能帮助各位深入的理解inotify+unison配置.

    服务器分别为:

    服务器A:192.168.1.6,同步目录:/var/www

    服务器B:192.168.1.7,同步目录:/var/www

    安装unison:

    首先安装ocaml,版本至少为3.07或更高.

    下载地址:http://caml.inria.fr.代码如下:

    1. tar xvf ocaml-3.10.2.tar.gz 
    2. cd ocaml-3.10.2 
    3. ./configure 
    4. make world opt 
    5. make install 
    6. cd .. 

    安装unison:下载地址:www.seas.upenn.edu/~bcpierce/unison/,代码如下:

    1. tar xvf unison-2.32.52.tar.gz 
    2. cd unison-2.32.52 
    3. make UISTYLE=text THREADS=true STATIC=true 
    4. cp unison /usr/local/bin 
    5. cd .. 

    注:UISTYLE=text THREADS=true STATIC=true表示使用命令行方式,加入线程支持以静态模式编译,安装inotify,下载地址:http://inotify-tools.sourceforge.net:

    1. tar xvf inotify-tools-3.14.tar.gz 
    2. cd inotify-tools-3.14 
    3. ./configure 
    4. make 
    5. make install 
    6. cd .. 

    配置双机ssh信任(除以下方法外,也可以在A中生成密钥后,把A上的.ssh目录全SCP到B服务器/root/.ssh,这样方便些),以root用户登陆,在服务器A上创建.ssh目录:

    1. mkdir ~/.ssh 
    2. chmod 700 ~/.ssh 

    生成RSA密钥:ssh-keygen -t rsa,然后连续三次回车,添加密钥到授权密钥文件中,在192.168.1.6服务器A上操作,2222是端口号,代码如下:

    1. cd ~/.ssh 
    2. ssh "-p 2222" 192.168.1.6 cat /root/.ssh/id_rsa.pub >> authorized_keys  #小写p 
    3. ssh "-p 2222" 192.168.1.7 cat /root/.ssh/id_rsa.pub >> authorized_keys 
    4. scp  -P 2222 authorized_keys 192.168.1.7:/root/.ssh/  #大写P 
    5. chmod 600 /root/.ssh/authorized_keys 

    在192.168.1.7服务器B上操作:chmod 600 /root/.ssh/authorized_keys

    分别在两台机器上执行如下测试,第一次执行时,会要求输入密码,以后执行则不需要说明信任成功,代码如下:

    1. ssh -p 2222 192.168.1.6 date 
    2. ssh -p 2222 192.168.1.7 date 

    添加脚本,在192.168.1.6服务器A上添加脚本:

    1. mkdir /script 
    2. vim /script/inotify.sh 
    3. #/bin/bash 
    4. UNISON=`ps -ef |grep -v grep|grep -c inotifywait` 
    5. if [ ${UNISON} -lt 1 ] 
    6. then 
    7. ip2="192.168.1.7" 
    8. src2="/var/www /" 
    9. dst2="/var/www/ " 
    10. /usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line  --phpfensi.com 
    11. do 
    12. /usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2 
    13. echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log 
    14. echo ` date +%F %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log 
    15. done 
    16. fi 

    在192.168.1.7服务器上添加脚本:

    1. mkdir /script 
    2. vim /script/inotify.sh 
    3. #/bin/bash 
    4. UNISON=`ps -ef |grep -v grep|grep -c inotifywait` 
    5. if [ ${UNISON} -lt 1 ] 
    6. then 
    7. ip2="192.168.1.6" 
    8. src2="/var/www /" 
    9. dst2="/var/www/ " 
    10. /usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line 
    11. do 
    12. /usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2 
    13. echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log 
    14. echo ` date +%F %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log 
    15. done 
    16. fi 

    在二台服务器上修改脚本权限:chmod a+x /script/inotify.sh,在计划任务中添加任务,原本在/etc/rc.local下添加开机启动的,但出问题,脚本并不执行,代码如下:

    1. crontab –e 
    2. #unison+inotify 
    3. * * * * * /bin/sh /script/inotify.sh > /dev/null 2>&1 & 

    重启电脑,测试二台服务器中/var/www的内容是否能同步,不重启电脑,手动执行脚本也可以测试:

    sh /script/inotify

    在其中一台/var/www目录中添加,或修改,或删除文件的时候,可以看到脚本状态,同时另一台服务器也应该会跟随操作,如果有问题,请手动修改下脚本.

    在某些公司中,是禁止禁用root远程登陆,只能使用普通用户进行同步方案,等待同步目录,二台电脑不能使用一样的待同步目录名,否则报错,代码如下:

    1. 192.168.1.6: /var/web1 
    2. 192.168.1.7: /var/web2 

    安装过程如上:

    1. useradd –g apache unison 
    2. passwd unison 
    3. --输入新密码: 
    4. chown –R unison. /var/www 
    5. mkdir /home/unison/.ssh 
    6. chmod 700 /home/unison/.ssh 
    7. su – unison 
    8. ssh-keygen -t rsa 

    然后连续三次回车,添加密钥到授权密钥文件中,在192.168.1.6服务器A上操作(2222是端口号,代码如下:

    1. cd /home/unison/.ssh 
    2. ssh "-p 2222" 192.168.1.6 cat /home/unison/.ssh/id_rsa.pub >> authorized_keys  #小写p 
    3. ssh "-p 2222" 192.168.1.7 cat /home/unison /.ssh/id_rsa.pub >> authorized_keys 
    4. scp  -P 2222 authorized_keys 192.168.1.7:/home/unison/.ssh/  #大写P 
    5. chmod 600 /home/unison/.ssh/authorized_keys 

    在192.168.1.7服务器B上操作:

    chmod 600/home/unison/.ssh/authorized_keys

    分别在两台机器上执行如下测试,第一次执行时,会要求输入密码,以后执行则不需要说明信任成功,代码如下:

    1. ssh -p 2222 unison@192.168.1.6 date 
    2. ssh -p 2222 unison@192.168.1.7 date 
    3. su - root 

    A脚本,代码如下:

    1. #/bin/bash 
    2. UNISON=`ps -ef |grep -v grep|grep -c inotifywait` 
    3. if [ ${UNISON} -lt 1 ] 
    4. then 
    5. ip2="unison@192.168.1.7:2222" 
    6. src2="/var/web1/" 
    7. dst2="/var/web2/" 
    8. /usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line 
    9. do 
    10. /usr/local/bin/unison -batch -sshargs "-i /home/unison/.ssh/id_rsa" $src2 ssh://$ip2  --www.bcty365.com
    11. /$dst2 
    12. echo -n "$line " >> /var/umelook-log/inotify/inotify$(date +%u).log 
    13. echo ` date +%F %T` >> /var/umelook-log/inotify/inotify$(date +%u).log 
    14. done 
    15. fi 

    B脚本,代码如下:

    1. #/bin/bash 
    2. UNISON=`ps -ef |grep -v grep|grep -c inotifywait` 
    3. if [ ${UNISON} -lt 1 ] 
    4. then 
    5. ip2="unison@192.168.1.6:2222" 
    6. src2="/var/web2/" 
    7. dst2="/var/web1/" 
    8. /usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line 
    9. do 
    10. /usr/local/bin/unison -batch -sshargs "-i /home/unison/.ssh/id_rsa" $src2 ssh://$ip2 
    11. /$dst2 
    12. echo -n "$line " >> /var/umelook-log/inotify/inotify$(date +%u).log 
    13. echo ` date +%F %T` >> /var/umelook-log/inotify/inotify$(date +%u).log 
    14. done 
    15. fi  

    在此我自己常用字的同步备份并不是这个而一个单向备份的工具,是网络比较常用的一个备份工具,在此我就不说了免得大家攻击我呀。

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-121-1682-1.html
    相关热词搜索: