CentOS   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了CentOS6.8使用Rsync+Inotify-tools实现数据实时同步大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

说明:         操作系统:CentOS release 6.8 (Final) x86_64         服务器IP:rsync_server(数据源) 10.15.43.100                          rsync_client  (目标端)10.15.43.228        同步目录: rsync_server       /app/rsync_serv
@H_944_14@说明:

@H_944_14@操作系统:CentOS release 6.8 (Final)x86_64

@H_944_14@ 服务器IP:rsync_server(数据源) 10.15.43.100

@H_944_14@ rsync_client (目标端)10.15.43.228

@H_944_14@ 同步目录: rsync_server /app/rsync_server

@H_944_14@ rsync_client/app/rsync_client

@H_944_14@

@H_944_14@rsync_client (目标端)10.15.43.228

@H_944_14@1、安装Rsync服务端

[root@localhostsrc]#yum-yinstallrsyncxinetd
[root@localhostsrc]#cp/etc/xinetd.d/rsync{,default}
[root@localhostsrc]#vim/etc/xinetd.d/rsync
servicersync
{
disable=no#修改为no
flags=IPv6
socket_type=stream
wait=no
user=root
server=/usr/bin/rsync
server_args=--daemon
log_on_failure+=USERID
}
[root@localhostsrc]#/etc/init.d/xinetdstart#CentOS中是以xinetd来管理Rsync服务的
[root@localhostsrc]#vim/etc/rsyncd.conf#创建配置文件
logfile=/var/log/rsyncd.log#日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile=/var/run/rsyncd.pid#pid文件的存放位置
lockfile=/var/run/rsync.lock#@L_192_6@maxconnections参数的锁文件
secretsfile=/etc/rsync.pass#用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motdfile=/etc/rsyncd.Motd#rsync启动时欢迎信息页面文件位置(文件内容自定义)
[app_rsync_client]#自定义名称
path=/app/rsync_client/#rsync服务端数据目录路径
comment=app_rsync_client#模块名称与[app_rsync_client]自定义名称相同
uid=root#设置rsync运行权限为root
gid=root#设置rsync运行权限为root
port=873
usechroot=no#认为true,修改为no,增加对目录文件软连接的备份
readonly=no设置rsync服务端文件为读写权限
list=no#不显示rsync服务端资源列表
macconnections=200
timeout=600
authusers=rsync#执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hostsallow=10.15.43.100#允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hostsdeny=10.10.2.84#禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
#先允许后拒绝
[root@localhostsrc]#vim/etc/rsync.pass#配置文件添加以下内容
rsync:123456#格式,用户名:密码,可以设置多个,每行一个用户名:密码
[root@localhostsrc]#chmod600/etc/rsyncd.conf
[root@localhostsrc]#chmod600/etc/rsync.pass
[root@localhostsrc]#/etc/init.d/xinetdrestart
@H_944_14@

@H_944_14@rsync_server(数据源) 10.15.43.100

@H_944_14@安装Rsync客户端

[root@localhostrsync_server]#whereisrsync#查看系统是否已安装rsync
rsync:/usr/bin/rsync/usr/share/man/man1/rsync.1.gz#说明已经安装
[root@localhostrsync_server]#
@H_944_14@yum install xinetd #已安装rsync只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的

@H_944_14@yum install rsync xinetd #如果认没有rsync,运行此命令进行安装rsync和xinetd

[root@localhostrsync_server]#vim/etc/xinetd.d/rsync
servicersync
{
disable=no#修改为no
flags=IPv6
socket_type=stream
wait=no
user=root
server=/usr/bin/rsync
server_args=--daemon
log_on_failure+=USERID
}
[root@localhostrsync_server]#/etc/init.d/xinetdrestart
[root@localhostrsync_server]#vim/etc/passwd.txt
123456
[root@localhostrsync_server]#chmod600/etc/passwd.txt
@H_944_14@

@H_944_14@测试

@H_944_14@在rsync_server的/app/rsync_server目录下创建文件file,在rsync_server端运行同步命令同步数据:

@H_944_14@rsync -avH --port=873 --progress --delete /app/rsync_client/ rsync@10.15.43.228::app_rsync_client --password-file=/etc/passwd.txt

@H_944_14@

@H_944_14@rsync_server(数据源) 10.15.43.100

[root@localhostsrc]#mkdir/app/rsync_client/test
[root@localhostsrc]#touch/app/rsync_client/test/file
[root@localhostrsync_server]#rsync-avH--port=873--progress--delete/app/rsync_server/rsync@10.15.43.228::app_rsync_client--password-file=/etc/passwd.txt

