好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

linux中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.代码如下:

  • tar xvf ocaml-3.10.2.tar.gz 
  • cd ocaml-3.10.2 
  • ./configure 
  • make world opt 
  • make install 
  • cd .. 

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

  • tar xvf unison-2.32.52.tar.gz 
  • cd unison-2.32.52 
  • make UISTYLE=text THREADS=true   STATIC = true  
  • cp unison /usr/local /bin 
  • cd .. 

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

  • tar xvf inotify-tools-3.14.tar.gz 
  • cd inotify-tools-3.14 
  • ./configure 
  • make 
  • make install 
  • cd .. 

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

  • mkdir ~/.ssh 
  • chmod 700 ~/.ssh 

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

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

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

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

  • ssh -p 2222 192.168.1.6  date  
  • ssh -p 2222 192.168.1.7 date  

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

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

    在192.168.1.7服务器上添加脚本:

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

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

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

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

    sh /script/inotify

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

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

  • 192.168.1.6: /var/web1 
  • 192.168.1.7: /var/web2 

    安装过程如上:

  • useradd –g apache unison 
  • passwd unison 
  • --输入新密码:  
  • chown –R unison. /var/www 
  • mkdir /home/unison/.ssh 
  • chmod 700 /home/unison/.ssh 
  • su – unison 
  • ssh-keygen -t rsa 

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

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

    在192.168.1.7服务器B上操作:

    chmod 600/home/unison/.ssh/authorized_keys

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

  • ssh -p 2222 unison@192.168.1.6  date  
  • ssh -p 2222 unison@192.168.1.7 date  
  • su - root 

    A脚本,代码如下:

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

    B脚本,代码如下:

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

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

     

    查看更多关于linux中inotify+unison实现数据双向同步备份的详细内容...

  •   阅读:70次