微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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_server

rsync_client/app/rsync_client


rsync_client (目标端)10.15.43.228

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#支持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


rsync_server(数据源) 10.15.43.100

安装Rsync客户端

[root@localhostrsync_server]#whereisrsync#查看系统是否已安装rsync
rsync:/usr/bin/rsync/usr/share/man/man1/rsync.1.gz#说明已经安装
[root@localhostrsync_server]#

yum install xinetd #已安装rsync只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的

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


测试

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

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


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
totalsizeis0speedupis0.00
[root@localhostrsync_server]#

/app/rsync_server/数据源的目录

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

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


rsync_client

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


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

1、安装Inotify-tools工

[root@localhostsrc]#ll/proc/sys/fs/inotify#查看服务器内核是否支持inotify,出现下面的内容,说明服务器内核支持inotify
total0
-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]#

max_queued_events:

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

max_user_watches:

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

max_user_instances:

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


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&

src_dir="/app/rsync_server/"#源服务器同步目录

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

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

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

a/

b/b1/


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

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

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


inotify参数

-m 是保持一直监听

-r 是递归查看目录

-q 是打印出事件

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

rsync参数

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

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

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

3、测试

在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]#

在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]#

如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。

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


报错:

错误一:

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

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

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


错误二:

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

解决办法:

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

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


错误三:

password file must not be other-accessible

continuing without password file

Password:

解决办法:

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


错误四:

password file must not be other-accessible

continuing without password file

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


错误五:

@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]

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


错误八:

@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 %IP%: 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: 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)

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native
centos6.5下postgres-XC集群安装与配置
CentOS 6使用openssl搭建根CA
CentOS6.6中安装VNC server
CentOS下更新Python最新版本