rsync
rsync简介
rsync(remote synchronize)是linux系统下的数据镜像备份工具。使用快速增量备份工具remote rsync可以远程同步,支持本地复制,或者与其他ssh、rsync主机同步。rsync算法可不是每一次都整份传输,而是只传输两个文件的不同部分,因为传输速度极快。除此之外,rsync可拷贝、显示目录属性、删除文件、并可选择性的压缩以及递归拷贝。
rsync的工作原理
- 1.客户端构造文件,文件包含了需要与服务器同步的所有文件信息
- 2.客户端将文件信息发送到服务器
- 3.服务器上rsync同步收到客户端发过来的文件信息,构建新的文件信息
- 4.客户端得到服务器发送过来的文件信息,然后把文件信息的文件重新传输到服务器
rsync特性
rsync支持很多特性:
- 可以镜像保存整个目录树和文件系统
- 可以很容易做到保持原来文件的权限、时间、软硬链接等等
- 无须特殊权限即可安装
- 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。
- 压缩传输:rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
- 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
- 支持匿名传输,以方便进行网站镜像
- rsync不仅可以远程同步数据(类似于scp),而且可本地同步数据(类似于cp),做差异同步
- openssh 8.0已经把scp标记为过时不建议使用了。建议用sftp或者rsync替代scp
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打通通道
rsync有两种常用的认证方式,一种是rsync-daemon方式,另外一种是ssh方式。
在平时使用过程,我们使用最多的是rsync-daemon方式。
注意:在使用rsync时,服务器和客户端都必须安装rsync程序。
rsync-daemon 认证
rsync在rsync-daemon认证方式下,默认监听TCP的873端口。
rsync-daemon认证方式是rsync的主要认证方式,这个也是我们经常使用的认证方式。
并且也只有在此种模式下,rsync才可以把密码写入到一个文件中。
注意:
rsync-daemon认证方式,需要服务器和客户端都安装rsync服务
并且只需要rsync服务器端启动rsync,同时配置rsync配置文件。
客户端启动不启动rsync服务,都不影响同步的正常进行。
ssh认证
rsync在ssh认证方式下,可通过系统用户进行认证,即在rsync上通过ssh隧道进行传输,类似于scp工具。
此时同步操作不在局限于rsync中定义的同步文件夹。
注意:
ssh认证方式,不需要服务器和客户端配置rsync配置文件
只需要双方都安装rsync服务,并且也不需要双方启动rsync。
# 若rsync服务端SSH为标准端口,此时rsync使用方式如下:
rsync -avz /root/test root@10.10.10.10:/root/
# 若rsync服务端SSH为非标准端口,可通过rsync的-e参数进行端口指定。使用方式如下:
rsync -avz /root/test -e 'ssh -p1234' root@10.10.10.10:/root/
rsync命令
//Rsync的命令格式常用的有以下三种:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
//对应于以上三种命令格式,rsync有三种不同的工作模式:
1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:
[root@localhost ~]# ls
anaconda-ks.cfg nfs.sh
[root@localhost ~]# rsync -a nfs.sh a.sh
[root@localhost ~]# ll
total 12
-rw-------. 1 root root 1453 Jun 13 19:27 anaconda-ks.cfg
-rwxr-xr-x 1 root root 1041 Aug 8 18:14 a.sh
-rwxr-xr-x 1 root root 1041 Aug 8 18:14 nfs.sh
[root@localhost ~]# ll -i
total 12
33574978 -rw-------. 1 root root 1453 Jun 13 19:27 anaconda-ks.cfg
33574979 -rwxr-xr-x 1 root root 1041 Aug 8 18:14 a.sh
33574990 -rwxr-xr-x 1 root root 1041 Aug 8 18:14 nfs.sh
2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包 \
含单个冒号":"分隔符时启动该模式。如:
[root@localhost ~]# rsync -avz nfs.sh root@172.16.12.129:/root/b.sh
sending incremental file list
nfs.sh
sent 643 bytes received 35 bytes 1,356.00 bytes/sec
total size is 1,041 speedup is 1.54
[root@localhost ~]# ssh root@172.16.12.129 'ls -l /root'
total 8
-rw-------. 1 root root 1454 Aug 6 04:39 anaconda-ks.cfg
-rwxr-xr-x 1 root root 1041 Aug 8 2018 b.sh
3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径 \
包含单个冒号":"分隔符时启动该模式。如:
[root@localhost ~]# ls
anaconda-ks.cfg a.sh nfs.sh
[root@localhost ~]# rsync -avz root@172.16.12.129:/etc/yum.repos.d /root/
receiving incremental file list
yum.repos.d/
yum.repos.d/163.repo
yum.repos.d/redhat.repo
sent 66 bytes received 918 bytes 1,968.00 bytes/sec
total size is 1,820 speedup is 1.85
[root@localhost ~]# ls
anaconda-ks.cfg a.sh nfs.sh yum.repos.d
[root@localhost ~]# ls yum.repos.d/
163.repo redhat.repo
//rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步
rsync+inotify
rsync可以实现增量,同步海量的数据,具有安全性高、备份迅速等优点。通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等
随着系统的的不断更新不断扩大,对数据的安全性和可靠性也提出的更好的要求,所有rsync暴漏出了许多不足,如下:
- rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式.
- rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。
基于以上暴漏出来的不足,加入了inotify这个监控机制,因此,rsync+inotify组合就出现了!
Inotify可以监控系统中所有的事件。linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
触发式文件同步:通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
环境说明:
服务器类型 | IP地址 | 应用 | 操作系统 |
---|---|---|---|
源服务器 | 192.168.17.133 | rsync inotify-tools 脚本 | centos7/redhat7 |
目标服务器 | 192.168.17.135 | rsync | centos7/redhat7 |
需求:
- 把源服务器上/etc目录实时同步到目标服务器的/tmp/下
目标服务器配置
#关闭防火墙和selinux
[root@mubiao ~]# systemctl stop firewalld.service
[root@mubiao ~]#
[root@mubiao ~]# setenforce 0
setenforce: SELinux is disabled
#安装rsync服务端软件
[root@mubiao ~]# dnf -y install rsync
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:02:20 ago on Thu Sep 22 15:06:41 2022.
Dependencies resolved.
Installed:
rsync-3.1.3-19.el8.x86_64
Complete!
#配置rsyncd.conf配置文化
[root@mubiao etc]# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
[etc_from_client]
path = /tmp/
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 333
timeout = 600
auth users = admin
#创建用户认证文件
[root@mubiao ~]# echo 'admin:123456' > /etc/rsync.pass
[root@mubiao ~]# cat /etc/rsync.pass
admin:123456
#设置文件权限
[root@mubiao ~]# chmod 600 /etc/rsync.pass
[root@mubiao ~]# ll /etc/rsync.pass
-rw------- 1 root root 13 Sep 22 15:16 /etc/rsync.pass
#需下载rsync-daemon这个包
[root@mubiao ~]# dnf list all |grep rsync
Failed to set locale, defaulting to C.UTF-8
rsync.x86_64 3.1.3-19.el8 @baseos
libguestfs-rsync.x86_64 1:1.44.0-6.module_el8.7.0+1140+ff0772f9 appstream
rsync-daemon.noarch 3.1.3-19.el8 baseos
[root@mubiao ~]# dnf -y install rsync-daemon
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:02:40 ago on Thu Sep 22 15:16:20 2022.
#启动rsync服务并设置开机自启
[root@mubiao ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@mubiao ~]# systemctl status rsyncd.service
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2022-09-22 15:19:13 CST; 11s ago
源服务器配置
#关闭防火墙与SELINUX
[root@yuan ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@yuan ~]# setenforce 0
setenforce: SELinux is disabled
#配置yum源,已经配置过了,这边就不配置了
[root@yuan ~]# yum makecache
Failed to set locale, defaulting to C.UTF-8
CentOS Stream 8 - AppStream 1.4 MB/s | 25 MB 00:18
CentOS Stream 8 - BaseOS 64% [================== ] 1.5 MB/s | 16 MB 00:05 ETA
CentOS Stream 8 - BaseOS 68% [=================== ] 1.6 MB/s | 17 MB 00:04 ETA
CentOS Stream 8 - BaseOS 2.0 MB/s | 25 MB 00:12
CentOS Stream 8 - Extras 12 kB/s | 18 kB 00:01
Last metadata expiration check: 0:00:01 ago on Thu Sep 22 14:41:48 2022.
Metadata cache created.
[root@yuan ~]# yum -y install epel-release
#安装rsync服务端软件.只需要安装,不要启动,不需要配置
[root@yuan ~]# dnf -y install rsync
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:02:22 ago on Thu Sep 22 14:41:48 2022.
Dependencies resolved.
Installed:
rsync-3.1.3-19.el8.x86_64
Complete!
#创建认证密码文件
[root@yuan ~]# echo '123456' > /etc/rsync.pass
[root@yuan ~]# cat /etc/rsync.pass
123456
#设置文件权限,设置文件所有者具有读取、写入权限
[root@yuan ~]# chmod 600 /etc/rsync.pass
#在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@yuan ~]# mkdir -pv /root/etc/test
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/test'
[root@yuan ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.17.135::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_924-2722763428/
deleting systemd-private-cba6e0fe6fe944d9a70c76d694f79e4d-mariadb.service-ms4ptj/tmp/
deleting systemd-private-cba6e0fe6fe944d9a70c76d694f79e4d-mariadb.service-ms4ptj/
./
test/
sent 75 bytes received 214 bytes 578.00 bytes/sec
total size is 0 speedup is 0.00
#运行完成后,在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
[root@mubiao ~]# ls /tmp/
test
#查看服务器内核是否支持inotify
[root@yuan ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Sep 22 15:57 max_queued_events
-rw-r--r-- 1 root root 0 Sep 22 15:57 max_user_instances
-rw-r--r-- 1 root root 0 Sep 22 15:57 max_user_watches
#如果有这三个max开头的文件则表示服务器内核支持inotify
#下载inotify-tools这个包需要自己配置本地yum源
#安装inotify-tools
[root@yuan ~]# dnf -y install epel-release-8-11.el8.noarch
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:32:07 ago on Thu Sep 22 15:30:06 2022.
Dependencies resolved.
[root@yuan ~]# dnf list all|grep inotify
Failed to set locale, defaulting to C.UTF-8
inotify-tools.x86_64 3.14-19.el8 epel
inotify-tools-devel.x86_64 3.14-19.el8 epel
python3-inotify.noarch 0.9.6-13.el8 base
python3-inotify_simple.noarch 1.3.4-1.el8 epel
rubygem-rb-inotify.noarch 0.10.0-1.el8 epel
rubygem-rb-inotify-doc.noarch 0.10.0-1.el8 epel
[root@yuan ~]# dnf -y install inotify-tools
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:00:18 ago on Thu Sep 22 16:02:43 2022.
Dependencies resolved.
Installed:
inotify-tools-3.14-19.el8.x86_64
Complete!
#安装完成后,写同步脚本。让脚本自动去检测我们制定的目录下文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@yuan ~]# mkdir /scripts
[root@yuan ~]# cd /scripts/
[root@yuan scripts]# ls
[root@yuan scripts]# vim inotify.sh
[root@yuan scripts]# chmod 755 inotify.sh
[root@yuan scripts]# ll
total 0
-rwxr-xr-x 1 root root 0 Sep 22 18:13 inotify.sh
[root@yuan scripts]# vim inotify.sh
host=192.168.17.135
src=/etc
des=etc_from_client
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 $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
#设置脚本开机自动启动
[root@yuan scripts]# chmod +x /etc/rc.d/rc.local
[root@yuan scripts]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 474 Jan 28 2022 /etc/rc.d/rc.local
[root@yuan scripts]# vim /etc/rc.local
nohup /bin/bash /scripts/inotify.sh
#启动脚本
[root@yuan scripts]# /scripts/inotify.sh &
[1] 535248
[root@yuan scripts]# ps -ef|grep inotify
root 535248 89920 0 18:44 pts/0 00:00:00 /bin/bash /scripts/inotify.sh
root 535249 535248 0 18:44 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 535250 535248 0 18:44 pts/0 00:00:00 /bin/bash /scripts/inotify.sh
root 535960 89920 0 18:45 pts/0 00:00:00 grep --color=auto inotify
#在源服务器/etc下创建一个文件 ,看目标服务器同步没有
[root@yuan scripts]# touch /etc/123
[root@yuan scripts]# sending incremental file list
etc/
etc/.pwd.lock
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=686/688)
etc/.updated
208 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=685/688)
sending incremental file list
sent 20,162 bytes received 235 bytes 40,794.00 bytes/sec
total size is 23,863,302 speedup is 1,169.94
#发现已同步
[root@mubiao ~]# ll /etc/123
-rw-r--r-- 1 root root 0 Sep 22 18:45 /etc/123