sendingincrementalfilelist
./
file
0100%0.00kB/s0:00:00(xfer#1,to-check=0/2)

sent81bytesreceived30bytes222.00bytes/sec
@R_161_10586@lsizeis0speedupis0.00
[root@localhostrsync_server]#
@H_944_14@/app/rsync_server/数据源的目录

@H_944_14@-password-file=/etc/passwd.txt数据源的密码文件

@H_944_14@rsync@10.15.43.228::app_rsync_client rsync目标端rsync服务端配置的用户名,app_rsync_client目标端rsync服务端配置的模块名称

@H_944_14@

@H_944_14@rsync_client

[root@localhostrsync_client]#ls
file
[root@localhostrsync_client]#
@H_944_14@

@H_944_14@在rsync_server(数据源) 10.15.43.100上安装Inotify-tools工具,实时触发rsync进行同步

@H_944_14@1、安装Inotify-tools工

[root@localhostsrc]#ll/proc/sys/fs/inotify#查看服务器内核是否支持inotify,出现下面的内容,说明服务器内核支持inotify
@R_161_10586@l0
-rw-r--r--1rootroot0Jul2710:32max_queued_events
-rw-r--r--1rootroot0Jul2710:32max_user_instances
-rw-r--r--1rootroot0Jul2710:32max_user_watches
[root@localhostsrc]#uname-r#Linux下支持inotify的内核最小为2.6.13
2.6.32-642.el6.x86_64
[root@localhostsrc]#tarzxvfinotify-tools-3.14.tar.gz
[root@localhostsrc]#cdinotify-tools-3.14
[root@localhosTinotify-tools-3.14]#./configure--prefix=/app/inotify
[root@localhosTinotify-tools-3.14]#make&&makeinstall
[root@localhosTinotify-tools-3.14]#vim/etc/profile#设置系统环境变量
exportPATH=/app/inotify/bin:$PATH
[root@localhosTinotify-tools-3.14]#source/etc/profile
[root@localhosTinotify-tools-3.14]#echo"/app/inotify/lib">/etc/ld.so.conf.d/inotify.conf
[root@localhosTinotify-tools-3.14]#ln-s/app/inotify/include/usr/include/inotify
[root@localhosTinotify-tools-3.14]#sysctl-a|egrep-i"max_queued_events|max_user_watches|max_user_instances"#修改inotify认参数(inotify内核参数值太小)
fs.inotify.max_user_instances=128
fs.inotify.max_user_watches=8192
fs.inotify.max_queued_events=16384
fs.epoll.max_user_watches=201420
[root@localhosTinotify-tools-3.14]#vim/etc/sysctl.conf
fs.inotify.max_user_instances=65535
fs.inotify.max_user_watches=99999999
fs.inotify.max_queued_events=99999999
[root@localhosTinotify-tools-3.14]#cat/proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events}
65535
99999999
99999999
[root@localhosTinotify-tools-3.14]#
@H_944_14@max_queued_events:

@H_944_14@inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确

@H_944_14@max_user_watches:

@H_944_14@要同步的文件包含多少目录,可以用:find /app/rsync_server/ -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/app/rsync_server/为同步文件目录)

@H_944_14@max_user_instances:

@H_944_14@每个用户创建inotify实例最大值

@H_944_14@

@H_944_14@2、创建脚本,实时触发rsync进行同步

[root@localhosTinotify]#catrsync.sh
#!/bin/bash
src_dir="/app/rsync_server/"
dst_dir="app_rsync_client"
exclude_dir="/app/inotify/exclude.list"
rsync_user="rsync"
rsync_passwd="/etc/passwd.txt"
dst_ip="10.15.43.22810.10.2.84"
rsync_command(){
rsync-avH--port=873--progress--delete--exclude-from=$exclude_dir$src_dir$rsync_user@$ip::$dst_dir--password-file=$rsync_passwd
}
foripin$dst_ip;do
rsync_command
done
/app/inotify/bin/inotifywait-mrq--timefmt'%d/%m/%y%H:%M'--format'%T%w%f%e'-eclose_write,modify,delete,create,attrib,move$src_dir\
|whilereadfile;do
foripin$dst_ip;do
rsync_command
echo"${filE}wasrsynced">>/tmp/rsync.log2>&1
done
done
[root@localhosTinotify]#chmod+xrsync.sh
[root@localhosTinotify]#touch/app/inotify/exclude.list
[root@localhosTinotify]#vim/etc/rc.d/rc.local
nohup/bin/sh/app/inotify/rsync.sh&
[root@localhosTinotify]#nohup/bin/sh/app/inotify/rsync.sh&
@H_944_14@src_dir="/app/rsync_server/"#源服务器同步目录

@H_944_14@dst_dir="app_rsync_client"#目标服务器rsync同步目录模块名称

@H_944_14@exclude_dir="/app/inotify/exclude.list"#不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;

@H_944_14@例如:不需要同步/app/rsync_server/"目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写

@H_944_14@a/

@H_944_14@b/b1/

@H_944_14@

@H_944_14@rsync_user="rsync"#目标服务器rsync同步用户名

@H_944_14@rsync_passwd="/etc/passwd.txt"#目标服务器rsync同步用户的密码在源服务器的存放路径

@H_944_14@dst_ip="10.15.43.228 10.10.2.84"#目标服务器ip,多个ip用空格分开

@H_944_14@

@H_944_14@inotify参数

@H_944_14@-m 是保持一直监听

@H_944_14@-r 是递归查看目录

@H_944_14@-q 是打印出事件

