阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

rsync+inotify实现数据同步及常见问题

182次阅读
没有评论

共计 9417 个字符,预计需要花费 24 分钟才能阅读完成。

rsync:Remote Sync,是类 Unix 系统下的数据镜像备份工具。通过 rsync 可以解决对实时性要求不高的数据进行备份需求;例如:指定的备份文件服务器数据到指定的远端服务器,对本地磁盘定期做数据镜像等。

inotify:inotify 是一种文件变化通知机制;通过 inotify 可以在监控文件系统中添加、删除、修改、移动等各种操作。

准备环境:

主服务器(inotify-Master)IP:172.18.42.201
从服务器(inotify-Slave)IP:172.18.42.200

一、部署 inotify-Slave
1、安装 rsync 程序
[root@inotify-slave ~]# yum install rsync

2、创建 rsync 用户
[root@inotify-slave ~]# useradd -s /bin/nologin -M rsync

3、创建 rsync 工作模块的目录
[root@inotify-slave ~]# mkdir /mydata/  ## 创建 rsync 工作模式的模块目录
[root@inotify-slave ~]# chown rsync.rsync /mydata/  ## 更改属主、主组;使 rsync 用户有权限更改数据
[root@inotify-slave ~]# ls -ld /mydata/
drwxr-xr-x 2 rsync rsync 6 May 19 21:02 /mydata/

4、配置虚拟用户的密码文件 /etc/rsync.password
[root@inotify-slave ~]# vim /etc/rsync.password
linuxidc:linux  ##“linuxidc”为虚拟用户用户名;“linux”为虚拟用户密码
[root@inotify-slave ~]# chmod 600 /etc/rsync.password    ## 为密码文件增加安全性
[root@inotify-slave ~]# ll /etc/rsync.password 
-rw——- 1 root root 12 May 18 21:54 /etc/rsync.password

