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


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
bkjia:linux  ##“bkjia”为虚拟用户用户名;“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 = bkjia  ##指定虚拟用户名
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" > bkjia.txt
[root@inotify-master ~]# rsync -avz bkjia.txt lweim@172.18.42.200::rsync --password-file=/etc/rsync.password
sending incremental file list
bkjia.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 bkjia.txt
[root@inotify-slave ~]# cat /mydata/bkjia.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/bkjia    ##本机监控的目录,可随意定义
dst=rsync      ##inotify-slave的rsync用户的模块目录
user=bkjia      ##虚拟用户的用户名
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/bkjia/
+ dst=rsync
+ user=bkjia 
+ passfile=/etc/rsync.password
+ inotify_home=/usr/local/inotify
+ '[' '!' -e /web/bkjia/ ']'
+ '[' '!' -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/bkjia/

七、在inotify-master本机监控的目录下创建文件
[root@inotify-master bkjia]# pwd
/www/bkjia
[root@inotify-master bkjia]# touch a aa aaa aaaa
[root@inotify-master bkjia]# 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实现文件备份同步详解   

利用inotifywait监控主机文件和目录

利用inotify+rsync实现Linux文件批量更新

inotify-tools+rsync实时同步文件安装和配置

rsync同步完整配置

CentOS 6.5下Rsync远程同步

Linux文件夹对比并提取的差分文件技巧-rsync的妙用

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

本文永久更新链接地址

相关内容