北京SEO

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

2019/10/10/17:46:56  阅读:1855  来源:谷歌SEO算法  标签: SEO培训

文章以一个例子来给各位介绍关于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. tarxvfocaml-3.10.2.tar.gz
  2. cdocaml-3.10.2
  3. ./configure
  4. makeworldopt
  5. makeinstall
  6. cd..

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

  1. tarxvfunison-2.32.52.tar.gz
  2. cdunison-2.32.52
  3. makeUISTYLE=textTHREADS=trueSTATIC=true
  4. cpunison/usr/local/bin
  5. cd..

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

  1. tarxvfinotify-tools-3.14.tar.gz
  2. cdinotify-tools-3.14
  3. ./configure
  4. make
  5. makeinstall
  6. cd..

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

  1. mkdir~/.ssh
  2. chmod700~/.ssh

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

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

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

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

  1. ssh-p2222192.168.1.6date
  2. ssh-p2222192.168.1.7date

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

  1. mkdir/script
  2. vim/script/inotify.sh
  3. #/bin/bash
  4. UNISON=`ps-ef|grep-vgrep|grep-cinotifywait`
  5. if[${UNISON}-lt1]
  6. then
  7. ip2="192.168.1.7"
  8. src2="/var/www/"
  9. dst2="/var/www/"
  10. /usr/local/bin/inotifywait-mrq-ecreate,delete,modify,move$src2|whilereadline--phpfensi.com
  11. do
  12. /usr/local/bin/unison-batch$src2ssh://$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-vgrep|grep-cinotifywait`
  5. if[${UNISON}-lt1]
  6. then
  7. ip2="192.168.1.6"
  8. src2="/var/www/"
  9. dst2="/var/www/"
  10. /usr/local/bin/inotifywait-mrq-ecreate,delete,modify,move$src2|whilereadline
  11. do
  12. /usr/local/bin/unison-batch$src2ssh://$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/null2>&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–gapacheunison
  2. passwdunison
  3. --输入新密码:
  4. chown–Runison./var/www
  5. mkdir/home/unison/.ssh
  6. chmod700/home/unison/.ssh
  7. su–unison
  8. ssh-keygen-trsa

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

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

在192.168.1.7服务器B上操作:

chmod 600/home/unison/.ssh/authorized_keys

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

  1. ssh-p2222unison@192.168.1.6date
  2. ssh-p2222unison@192.168.1.7date
  3. su-root

A脚本,代码如下:

  1. #/bin/bash
  2. UNISON=`ps-ef|grep-vgrep|grep-cinotifywait`
  3. if[${UNISON}-lt1]
  4. then
  5. ip2="unison@192.168.1.7:2222"
  6. src2="/var/web1/"
  7. dst2="/var/web2/"
  8. /usr/local/bin/inotifywait-mrq-ecreate,delete,modify,move$src2|whilereadline
  9. do
  10. /usr/local/bin/unison-batch-sshargs"-i/home/unison/.ssh/id_rsa"$src2ssh://$ip2--phpfensi.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-vgrep|grep-cinotifywait`
  3. if[${UNISON}-lt1]
  4. then
  5. ip2="unison@192.168.1.6:2222"
  6. src2="/var/web2/"
  7. dst2="/var/web1/"
  8. /usr/local/bin/inotifywait-mrq-ecreate,delete,modify,move$src2|whilereadline
  9. do
  10. /usr/local/bin/unison-batch-sshargs"-i/home/unison/.ssh/id_rsa"$src2ssh://$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

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

广告内容

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

相关阅读

热门评论

sunshine技术博客 sunshine技术博客

sunshine技术博客

总篇数164

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法