5、配置 rsync 配置文件 /etc/rsyncd.conf
[root@inotify-slave ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
user chroot = no
max connections = 200
timeout = 300
read only = no
[rsync]    ## 定义模块名,名字可随意
path = /mydata  ## 指定 rsync 用户的模块目录
auth users = linuxidc  ## 指定虚拟用户名
secrets file = /etc/rsync.password  ## 指定虚拟用户的密码文件路径
ignore errors  ## 表示忽略错误

6、启动 rsync 服务
[root@inotify-slave ~]# rsync –daemon –config=/etc/rsyncd.conf    ##rsync 监听在 tcp 协议的 873 端口
[root@inotify-slave ~]# ps aux | grep rsync
root      1330  0.0  0.0 114640  328 ?        Ss  21:13  0:00 rsync –daemon –config=/etc/rsyncd.conf
root      1338  0.0  0.1 112644  952 pts/0    R+  21:13  0:00 grep –color=auto rsync

二、配置 inotify-master 的密码文件
1、配置虚拟用户的密码文件
[root@inotify-master ~]# echo “linux” > /etc/rsync.password   
[root@inotify-master ~]# cat /etc/rsync.password
linux    ## 注意:只需要写出虚拟用户的密码;不用写出用户名
[root@inotify-master ~]# chmod 600 /etc/rsync.password   
[root@inotify-master ~]# ls -ld /etc/rsync.password
-rw——- 1 root root 6 May 19 21:18 /etc/rsync.password

三、通过 inotify-master 测试推送文件
[root@inotify-master ~]# echo “Hello Word” > linuxidc.txt
[root@inotify-master ~]# rsync -avz linuxidc.txt lweim@172.18.42.200::rsync –password-file=/etc/rsync.password
sending incremental file list
linuxidc.txt
 
sent 81 bytes  received 27 bytes  216.00 bytes/sec
total size is 11  speedup is 0.10

四、检查 inotify-slave 的工作模块目录
[root@inotify-slave ~]# ll /mydata/
total 4
-rw-r–r– 1 rsync rsync 11 May 19 21:21 linuxidc.txt
[root@inotify-slave ~]# cat /mydata/linuxidc.txt 
Hello Word    ## 推送成功

五、部署 rsync-master
1、通过 inofity 源码包编译安装
[root@inotify-master ~]# tar -xf inotify-tools-3.13.tar
[root@inotify-master ~]# cd inotify-tools-3.13/
[root@inotify-master inotify-tools-3.13]# ./ configure –prefix=/usr/local/inotify  ## 指明安装路径
[root@inotify-master ~]# make -j 4 && make install

2、编写监控脚本
12345678910111213141516171819202122 [root@inotify-master ~]# vim inotify.sh
#!/bin/bash
 
host=172.18.42.200  ## 指明 inotify-slave 的 ip 地址
src=/www/linuxidc    ## 本机监控的目录,可随意定义
dst=rsync      ##inotify-slave 的 rsync 用户的模块目录
user=linuxidc      ## 虚拟用户的用户名
passfile=/etc/rsync.password  ## 调用本地的密码文件
inotify_home=/usr/local/inotify  ## 指明 inotify 的安装目录
 
if [! -e “$src”] || [! -e “${inotify_home}/bin/inotifywait” ] || [! -e “/usr/bin/rsync”] || [! -e “/etc/rsync.password”]; then
#if  [! -e “${inotify_home}/bin/inotifywait” ] || [! -e “/usr/bin/rsync”] || [! -e “/etc/rsync.password”]; then
echo “Check File and Folder”
exit 1
fi
 
 
${inotify_home}/bin/inotifywait -mrq -e close_write,delete,create,attrib $src | while read file
do
cd $src && rsync -arvz -P ./ –timeout=100 $user@$host::$dst –password-file=$passfile &>/dev/null
done
exit 0

六、执行 inotify-master 上的 inotify.sh 脚本
[root@inotify-master ~]# bash -x inotify.sh  ## 运行脚本
+ host=172.18.42.200
+ src=/web/linuxidc/
+ dst=rsync
+ user=linuxidc 
+ passfile=/etc/rsync.password
+ inotify_home=/usr/local/inotify
+ ‘[‘ ‘!’ -e /web/linuxidc/ ‘]’
+ ‘[‘ ‘!’ -e /usr/local/inotify/bin/inotifywait ‘]’
+ ‘[‘ ‘!’ -e /usr/bin/rsync ‘]’
+ ‘[‘ ‘!’ -e /etc/rsync.password ‘]’
+ read file
+ /usr/local/inotify/bin/inotifywait -mrq -e close_write,delete,create,attrib /web/linuxidc/

七、在 inotify-master 本机监控的目录下创建文件
[root@inotify-master linuxidc]# pwd
/www/linuxidc
[root@inotify-master linuxidc]# touch a aa aaa aaaa
[root@inotify-master linuxidc]# ll
total 0
-rw-r–r– 1 root root 0 May 19 22:54 a
-rw-r–r– 1 root root 0 May 19 22:54 aa
-rw-r–r– 1 root root 0 May 19 22:54 aaa
-rw-r–r– 1 root root 0 May 19 22:54 aaaa

八、在 inotify-slave 的 rsync 工作模块目录下查看
[root@inotify-slave mydata]# ll
total 0
-rw-r–r– 1 rsync rsync 0 May 19 22:54 a
-rw-r–r– 1 rsync rsync 0 May 19 22:54 aa
-rw-r–r– 1 rsync rsync 0 May 19 22:54 aaa
-rw-r–r– 1 rsync rsync 0 May 19 22:54 aaaa
[root@inotify-slave mydata]# pwd
/mydata

以下是为配置 rsync 时的常见问题:

问题一:
@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]

原因:
服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。

问题 1:
在 client 上遇到问题:
rsync -auzv –progress –password-file=/etc/rsync.pas root@192.168.133.128::backup /home/
rsync: could not open password file “/etc/rsync.pas”: No such file or directory (2)
Password:
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到这个问题:client 端没有设置 /etc/rsync.pas 这个文件,而在使用 rsync 命令的时候,加了这个参数 –
password-file=/etc/rsync.pas

问题 2:
rsync -auzv –progress –password-file=/etc/rsync.pas root@192.168.133.128::backup /home/
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到这个问题:client 端已经设置 /etc/rsync.pas 这个文件,里面也设置了密码 111111,和服务器一致,但是
服务器段设置有错误,服务器端应该设置 /etc/rsync.pas,里面内容 root:111111 , 这里登陆名不可缺少

