说明:
操作系统: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服务端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
[root@localhost src]
# yum -y install rsync xinetd
[root@localhost src]
# cp /etc/xinetd.d/rsync{,default}
[root@localhost src]
# vim /etc/xinetd.d/rsync
service
rsync
{
disable = no
#修改为no
flags = IPv6
socket_type = stream
wait = no
user = root
server =
/usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@localhost src]
# /etc/init.d/xinetd start #CentOS中是以xinetd来管理Rsync服务的
[root@localhost src]
# vim /etc/rsyncd.conf #创建配置文件
logfile =
/var/log/rsyncd
.log
#日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile =
/var/run/rsyncd
.pid
#pid文件的存放位置
lockfile =
/var/run/rsync
.lock
#支持max connections参数的锁文件
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
use chroot = no
#默认为true,修改为no,增加对目录文件软连接的备份
read
only = no 设置
rsync
服务端文件为读写权限
list = no
#不显示rsync服务端资源列表
mac connections = 200
timeout = 600
auth
users
=
rsync
#执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 10.15.43.100
#允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 10.10.2.84
#禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
#先允许后拒绝
[root@localhost src]
# vim /etc/rsync.pass #配置文件,添加以下内容
rsync
:123456
#格式,用户名:密码,可以设置多个,每行一个用户名:密码
[root@localhost src]
# chmod 600 /etc/rsyncd.conf
[root@localhost src]
# chmod 600 /etc/rsync.pass
[root@localhost src]
# /etc/init.d/xinetd restart
|
rsync_server(数据源) 10.15.43.100
安装Rsync客户端
1
2
3
|
[root@localhost rsync_server]
# whereis rsync #查看系统是否已安装rsync
rsync
:
/usr/bin/rsync
/usr/share/man/man1/rsync
.1.gz
#说明已经安装
[root@localhost rsync_server]
#
|
yum install xinetd #已安装rsync只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的
yum install rsync xinetd #如果默认没有rsync,运行此命令进行安装rsync和xinetd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@localhost rsync_server]
# vim /etc/xinetd.d/rsync
service
rsync
{
disable = no
#修改为no
flags = IPv6
socket_type = stream
wait = no
user = root
server =
/usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@localhost rsync_server]
# /etc/init.d/xinetd restart
[root@localhost rsync_server]
# vim /etc/passwd.txt
123456
[root@localhost rsync_server]
# chmod 600 /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
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@localhost src]
# mkdir /app/rsync_client/test
[root@localhost src]
# touch /app/rsync_client/test/file
[root@localhost rsync_server]
# rsync -avH --port=873 --progress --delete /app/rsync_server/ rsync@10.15.43.228::app_rsync_client --password-file=/etc/passwd.txt
sending incremental
file
list
./
file
0 100% 0.00kB
/s
0:00:00 (xfer
#1, to-check=0/2)
sent 81 bytes received 30 bytes 222.00 bytes
/sec
total size is 0 speedup is 0.00
[root@localhost rsync_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
1
2
3
|
[root@localhost rsync_client]
# ls
file
[root@localhost rsync_client]
#
|
在rsync_server(数据源) 10.15.43.100上安装Inotify-tools工具,实时触发rsync进行同步
1、安装Inotify-tools工
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[root@localhost src]
# ll /proc/sys/fs/inotify #查看服务器内核是否支持inotify,出现下面的内容,说明服务器内核支持inotify
total 0
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_queued_events
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_instances
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_watches
[root@localhost src]
# uname -r #Linux下支持inotify的内核最小为2.6.13
2.6.32-642.el6.x86_64
[root@localhost src]
# tar zxvf inotify-tools-3.14.tar.gz
[root@localhost src]
# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]
# ./configure --prefix=/app/inotify
[root@localhost inotify-tools-3.14]
# make && make install
[root@localhost inotify-tools-3.14]
# vim /etc/profile #设置系统环境变量
export
PATH=
/app/inotify/bin
:$PATH
[root@localhost inotify-tools-3.14]
# source /etc/profile
[root@localhost inotify-tools-3.14]
# echo " /app/inotify/lib" > /etc/ld.so.conf.d/inotify.conf
[root@localhost inotify-tools-3.14]
# ln -s /app/inotify/include /usr/include/inotify
[root@localhost inotify-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@localhost inotify-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@localhost inotify-tools-3.14]
# cat /proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events}
65535
99999999
99999999
[root@localhost inotify-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进行同步
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[root@localhost inotify]
# cat rsync.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.228 10.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
}
for
ip
in
$dst_ip;
do
rsync_command
done
/app/inotify/bin/inotifywait
-mrq --timefmt
'%d/%m/%y %H:%M'
--
format
'%T %w%f%e'
-e close_write,modify,delete,create,attrib,move $src_dir \
|
while
read
file
;
do
for
ip
in
$dst_ip;
do
rsync_command
echo
"${file} was rsynced"
>>
/tmp/rsync
.log 2>&1
done
done
[root@localhost inotify]
# chmod +x rsync.sh
[root@localhost inotify]
# touch /app/inotify/exclude.list
[root@localhost inotify]
# vim /etc/rc.d/rc.local
nohup
/bin/sh
/app/inotify/rsync
.sh &
[root@localhost inotify]
# 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,delete,modify,attrib 是指监听“创建 移动 删除 写入 权限” 事件
rsync参数
-v, --verbose 详细模式输出
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-H, --hard-links 保留硬链结
3、测试
在rsync_server(数据源) 10.15.43.100的/app/rsync_server创建文件
1
2
3
4
5
6
7
|
[root@localhost rsync_server]
# touch test{1..9}
[root@localhost rsync_server]
# touch test{a..j}
[root@localhost rsync_server]
# ls
test1 test2 test3 test4 test5 test6 test7 test8 test9 testa testb testc testd teste testf testg testh testi testj
[root@localhost rsync_server]
# pwd
/app/rsync_server
[root@localhost rsync_server]
#
|
在rsync_client (目标端)10.15.43.228、10.10.2.84上查看已经同步
1
2
3
4
5
|
[root@localhost rsync_client]
# ls
test1 test2 test3 test4 test5 test6 test7 test8 test9 testa testb testc testd teste testf testg testh testi testj
[root@localhost rsync_client]
# pwd
/app/rsync_client
[root@localhost rsync_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 服务也会出现这种情况。
本文转自 justin_peng 51CTO博客,原文链接:http://blog.51cto.com/ityunwei2017/1951362,如需转载请自行联系原作者