@H_944_14@-e create,move,attrib 是指监听“创建 移动 删除 写入 权限” 事件

@H_944_14@rsync参数

@H_944_14@-v,--verbose 详细模式输出

@H_944_14@ -a,--archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD

@H_944_14@ -H,--hard-links 保留硬链结

@H_944_14@

@H_944_14@3、测试

@H_944_14@在rsync_server(数据源) 10.15.43.100的/app/rsync_server创建文件

[root@localhostrsync_server]#touchtest{1..9}
[root@localhostrsync_server]#touchtest{a..j}
[root@localhostrsync_server]#ls
test1test2test3test4test5test6test7test8test9testaTESTBTESTCtestdtestetestftestgtesthtestitestj
[root@localhostrsync_server]#pwd
/app/rsync_server
[root@localhostrsync_server]#
@H_944_14@在rsync_client (目标端)10.15.43.228、10.10.2.84上查看已经同步

[root@localhostrsync_client]#ls
test1test2test3test4test5test6test7test8test9testaTESTBTESTCtestdtestetestftestgtesthtestitestj
[root@localhostrsync_client]#pwd
/app/rsync_client
[root@localhostrsync_client]#
@H_944_14@如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。

@H_944_14@至此,Linux下Rsync+Inotify-tools实现数据实时同步完成。如果要双向同步可以把以上反过来部署次。

@H_944_14@

@H_944_14@报错:

@H_944_14@错误一:

@H_944_14@rsync error: some files/attrs were not transferred (see prevIoUs errors) (code 23) at main.c(1039) [sender=3.0.6]

@H_944_14@检查服务器端的目录(备份目录)是否存在,并检查其权限。创建目录并修正权限可解决问题。

@H_944_14@我这里这个报错是因为Selinux开启导致的,setenforce 0或者直接disabled即可

@H_944_14@

@H_944_14@错误二:

@H_944_14@rsync error: error starTing client-server protocol (code 5) at main.c(1522) [sender=3.0.5]

@H_944_14@解决办法:

@H_944_14@(1) 检查服务、客户端密码文件是否正确:服务端密码文件(这里为/etc/rsync.pass) 的格式为 用户:密码; 客户端密码文件为:密码(没有用户名)

@H_944_14@(2)检查密码文件的权限是否正确

@H_944_14@

@H_944_14@错误三:

@H_944_14@password file must not be other-accessible

@H_944_14@conTinuing without password file

@H_944_14@password:

@H_944_14@解决办法:

@H_944_14@检查服务端和客户端上的密码配置文件权限是否为600(只能为600),若不是可以通过命令 chmod 600 rsync.pass 修改即可

@H_944_14@

@H_944_14@错误四:

@H_944_14@password file must not be other-accessible

@H_944_14@conTinuing without password file

@H_944_14@这是因为/etc/rsync.pass /etc/passwd.txt的权限不对,应该设置为600。如:chmod 600 /etc/passwd.txt

@H_944_14@

@H_944_14@错误五:

@H_944_14@@ERROR: access denied to www from unkNown (192.168.1.123)

@H_944_14@rsync: connection unexpectedly closed (0 bytes received so far) [receiver]

@H_944_14@rsync error: error in rsync protocol data stream (code 12) at io.c(359)

@H_944_14@配置选项host allow的问题

@H_944_14@

@H_944_14@错误六:

@H_944_14@rsync error: received SIGINT,SIGTERM,or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]

@H_944_14@rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]

@H_944_14@导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r Now服务器

@H_944_14@

@H_944_14@错误七:

@H_944_14@rsync: read error: Connection reset by peer (104)

@H_944_14@rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]

@H_944_14@原数据目录里没有数据存在

@H_944_14@

@H_944_14@错误八:

@H_944_14@@ERROR: chroot Failed

@H_944_14@rsync: connection unexpectedly closed (75 bytes read so far)

@H_944_14@rsync error: error in rsync protocol data stream (code 12) at io.c(150)

@H_944_14@这是因为你在 rsync.conf 中设置的 path 路径不存在,要新建目录才能开启同步

@H_944_14@

@H_944_14@错误九:

@H_944_14@rsync: Failed to connect to %IP%: No route to host (113)

@H_944_14@rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]

@H_944_14@防火墙问题导致,这个最好先彻底关闭防火墙,排错的基本法就是这样,无论是S还是C,还有ignore errors选项问题也会导致

@H_944_14@

@H_944_14@错误十:

@H_944_14@@ERROR: auth Failed on module xxxxx

@H_944_14@rsync: connection unexpectedly closed (90 bytes read so far)

@H_944_14@rsync error: error in rsync protocol data stream (code 12) at io.c(150)

@H_944_14@这是因为密码设置错了,无法登入成功,检查一下/etc/rsync.pass /etc/passwd.txt。还有服务器端没启动rsync 服务也会出现这种情况。

大佬总结

以上是大佬教程为你收集整理的CentOS6.8使用Rsync+Inotify-tools实现数据实时同步全部内容,希望文章能够帮你解决CentOS6.8使用Rsync+Inotify-tools实现数据实时同步所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。