问题 3:
rsync -auzv –progress –password-file=/etc/rsync.pas root@192.168.133.128::backup /home/
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到这个问题,是因为服务器端的 /home/backup 其中 backup 这个目录并没有设置,所以提示:chdir failed

问题 4:
rsync: write failed on “/home/backup2010/wensong”: No space left on device (28)
rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7]
rsync: connection unexpectedly closed (2721 bytes received so far) [generator]
rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7]
磁盘空间不够,所以无法操作。
可以通过 df /home/backup2010 来查看可用空间和已用空间

问题 5:网络收集问题
1、权限问题
类似如下的提示:rsync: opendir “/kexue” (in dtsChannel) failed: Permission denied (13)注意查看同步的目录权限是否为 755
2、time out
rsync: failed to connect to 203.100.192.66: Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
检查服务器的端口 netstat –tunlp,远程 telnet 测试。
可能因为客户端或者服务端的防火墙开启 导致无法通信,可以设置规则放行 rsync(873 端口)或者直接关闭防火墙。

还有一种在同步过程中可能会提示没有权限(将同步目录加上 SvcwRsync 全部权限即可,更简单的方法就是将 SvcwRsync 设为管理员即可)

3、服务未启动
rsync: failed to connect to 10.10.10.170: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
启动服务:rsync –daemon –config=/etc/rsyncd.conf
4、磁盘空间满
rsync: recv_generator: mkdir “/teacherclubBackup/rsync……” failed: No space left on device (28)
*** Skipping any contents from this failed directory ***
5、Ctrl+ C 或者大量文件
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [receiver=3.0.5]
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [generator=3.0.5]
说明:导致此问题多半是服务端服务没有被正常启动,��服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后 shutdown -r now 服务器

6、xnetid 启动
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5]
查看 rsync 日志
rsync: unable to open configuration file “/etc/rsyncd.conf”: No such file or directory
xnetid 查找的配置文件位置默认是 /etc 下,根据具体情况创建软链接。例如:
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
或者更改指定默认的配置文件路径,在 /etc/xinetd.d/rsync 配置文件中。
Rsync configure:
配置一:
ignore errors
说明:这个选项最好加上,否则再很多 crontab 的时候往往发生错误你也未可知,因为你不可能天天去看每时每刻去看 log,不加上这个出现错误的几率相对会很高,因为任何大点的项目和系统,磁盘 IO 都是一个瓶颈

Rsync error:
错误一:
@ERROR: auth failed on module xxxxx
rsync: connection unexpectedly closed (90 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
说明:这是因为密码设置错了,无法登入成功,检查一下 rsync.pwd,看客服是否匹配。还有服务器端没启动 rsync 服务也会出现这种情况。
错误二:
password file must not be other-accessible
continuing without password file
Password:
说明:这是因为 rsyncd.pwd rsyncd.sec 的权限不对,应该设置为 600。如:chmod 600 rsyncd.pwd
错误三:
@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
说明:这是因为你在 rsync.conf 中设置的 path 路径不存在,要新建目录才能开启同步
错误四:
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]
说明:防火墙问题导致,这个最好先彻底关闭防火墙,排错的基本法就是这样,无论是 S 还是 C,还有 ignore errors 选项问题也会导致

错误五:
@ERROR: access denied to www from unknown (192.168.1.123)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
说明:此问题很明显,是配置选项 host allow 的问题,初学者喜欢一个允许段做成一个配置,然后模块又是同一个,致使导致
错误六:
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]
rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后 shutdown -r now 服务器
错误七:
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
说明:原数据目录里没有数据存在

RSync 实现文件备份同步详解   http://www.linuxidc.com/Linux/2014-09/106967.htm

利用 inotifywait 监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用 inotify+rsync 实现 Linux 文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync 实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

rsync 同步完整配置 http://www.linuxidc.com/Linux/2013-06/85781.htm

CentOS 6.5 下 Rsync 远程同步 http://www.linuxidc.com/Linux/2014-05/101084.htm

Linux 文件夹对比并提取的差分文件技巧 -rsync 的妙用 http://www.linuxidc.com/Linux/2016-02/128307.htm

Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-08/134027.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计9417字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中