1. rsync简介
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
2. rsync特性
rsync支持很多特性:
- 可以镜像保存整个目录树和文件系统
- 可以很容易做到保持原来文件的权限、时间、软硬链接等等
- 无须特殊权限即可安装
- 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
- 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
- 支持匿名传输,以方便进行网站镜象
3. rsync的ssh认证协议
rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:
- ssh协议
- rsync协议
rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件
rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf
ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道
//这种方式默认是省略了 -e ssh 的,与下面等价:
rsync -avz /SRC -e ssh root@192.168.234.22:/DEST
-a //文件宿主变化,时间戳不变
-z //压缩数据传输
//当遇到要修改端口的时候,我们可以:
rsync -avz /SRC -e "ssh -p2222" root@192.168.234.22:/DEST
//修改了ssh 协议的端口,默认是2222
4. rsync命令
//Rsync的命令格式常用的有以下三种:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
//对应于以上三种命令格式,rsync有三种不同的工作模式
1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。
[root@wyz ~]# rsync -a anaconda-ks.cfg a.sh
[root@wyz ~]# ls
anaconda-ks.cfg a.sh
[root@wyz ~]# ll
total 8
-rw-------. 1 root root 1475 Mar 14 11:34 anaconda-ks.cfg
-rw-------. 1 root root 1475 Mar 14 11:34 a.sh
[root@wyz ~]# ll -i
total 8
100663362 -rw-------. 1 root root 1475 Mar 14 11:34 anaconda-ks.cfg
100743644 -rw-------. 1 root root 1475 Mar 14 11:34 a.sh
2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。
[root@wyz ~]# rsync -avz anaconda-ks.cfg root@192.168.234.22:/root/wyz.sh
sent 870 bytes received 35 bytes 106.47 bytes/sec
total size is 1,475 speedup is 1.63
[root@wyz ~]# ssh root@192.168.234.22 'ls -l /root'
root@192.168.234.22's password:
total 4
-rw-------. 1 root root 1475 Mar 14 23:34 wyz.sh
[root@wyz ~]#
3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。
[root@wyz ~]# rsync -avz root@192.168.234.22:/etc/yum.repos.d /root/
root@192.168.234.22's password:
receiving incremental file list
yum.repos.d/
yum.repos.d/CentOS7-Base-163.repo
yum.repos.d/epel-testing.repo
yum.repos.d/epel.repo
yum.repos.d/redhat.repo
yum.repos.d/xx.repo
sent 123 bytes received 1,729 bytes 740.80 bytes/sec
total size is 3,875 speedup is 2.09
[root@wyz ~]# ls
anaconda-ks.cfg a.sh yum.repos.d
//rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步
5. rsync+inotify
rsync与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
在前面有讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
示列
部署rsync+inotify同步/etc目录至目标服务器的/NAME/下。这里的NAME是指你的名字,比如你叫tom,则要把/etc目录同步至目标服务器的/tom/下。
- 环境准备
服务器类型 | IP地址 | 应用 | 操作系统 |
---|---|---|---|
源服务器 | 192.168.234.22 | rsync inotify-tools 脚本 | centos7/redhat7 |
目标服务器 | 192.168.234.11 | rsync | centos7/redhat7 |
- 在目标服务器上的操作
//关闭防火墙与SELINUX
[root@wyz ~]# systemctl stop firewalld
[root@wyz ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@wyz ~]# setenforce 0
[root@wyz ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
//安装rsync服务端软件
[root@wyz ~]# yum -y install rsync
....
Installed:
rsync.x86_64 0:3.0.9-18.el7
Complete!
//设置rsyncd.conf配置文件
[root@wyz ~]# 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 //用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
[etc_from_wyz] //自定义同步名称
path = /wyz/ //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 = wyz //执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
> EOF
//创建用户认证文件
[root@wyz ~]# echo 'wyz:123456' > /etc/rsync.pass
[root@wyz ~]# cat /etc/rsync.pass
wyz:123456
//设置文件权限
[root@wyz ~]# chmod 600 /etc/rsync*
[root@wyz ~]# ll /etc/rsync*
-rw-------. 1 root root 879 Apr 25 02:38 /etc/rsyncd.conf
-rw-------. 1 root root 11 Apr 25 02:39 /etc/rsync.pass
//启动rsync服务并设置开机自启动
[root@wyz ~]# systemctl start rsyncd
[root@wyz ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@wyz ~]# 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 :::*
LISTEN 0 80 :::3306 :::*
- 在源服务器上做以下操作:
//关闭防火墙与SELINUX
[root@server ~]# systemctl stop firewalld
[root@server ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@server ~]# setenforce 0
[root@server ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
//配置yum源
[root@server ~]# cd /etc/yum.repos.d/
[root@server yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@server ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@server ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@server ~]# yum -y install epel-release
//安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@server ~]# yum -y install rsync
//创建用户认证文件
[root@server ~]# echo '123456' > /etc/rsync.pass
[root@server ~]# cat /etc/rsync.pass
123456
//设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@server ~]# chmod 600 /etc/rsync.pass
[root@server ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 4月 25 15:13 /etc/rsync.pass
//同步查看
[root@server ~]# rsync -avH --port 873 --progress --delete /etc/ wyz@192.168.234.11::etc_from_wyz --password-file=/etc/rsync.pass
....
sent 37,083,326 bytes received 54,117 bytes 5,713,452.77 bytes/sec
total size is 36,889,803 speedup is 0.99
//目标服务器上文件同步完成
[root@wyz ~]# ls /wyz/|wc -l
277
//安装inotify-tools工具,实时触发rsync进行同步
//查看服务器内核是否支持inotify
[root@server ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r--. 1 root root 0 4月 25 15:28 max_queued_events
-rw-r--r--. 1 root root 0 4月 25 15:28 max_user_instances
-rw-r--r--. 1 root root 0 4月 25 15:28 max_user_watches
//如果有这三个max开头的文件则表示服务器内核支持inotify
//安装inotify-tools及其他工具
[root@server ~]# yum -y install make gcc gcc-c++ inotify-tools
//写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下
//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@server ~]# mkdir /scripts
[root@server ~]# touch /scripts/inotify.sh
[root@server ~]# chmod 755 /scripts/inotify.sh
[root@server ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 4月 25 15:31 /scripts/inotify.sh
//写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下
//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@server scripts]# vim /scripts/inotify.sh
#!/bin/bash
host=192.168.234.11
src=/etc/
des=etc_from_wyz
password=/etc/rsync.pass
user=wyz
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 $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
//执行脚本 (onhup命令可以让脚本在后台执行,即使当前远程窗口关闭也不会停止)
[root@server scripts]# nohup bash /scripts/inotify.sh &
[1] 3372
[root@server scripts]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@server scripts]# ps -ef|grep inotify
root 3372 2057 0 15:35 pts/0 00:00:00 bash /scripts/inotify.sh
root 3373 3372 0 15:35 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 /etc
root 3374 3372 0 15:35 pts/0 00:00:00 bash /scripts/inotify.sh
root 3417 2057 0 15:38 pts/0 00:00:00 grep --color=auto inotify
[root@server scripts]#
//在源服务器上生成一个新文件
[root@server ~]# mkdir /etc/wyz
[root@server ~]# echo "hello wyz" >>/etc/wyz/wangyizhen
[root@server ~]# cat /etc/wyz/wangyizhen
hello wyz
//在目标服务器上查看
[root@wyz ~]# ls /wyz/wyz/
wangyizhen
[root@wyz ~]# cat /wyz/wyz/wangyizhen
hello wyz
[root@wyz ~]#
- 拓展
//文件同步实时监控脚本开机自动启动
[root@server ~]# chmod +x /etc/rc.d/rc.local
[root@server ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 6月 27 2017 /etc/rc.d/rc.local
[root@server ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@server ~]# tail /etc/rc.d/rc.local
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# 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