实时同步rsync+inotify

环境
操作系统:CentOS 7u3
源服务器:192.168.179.128
目标服务器:192.168.179.129
目的:把源服务器上/a目录实时同步到目标服务器的/a下
注意:配置相关端口和安全规则。源服务器去同步目标服务器上的内容
192.168.179.128
一、 安装rsync
rhel7:

yum install rsync
systemctl start rsyncd

rhel6:

yum install rsync xinetd
vi /etc/xinetd.d/rsync
disable = no

/etc/init.d/xinetd start

注意:6里面 rsync依据超级守护进程启动
二、创建rsyncd.conf配置文件
vim /etc/rsyncd.conf #7上有默认配置文件,rhel6上没有默认文件要自己创建配置文件,添加以下代码

log file = /var/log/rsyncd.log #遇到错误可以查看日志,很详细
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file = /etc/rsyncd.Motd #rsync启动时欢迎信息页面文件位置
[home_test] #同步模块名称,自定义(不要太短了)
path = /home_test #rsync服务端数据目录路径
comment = home_test #模块名称,和[host_test]一样
uid = root 需要两服务器目录和文件的属主属组
gid = root 需要两服务器目录和文件的属主属组
port=873 rsync 默认端口
use chroot = no
read only = no
list = no #不显示rsync服务端资源列表
max connections = 200
timeout = 600
auth users = user1 #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.245.147 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.245.150 不允许同步的ip地址,用逗号隔开

三、创建用户认证文件
vim /etc/rsync.pass

user1:123456 #格式,用户名:密码,可以设置多个,每行一个用户名:密码

四、设置文件权限

chmod 600 /etc/rsyncd.conf #设置文件所有者读取、写入权限
chmod 600 /etc/rsync.pass #设置文件所有者读取、写入权限
重启rsync

192.168.179.129
五、安装rsync

 yum install rsync xinetd

六、创建密码文件
vim /etc/passwd.txt

123456 #密码
注意:这里的密码和客户端的密码是一样的
    chmod 600 /etc/passwd.txt #设置文件权限,只设置文件所有者具有读取、写入权限

配置完成
七、测试同步
创建好测试目录后
rsync -avH --port=873 --progress --delete /home_test user1@192.168.179.128::home_test --password-file=/etc/passwd.txt
/home_test 是指服务端数据目录
user1 是客户端设置好的账号
home_test 是指客户端设置的模块名称
同步失败查看文后的错误解决方法
八、安装Inotify-tools工具,实时触发rsync进行同步
1、查看服务器内核是否支持inotify (下面输出说明支持)
ll /proc/sys/fs/inotify
-rw-r–r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r–r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r–r-- 1 root root 0 Mar 7 02:17 max_user_watches
2、安装inotify-tools
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify && make && make install
3、设置系统环境变量,添加软连接
echo “PATH=/usr/local/inotify/bin:$PATH” >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh
echo “/usr/local/inotify/lib” >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
ln -s /usr/local/inotify/lib/libnotifytools.so.0 /usr/lib64
4、修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances = 128
5、修改参数:
sysctl -w fs.inotify.max_queued_events=“99999999”
sysctl -w fs.inotify.max_user_watches=“99999999”
sysctl -w fs.inotify.max_user_instances=“65535”

或者  vi /etc/sysctl.conf   #添加以下代码

fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
参数说明
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home_test -type d | wc -l 统计,必须保证max_user_watches值大于统
计结果(这里/home_test为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值
6、创建脚本,实时触发rsync进行同步
vim /usr/local/inotify/rsync.sh

 #!/bin/bash
    srcdir="/home_test"
    dstdir="home_test"
    excludedir="/usr/local/inotify/exclude.list"
    rsyncuser="user1"
    rsyncpassdir="/etc/passwd.txt"
    dstip="192.168.245.148"
       for ip in $dstip
        do
        rsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
        done
        
        /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir | while read file
        do
        for ip in $dstip
        do
        rsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
        echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
        done
        done
    
修改权限
    chmod +x /usr/local/inotify/rsync.sh 
脚本参数说明:
    excludedir=/usr/local/inotify/exclude.list   
    不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;
7、设置脚本开机自动执行
vim /etc/rc.d/rc.local #编辑,在最后添加一行
sh /usr/local/inotify/rsync.sh & #设置开机自动在后台运行脚本
chmod +x /etc/rc.d/rc.local
systemctl enable rc-local
inotify参数
-m 是保持一直监听
-r 是递归查看目录
-q 是打印出事件
-e create,move,delete,modify,attrib 是指 “监听 创建 移动 删除 写入 权限” 事件

============================================
rsync参数
-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用–suffix选项来指定不同的备份文件前缀。
–backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-L, --copy-links 想对待常规文件一样处理软链结
–copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
–safe-links 忽略指向SRC路径目录树以外的链结
-H, --hard-links 保留硬链结
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, --dry-run现实哪些文件将被传输
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
–rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
–existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
–delete 删除那些DST中SRC没有的文件
–delete-excluded 同样删除接收端那些被该选项指定排除的文件
–delete-after 传输结束以后再删除
–ignore-errors 及时出现IO错误也进行删除
–max-delete=NUM 最多删除NUM个文件
–partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
–force 强制删除目录,即使不为空
–numeric-ids 不将数字的用户和组ID匹配为用户名和组名
–timeout=TIME IP超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
–size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
–modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR 在DIR中创建临时文件
–compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 --partial
–progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
–exclude=PATTERN 指定排除不需要传输的文件模式
–include=PATTERN 指定不排除而需要传输的文件模式
错误排查
问题一:
@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 错误与解决方法整理补充篇

由于我们经常使用rsync进行服务器文件的同步工作,但在配置过程中,会出现很多问题,下面的错误基本上都是通过客户端返回的错误进行分析。

  另外查找错误过程中也学到了一个查看错误的方法-查看系统日志,我们操作的每步都会在日志上有说明,有时候报错看不懂,日志上都写的清清楚楚。

  命令:cat /var/log/messages

问题一:

@ERROR: auth failed on module bachup
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:服务器端该模块(backup)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。 提供正确的用户名密码解决此问题。
这个问题比较蠢,如果可能你和我一样明明在模块里写了密码文件,怎么都不通,这个时候我劝你先检查一下所写模块中是不是和下图一样。比如,某处多了个s。

问题二:110错误 -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端口) 或者直接关闭防火墙。
在这里博主是重启过服务器,然后就报110了,直接开启rsync服务就行。

命令:service rsyncd start

问题三:113错误 -防火墙
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]
原因:
防火墙问题导致,这个很简单,关闭防火墙就可以。不过博主还是被困在这里几个小时,因为linux防火墙分两种:

-iptable和-firewall
认识到这个问题首先查看系统的防火墙种类,查看防火墙状态即可
service iptable status
firewall-cmd --state

确定防火墙种类后再按对应方法将防火墙关闭即可。

问题四:1516错误

@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Receiver=3.0.9]

解决:这个问题错误差别很大,可查看系统日志查看错误,再搜索解决方法。

注意:一般这种情况都建立在可以连通的基础。去服务端查看系统日志,可以查看相关错误。

命令:cat /var/log/massages

cat /var/log/rsyncd.log

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值