rysnc和intotify实现数据同步

rsync+intotify实现数据同步
一、rsync概述
rsync与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
    随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数 据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少 的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一 定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了。




二、    inotify 概述
2.1、初识inotify
Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统 中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样 的一个第三方软件。
   在上面章节中,我们讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
   
   
在WEB内容发布服务器安装inotify.  
 2.2、inotify安装
# tar –xvf inotify-tools-3.14.tar.gz
# ./configure
# make && make install




# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 44319 4月   6 18:51 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 41409 4月   6 18:51 /usr/local/bin/inotifywatch
inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树
inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息


  
  








三、    企业案例WEB内容版本发布管理
3.1、环境描述
随着企业的业务增多,为了维持所有WEB层的WEB程序保持一致。发版本的通过一台内容服务器向外进行分布和辐射所有的更新的WEB程序,模拟环境如下:
WEB1 节点 IP:10.0.0.202
WEB2节点内容发布Server IP:10.0.0.201


3.2、安装WEB1节点
# yum –y install rsync
# vim /etc/web1.pass
gongda:123
# chmod 600 /etc/web1.pass


#: yum –y install rsync
# mkdir /web1/wwwroot –p
# vim /etc/rsyncd.conf   #此配置文件没有。需要重新编辑一个
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web1]
path = /web1/wwwroot/
comment = web1 file
ignore errors
read only = no
write only = no
hosts allow = 10.0.0.201
hosts deny = *
list = false
uid = root
gid = root
auth users = web1user
secrets file = /etc/web1.pass


启动rsync服务
# rsync --daemon








3.2、安装WEB2内容管理节点
# yum –y install rsync




在WEB内容发布服务器安装inotify.  
(1)、inotify安装
# tar –xvf inotify-tools-3.14.tar.gz
# ./configure
# make && make install


# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 44319 4月   6 18:51 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 41409 4月   6 18:51 /usr/local/bin/inotifywatch
inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树
inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息




(2)同步的密码,注意此处只需要写密码,和web1.pass里面的密码要以致
# vim /etc/server.pass
123
#chmod 600 /etc/server.pass


(3)编写一个启动脚本
# vim opt/inotify-rsync.sh 
#!/bin/bash
host1=10.0.0.202
#======================
src=/web/wwwroot/
dst1=web1
user1=gongda
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib  $src \
| while read files
        do
  /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1
                echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
         done


(4)启动脚本
#cd /opt
#chmod +x inotify-rsync.sh
# nohup bash –x inotify-rsync.sh &




3.2、常见错误
问题一:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:服务器端的目录不存在或无权限。创建目录并修正权限可解决问题。
 
问题二:
@ERROR: auth failed on module tee
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。提供正确的用户名密码解决此问题。
 
问题三:
@ERROR: Unknown module ‘tee_nonexists’
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。
 
问题四:
password file must not be other-accessible
continuing without password file
Password:
原因:这是因为rsyncd.pwd rsyncd.secrets的权限不对,应该设置为600。如:chmod 600 rsyncd.pwd
 
问题五:
rsync: failed to connect to 218.107.243.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
原因:对方没开机、防火墙阻挡、通过的网络上有防火墙阻挡,都有可能。关闭防火墙,其实就是把tcp udp的873端口打开。
问题六:
rsync error: error starting client-server protocol (code 5) at main.c(1524) [Receiver=3.0.7]
原因:/etc/rsyncd.conf配置文件内容有错误。请正确核对配置文件。
 
问题七:
rsync: chown "" failed: Invalid argument (22)
原因:权限无法复制。去掉同步权限的参数即可。(这种情况多见于Linux向Windows的时候)
问题八:
@ERROR: daemon security issue -- contact admin
rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6]
原因:同步的目录里面有软连接文件,需要服务器端的/etc/rsyncd.conf打开use chroot = yes。掠过软连接文件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值