RSYNC 服务介绍
一、rsync介绍
简介:
rsync数据同步优缺点
与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足。首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
- rsync功能
- 作为命令,实现本地-远程文件同步
- 作为服务,实现本地-远程文件同步
- rsync特点
- 可以镜像保存整个目录树和文件系统
- 可以保留原有的权限(permission,mode),owner,group,时间(修改时间,modify time),软硬链接,文件acl,文件属性(attributes)信息等
- 传输效率高,使用同步算法,只比较变化的
- 支持匿名传输,方便网站镜像;也可以做验证,加强安全
- rsync同类服务
- sync 同步:刷新文件系统缓存,强制将修改过的数据块写入磁盘,并且更新超级块。
- async 异步:将数据先放到缓冲区,再周期性(一般是30s)的去同步到磁盘。
- rsync 远程同步:remote synchronous
-
inotify
inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
二、rsync语法介绍
安装服务软件:yum -y install xinetd rsync
2.1 man rsync查看
rsync(1) rsync(1)
NAME
rsync ? a fast, versatile, remote (and local) file-copying tool //rsync 介绍
SYNOPSIS
Local: rsync [OPTION...] SRC... [DEST]
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
//shell远程访问(命令模式)
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
//通过后台程序访问(作为服务)
Usages with just one SRC arg and no DEST arg will list the source
files instead of copying.
2.2 rsync相关参数
-v 详细模式输出
-a 归档模式,递归的方式传输文件,并保持文件的属性,equals -rlptgoD
-r 递归拷贝目录
-l 保留软链接
-p 保留原有权限
-t 保留原有时间(修改)
-g 保留属组权限
-o 保留属主权限
-D 等于--devices --specials 表示支持b,c,s,p类型的文件
-R 保留相对路径
-H 保留硬链接
-A 保留ACL策略
-e 指定要执行的远程shell命令
-E 保留可执行权限
-X 保留扩展属性信息 a属性
三、 rsync作为命令同步数据
3.1 本机同步数据
[root@review1 ~]# mkdir dir1
[root@review1 ~]# mkdir dir2
[root@review1 ~]# touch dir1/file{1..5}
[root@review1 ~]# ls dir1/
file1 file2 file3 file4 file5
[root@review1 ~]# ls dir2
[root@review1 ~]#
//检测dir1中有5个文件,dir2中没有文件
//同步方式一:
[root@review1 ~]# rsync -va /root/dir1 /root/dir2
sending incremental file list
dir1/
dir1/file1
dir1/file2
dir1/file3
dir1/file4
dir1/file5
sent 279 bytes received 111 bytes 780.00 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]#
[root@review1 ~]# ls dir2
dir1
//同步dir1到dir2中成功
//方式二:
[root@review1 ~]# rsync -va /root/dir1/ /root/dir2
sending incremental file list
./
file1
file2
file3
file4
file5
sent 266 bytes received 110 bytes 752.00 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls dir2
dir1 file1 file2 file3 file4 file5
总结:
1. 本地数据同步的时候,源目录后面的“/”会影响同步的结果
2. # rsync -av /dir1/ /dir3 //只同步目录下面的文件到指定的路径
3. # rsync -av /dir1 /dir2 //将当前目录dir1和目录下的所有文件一起同步
-R:不管加不加”/”,都会将源数据的绝对路径一起同步
3.2 远程同步
需求1:将本地(192.168.221.129)/root/dir1
文件同步到远端(192.168.226.128)/root/dir
中:
[root@review1 ~]# rsync -av /root/dir1 root@192.168.226.128:/root/dir/ //同步的命令
The authenticity of host '192.168.226.128 (192.168.226.128)' can't be establis
RSA key fingerprint is 24:36:34:69:1f:6e:b7:60:b0:2a:ae:90:46:aa:86:c5.
Are you sure you want to continue connecting (yes/no)? yes //key授权
Warning: Permanently added '192.168.226.128' (RSA) to the list of known hosts.
root@192.168.226.128's password: //远端用户密码
sending incremental file list
dir1/
dir1/file1
dir1/file2
dir1/file3
dir1/file4
dir1/file5
sent 279 bytes received 111 bytes 26.90 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]#
//远端:192.168.226.128
[root@min1 dir]# pwd
/root/dir
[root@min1 dir]# ls
dir1
[root@min1 dir]#
//同步成功
- 本地到远程同步另一种写法,利用ssh
将本地(192.168.221.129)/root/dir文件同步到远端(192.168.226.128)/root/中:
[root@review1 dir2]# rsync -ave 'ssh -lroot' /root/dir 192.168.226.128:/root/
root@192.168.226.128's password:
sending incremental file list
dir/
dir/dir1/
dir/dir1/file1
dir/dir1/file2
dir/dir1/file3
dir/dir1/file4
dir/dir1/file5
sent 300 bytes received 115 bytes 26.77 bytes/sec
total size is 0 speedup is 0.00
主机192.168.226.128
[root@min1 ~]# pwd
/root
[root@min1 ~]# ls
anaconda-ks.cfg install.log software
dir install.log.syslog testdir
[root@min1 ~]# cd dir
[root@min1 dir]# ls
dir1
[root@min1 dir]#
//同步成功
需求2:将远程数据/root/dir
同步到本地/root
中:
[root@review1 ~]# rsync -av root@192.168.226.128:/root/dir /root
root@192.168.226.128's password:
receiving incremental file list
dir/
dir/dir1/
dir/dir1/file1
dir/dir1/file2
dir/dir1/file3
dir/dir1/file4
dir/dir1/file5
sent 114 bytes received 305 bytes 25.39 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls /root
anaconda-ks.cfg dir dir1 dir2 install.log install.log.syslog
[root@review1 ~]#
//同步成功!
注1:
rsync 并不是单纯的复制文件,它主要功能是进行文件同步!例如:
//主机:192.168.226.128 中testdir目录下有5个文件
[root@min1 testdir]# pwd
/root/testdir
[root@min1 testdir]# ls
file1 file2 file3 file4 file5
[root@min1 testdir]#
//本地主机:192.168.221.129 中/root目录
[root@review1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog
//将远程testdir同步到本地
[root@review1 ~]# rsync -avR root@192.168.226.128:testdir /root/
root@192.168.226.128's password:
receiving incremental file list
testdir/
testdir/file1
testdir/file2
testdir/file3
testdir/file4
testdir/file5
sent 110 bytes received 287 bytes 27.38 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog testdir
[root@review1 ~]# cd testdir/
[root@review1 testdir]# ls
file1 file2 file3 file4 file5
//远端删除file1-file3
[root@min1 testdir]# ls
file1 file2 file3 file4 file5
[root@min1 testdir]# rm file{2..4}
rm: remove regular empty file `file2'? y
rm: remove regular empty file `file3'? y
rm: remove regular empty file `file4'? y
[root@min1 testdir]# ls
file1 file5
//本地同步
[root@review1 testdir]# rsync -avR --delete root@192.168.226.128:testdir /root/
root@192.168.226.128's password:
receiving incremental file list
deleting testdir/file4
deleting testdir/file3
deleting testdir/file2
testdir/
sent 15 bytes received 75 bytes 3.16 bytes/sec
total size is 0 speedup is 0.00
[root@review1 testdir]# ls
file1 file5
[root@review1 testdir]#
//加上--delete参数,源文件删除,本地也删除
总结:
rsync是一个同步命令(服务),它不仅可以用来复制、备份,最大的作用在于同步,即保持两端一直,所以远端文件被删除后,同步后,本地文件也可以删除,要注意rsync的灵活用法。
注2:
rsync同步参数-R是会同步绝对路径的。例:
[root@review1 ~]# rsync -avR root@192.168.226.128:/root/testdir /root/
root@192.168.226.128's password:
Permission denied, please try again.
root@192.168.226.128's password:
receiving incremental file list
root/
root/testdir/
root/testdir/file1
root/testdir/file2
root/testdir/file3
root/testdir/file4
root/testdir/file5
sent 114 bytes received 315 bytes 15.05 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog root
[root@review1 ~]# cd root/
[root@review1 root]# ls
testdir
//此处同步将192.168.226.128中的root也创建了一个,同步的是绝对路径,没有的文件夹自动帮你创建。
[root@review1 root]# rsync -avR root@192.168.226.128:testdir /root/
root@192.168.226.128's password:
receiving incremental file list
testdir/
testdir/file1
testdir/file2
testdir/file3
testdir/file4
testdir/file5
sent 110 bytes received 287 bytes 29.41 bytes/sec
total size is 0 speedup is 0.00
[root@review1 root]# ls
testdir
[root@review1 root]# cd
[root@review1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog root testdir
//将root从目录中去掉就不会再创建root,直接在本地的/root 中直接创建了testdir
总结:
rsync -R会同步绝对路径,没有则自动创建,所以在写命令时一定要注意。
四、rsync作为服务运行
环境准备
操作系统:CentOS release 6.8 (Final) x86_64
服务器IP:
rsync_server(数据源)192.168.0.44
rsync_client(目标端)192.168.0.45
同步目录:
rsync_server /app/rsync_server
rsync_client /app/rsync_client
rsync作为服务是托管给xinetd服务管理的,有以下特点:①进程在后台运行,不受终端影响(关终端不会关闭服务,除非杀死相关进程)②可以用相关参数实现一些功能,比如:日志记录,访问控制,验证登录等
4.1 同步
- 修改子配置文件
/etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
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@review1 etc]# pwd
/etc
[root@review1 etc]# ls |grep rsync.conf
[root@review1 etc]#
- 创建主配置文件
/etc/rsyncd.conf
(注意,这里是rsyncd.conf 不是rsync.conf,配置文件名写错会报错,无法远程同步。) -
# 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 = 192.168.0.45 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开 hosts deny = 192.168.0.46,192.168.0.47 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开,先允许后拒绝
-
配置rsync同步的账户密码
# vim /etc/rsync.pass #配置文件,添加以下内容 rsync:123456 #格式,用户名:密码,可以设置多个,每行一个用户名:密码
-
赋权启动rsync,rsync有严格控制的权限
# chmod 600 /etc/rsyncd.conf # chmod 600 /etc/rsync.pass # /etc/init.d/xinetd restart
-
注:若要把本地文件同步到远程主机上,需要更改主配置文件
配置rsync同步的账户密码
# vim /etc/passwd.txt
123456
# chmod 600 /etc/passwd.txt
4.2 密码同步
特别注意:
①配置文件中auth users 写了哪个用户,哪个用户就可以访问,没写的就不能访问,密码访问一旦开启,所有人都要输入密码,所以没有在auth users中指定的用户是无法访问的。
②密码文件的属主必须是rsync服务的运行者,权限必须是600。例如:root运行rsync –daemon,则secrets file的owner也必须是root;secrets file权限必须是600。
测试手动同步
# mkdir -pv /app/rsync_server && touch /app/rsync_server/test.txt
在rsync_server的/app/rsync_server目录下创建文件test.txt,在rsync_server端运行同步命令同步数据:
rsync -avH --port=873 --progress --delete /app/rsync_client/ rsync@192.168.0.45::app_rsync_client --password-file=/etc/passwd.txt
注释:
/app/rsync_server/ #数据源的目录
-password-file=/etc/passwd.txt #数据源的密码文件
rsync@10.15.43.228::app_rsync_client #rsync目标端rsync服务端配置的用户名,app_rsync_client目标端rsync服务端配置的模块名称
检查客户端rsync_client目录
# ls /app/rsync_client/
test.txt
五、rsync+inotify架构实现数据实时同步
5.1 安装inotify工具
下载地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
- 解压软件到指定文件夹
[root@review1 software]# ls
inotify-tools-3.13.tar.gz
[root@review1 software]# tar zxvf inotify-tools-3.13.tar.gz -C /usr/src/
- 配置并安装
[root@review1 software]# cd /usr/src/inotify-tools-3.13/
[root@review1 inotify-tools-3.13]# ls
aclocal.m4 config.h.in COPYING libinotifytools man src
AUTHORS config.sub depcomp ltmain.sh missing
ChangeLog configure INSTALL Makefile.am NEWS
config.guess configure.ac install-sh Makefile.in README
[root@review1 inotify-tools-3.13]# ./configure --prefix=/usr/local/inotify
//编译、安装
[root@review1 inotify-tools-3.13]# make && make install
- inotify-tools也可以通过EPEL存储库获得。安装EPEL:
-
yum install -y epel-release && yum update
然后安装包:
yum -y install inotify-tools
- 检查安装情况
[root@review1 inotify-tools-3.13]# cd /usr/local/inotify/
[root@review1 inotify]# ls
bin include lib share
[root@review1 inotify]# cd bin/
[root@review1 bin]# ls
inotifywait inotifywatch
[root@review1 bin]# cd ../include/
[root@review1 include]# ls
inotifytools
[root@review1 include]# cd ../lib/
[root@review1 lib]# ls
libinotifytools.a libinotifytools.so libinotifytools.so.0.4.1
libinotifytools.la libinotifytools.so.0
[root@review1 lib]# cd ../share/
[root@review1 share]# ls
doc man
配置inotify-tools
# 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
# vim /etc/sysctl.conf 添加
fs.inotify.max_queued_events = 99999999
fs.inotify.max_user_watches = 99999999
fs.inotify.max_user_instances = 65535
#sysctl -p 参数立即生效
# cat /proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events} #检查参数是否生效
65535
99999999
99999999
注释:
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实例最大值
3、创建实时同步脚本
# vim /usr/local/inotify/rsync.sh
#!/bin/bash
src_dir="/app/rsync_server/"
dst_dir="app_rsync_client"
exclude_dir="/usr/local/inotify/exclude.list"
rsync_user="rsync"
rsync_passwd="/etc/passwd.txt"
dst_ip="192.168.0.45"
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
/usr/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
注释:
src_dir="/app/rsync_server/" #源服务器同步目录
dst_dir="app_rsync_client" #目标服务器rsync同步目录模块名称
exclude_dir="/usr/local/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="192.168.0.45" #目标服务器ip,多个ip用空格分开
##赋权,添加开机启动
# chmod +x /usr/local/inotify/rsync.sh
# touch /usr/local/inotify/exclude.list
# vim /etc/rc.d/rc.local
nohup /bin/sh /usr/local/inotify/rsync.sh &
# nohup /bin/sh /usr/local/inotify/rsync.sh &
4、测试
在rsync_server(数据源)192.168.0.44的/app/rsync_server创建文件
# cd /app/rsync_server
# touch test{1..9}
# touch test{a..j}
# ls
test1 test2 test3 test4 test5 test6 test7 test8 test9 testa testb testc testd teste testf testg testh testi testj
在rsync_client(目标端)192.168.0.45上查看已经同步
# cd /app/rsync_client
# ls
test1 test2 test3 test4 test5 test6 test7 test8 test9 testa testb testc testd teste testf testg testh testi testj
如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。
至此,Linux下Rsync+Inotify-tools实现数据实时同步完成。如果要双向同步可以把以上反过来部署一次。
FAQ
Q1:
#rsync -avH --port=873 --progress --delete /app/rsync_client/ rsync@192.168.0.45::app_rsync_client --password-file=/etc/passwd.txt
@ERROR: auth failed on module app_rsync_client
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
A:如果出现这个错误,请详细检查配置文件是否有误,建议删掉无用的注释
Q2:
#rsync -avH --port=873 --progress --delete /app/rsync_client rsync@192.168.0.45::app_rsync_client --password-file=/etc/passwd.txt
sending incremental file list
rsync: link_stat "/app/rsync_client" failed: No such file or directory (2)
A:检查客户端及服务端文件夹是否存在,这里应该还有一个坑,就是这里是在服务端(数据源)同步,目录应该指向“/app/rsync_client”
因此,如果是同步应用程序目录,建议这里的源目录,与目标目录设置为同一个。
或者简单点的,例2:
1 #!/bin/bash
2 /usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create, attrib,move /dir |while read events
3 do
4 rsync -a --delete /dir/ /dir1/ ; //同步dir到dir1
5 echo "`date +%F\ %T`出现事件$events" >> /var/log /rsync.log 2>&1;
6 done
7
8 # chmod +x 1.sh
注:①/usr/local/inotify/bin/inotifywait inotify监测模块命令所在路径
②/dir 监测目录
③/dir1 备份目录
④这里的rsync也可以远程同步到其他主机,和rsync的远程同步写法一样。
- 测试
[root@review1 bin]# cd /dir
[root@review1 dir]# ls
file1 file2 file3
[root@review1 dir]# cd /dir1
[root@review1 dir1]# ls
[root@review1 dir1]#
[root@review1 dir1]# ls
[root@review1 dir1]# cd /dir
[root@review1 dir]# mkdir test
[root@review1 dir]# cd /dir1
[root@review1 dir1]# ls
file1 file2 file3 test
//同步成功
//日志
tail -f /var/log/rsync.log
2018-05-25 09:19:23出现事件/dir/ CREATE,ISDIR test
5.3 错误总结
- inotify监测目录问题
/usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create, attrib,move /root/dir |while read events
//注意这里的监测目录是/root/dir
//报错
[root@review1 bin]# ./inotify.sh
Couldn't watch /root/dir: File name too long
结论:
这里的File name too long是因为/root/dir/目录内部递归了太多的目录,目录太多,inotify就无法监测了!
文章转载于 https://blog.csdn.net/qq_21419995/article/details/80458379 http://blog.51cto.com/ljohn/2047156