会哭的雨 2020-05-30
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:
rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件
rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf
ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道
# rsync常用选项:
-a, --archive 归档
-v, --verbose 啰嗦模式
-q, --quiet 静默模式
-r, --recursive 递归
-p, --perms 保持原有的权限属性
-z, --compress 在传输时压缩,节省带宽,加快传输速度
--delete 在源服务器上做的删除操作也会在目标服务器上同步
# Rsync的命令格式常用的有以下三种:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC []HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
1)拷贝本地文件
[ ~]# ls
anaconda-ks.cfg
[ ~]# rsync -a anaconda-ks.cfg a.cfg
[ ~]# ls
a.cfg anaconda-ks.cfg
2) 将本地机器的内容拷贝到远程机器
[ ~]# rsync -avz anaconda-ks.cfg :/root/b.cfg # 需要两边都安装rsync服务
[ ~]# ls
b.cfg
3) 将远程机器的内容拷贝到本地机器
[ ~]# rsync -avz :/etc/yum.repos.d /root/ #目录后面加上"/"会拷贝目录下所有东西不加会拷贝目录本身
[ ~]# ls
yum.repos.d
[ ~]# rsync -avz :/etc/yum.repos.d/ /root/
[ ~]# ls
ss.repo redhat.reporsync与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,inux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
在前面有讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
环境说明:
服务器类型 IP地址 应用 操作系统
客户端 192.168.100.96 rsync,inotify-tools,脚本 centos7/redhat7
服务端 192.168.100.100 rsync centos7/redhat7
需求:
部署rsync+inotify同步客户端/runtime目录至目标服务器的/tmp/下。
步骤:
服务端操作如下:
# 关闭防火墙和selinux [ ~]# systemctl stop firewalld [ ~]# systemctl disable firewalld [ ~]# getenforce Disabled # 安装rsync服务 [ ~]# yum -y install rsync # 设置rsyncd.conf配置文件 [ ~]# cat >> /etc/rsyncd.conf <<EOF log file = /var/log/rsyncd.log # 日志文件位置,启动rsync后自动产生这个文件,无需提前创建 pidfile = /var/run/rsyncd.pid # pid文件的存放位置 lock file = /var/run/rsync.lock # 支持max connections参数的锁文件 secrets file = /etc/rsync.pass # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件 [www] # 自定义同步名称 path = /tmp/ # rsync服务端数据存放路径,客户端的数据将同步至此目录 comment = sync etc from client # 说明信息 uid = root # 设置rsync运行权限为root gid = root # 设置rsync运行权限为root port = 873 # 默认端口 ignore errors # 表示出现错误忽略错误 use chroot = no # 默认为true,修改为no,增加对目录文件软连接的备份 read only = no # 设置rsync服务端为读写权限 list = no # 不显示rsync服务端资源列表 max connections = 200 # 最大连接数 timeout = 600 # 设置超时时间 auth users = admin # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开 hosts allow = 192.168.100.96 # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开 hosts deny = 192.168.1.1 # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开 EOF # 创建用户认证文件 [ ~]# echo ‘admin:123456‘ > /etc/rsync.pass [ ~]# cat /etc/rsync.pass admin:123456 # 设置文件权限 [ ~]# chmod 600 /etc/rsync* [ ~]# ll /etc/rsync* -rw------- 1 root root 1946 8月 3 12:32 /etc/rsyncd.conf -rw------- 1 root root 13 8月 3 12:32 /etc/rsync.pass # 启动rsync服务并设置开机自启动 [ ~]# systemctl restart rsyncd [ ~]# systemctl enable rsyncd # 查看端口号873是否起来 [ ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 5 *:873 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 5 :::873 :::*
客户端操作如下:
# 关闭防火墙与SELINUX
[ ~]# systemctl stop firewalld
[ ~]# systemctl disable firewalld
[ ~]# getenforce
Disabled
# 因为客户端需要安装inotify-tools工具所以需要配置网络源
[ ~]# cd /etc/yum.repos.d/
[ yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[ yum.repos.d]# sed -i ‘s/\$releasever/7/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[ yum.repos.d]# sed -i ‘s/^enabled=.*/enabled=1/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[ yum.repos.d]# yum -y install epel-release
# 安装rsync服务
[ yum.repos.d]# yum -y install rsync
# 创建认证密码文件
[ ~]# echo ‘123456‘ > /etc/rsync.pass #客户端只需要密码不需要用户名
[ ~]# cat /etc/rsync.pass
123456
# 设置文件权限为600
[ ~]# chmod 600 /etc/rsync.pass
[ ~]# ll /etc/rsync.pass
-rw------- 1 root root 7 8月 3 16:00 /etc/rsync.pass
# 在客户端上创建测试目录
[ ~]# mkdir /runtime
# 在客户端上同步/runtime目录
[ ~]# rsync -avH --port 873 --progress --delete /runtime ::www --password-file=/etc/rsync.pass
sending incremental file list
runtime/
sent 65 bytes received 24 bytes 178.00 bytes/sec
total size is 0 speedup is 0.00
# 完成后去服务端/tmp下面查看是否存在runtime这个目录,有说明同步成功
[ ~]# ls /tmp/
runtime
# 安装inotify-tools工具,实时触发rsync进行同步
# 查看服务器内核是否支持inotify
[ ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 8月 3 16:32 max_queued_events
-rw-r--r-- 1 root root 0 8月 3 16:32 max_user_instances
-rw-r--r-- 1 root root 0 8月 3 16:32 max_user_watches
# 如果有这三个文件说明该系统支持innotify
# 安装inotify-tools
[ ~]# yum -y install make gcc gcc-c++
[ ~]# yum -y install inotify-tools
# 写同步脚本
[ ~]# mkdir /scripts
[ ~]# touch /scripts/inotify.sh
[ ~]# chmod 755 /scripts/inotify.sh
[ ~]# vim /scripts/inotify.sh
host=192.168.100.100 //目标服务器的ip(备份服务器)
src=/runtime //在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=www //自定义的模块名,需要与目标服务器上定义的同步名称一致
password=/etc/rsync.pass //执行数据同步的密码文件
user=admin //执行数据同步的用户名
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt ‘%Y%m%d %H:%M‘ --format ‘%T %w%f%e‘ -e modify,delete,create,attrib $src | while read files;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
# 启动脚本
[ ~]# nohup bash /scripts/inotify.sh &
[1] 9718
[ ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[ ~]# ps -ef |grep inotify
root 8270 1 0 11:38 ? 00:00:00 bash /scripts/inotify.sh
root 8271 8270 0 11:38 ? 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /runtime
root 8272 8270 0 11:38 ? 00:00:00 bash /scripts/inotify.sh
root 9718 9328 0 16:41 pts/0 00:00:00 bash /scripts/inotify.sh
root 9719 9718 0 16:41 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /runtime
root 9720 9718 0 16:41 pts/0 00:00:00 bash /scripts/inotify.sh
root 9722 9328 0 16:41 pts/0 00:00:00 grep --color=auto inotify
# 在客户端生成一个文件到/runtime
[ ~]# echo "ni hao" >/runtime/abc
[ ~]# ls /runtime/
abc
# 查看inotify生成的日志
[ ~]# cat /tmp/rsync.log
20190803 16:46 /runtime/abcCREATE was rsynced
20190803 16:46 /runtime/abcMODIFY was rsynced
# 服务端查看/tmp/runtime同步过去没有
[ ~]# ls /tmp/runtime/
abc设置脚本开机自动启动:
# 给 /etc/rc.d/rc.local这个文件执行权限 [ ~]# ll /etc/rc.d/rc.local -d -rw-r--r--. 1 root root 473 10月 31 2018 /etc/rc.d/rc.local [ ~]# chmod +x /etc/rc.d/rc.local [ ~]# ll /etc/rc.d/rc.local -d -rwxr-xr-x. 1 root root 473 10月 31 2018 /etc/rc.d/rc.local [ ~]# echo ‘nohup /bin/bash /scripts/inotify.sh‘ >> /etc/rc.d/rc.local [ ~]# tail -5 /etc/rc.d/rc.local # Please note that you must run ‘chmod +x /etc/rc.d/rc.local‘ to ensure # that this script will be executed during boot. touch /var/lock/subsys/local nohup /bin/bash /scripts/inotify.sh
到目标服务器上去查看是否把新生成的文件自动传上去了:
[ tmp]# pwd /tmp [ tmp]# ls etc test [ tmp]# ls etc/httpd24/ extra httpd.conf magic mime.types original test //由此可见,已将源服务器的/etc目录整个同步到了目标服务器,且新增的test文件也自动同步了
在客户端上执行rsync命令出现报错:
[ ~]# rsync -avH --port 873 --progress --delete /runtime ::www --password-file=/etc/rsync.pass @ERROR: auth failed on module www rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
原因:
1、密码输入错误:
请再次确认你登录用户的密码无误
2、配置文件写错:
看看自己模块配置下面的 *auth users、secrets file 对应信息是否存在疏忽
3、secrets file 权限问题:
服务端的 secrets file 权限必须是600
4、secrets file 格式错误:
secrets file 的文件格式是 user:password
而,服务端与客户端中的文件格式有所不同
其中:服务端文件需要填写用户名和密码,并且以冒号分割,客户端中却只填写密码即可
5、注释不兼容的问题:
配置文件的注释信息最好都进行换行处理 [ ~]# cat /etc/rsyncd.conf log file = /var/log/rsyncd.log # 日志文件位置,启动rsync后自动产生这个文件 pidfile = /var/run/rsyncd.pid # pid文件的存放位置 lock file = /var/run/rsync.lock # 支持max connections参数的锁文件 secrets file = /etc/rsync.pass # 用户认证配置文件,里面保存用户名称和密码 .......
本人之前就没有换行所以报了这个错误,修改之后就没问题了
其他报错:
@ERROR: chroot failed rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器端的目录不存在或无权限,创建目录并修正权限可解决问题
@ERROR: Unknown module ‘runtime‘ rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
原因:
服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题,也就是服务端配置文件中括号里面的名字跟上面命令中的不一致。
@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]
原因:
防火墙问题导致,这个最好先彻底关闭防火墙,还有ignore errors选项问题也会导致