Rsync服务
文章目录1 Rsync服务介绍2 Rsync特性3 rsync的认证协议3.1 rsync-daemon认证3.2 ssh认证4 Rsync工作模式5 Rsync命令格式和常用命令6 安装Rsync命令7 守护进程模式1 Rsync服务介绍Rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。Rs
文章目录
1 Rsync服务介绍
Rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。Rsync软件适用于unix/linux/windows等多种操作系统平台
Rsync是一个快速和非常通用的文件复制工具。它能本地复制,远程复制,或者远程守护进程方式复制。它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步
2 Rsync特性
- 可以镜像保存整个目录树和文件系统文件系统
- 可以很容易做到保持原来文件的权限、时间、软硬链接等等
- 无须特殊权限即可安装
- 快速:第一次同步时 rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩解压缩)操作,因此可以使用更少的带宽
- 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
- 支持匿名传输,以方便进行网站镜像
总结: 一个rsync命令相当于scp、cp、rm命令,但是rsync命令比scp、cp、rm命令更胜一筹
3 rsync的认证协议
3.1 rsync-daemon认证
- rsync在rsync-daemon认证方式下,默认监听TCP的873端口
- rsync-daemon认证方式是rsync的主要认证方式,这个也是我们经常使用的认证方式
- 并且也只有在此种模式下,rsync才可以把密码写入到一个文件中
3.2 ssh认证
- rsync在ssh认证方式下,可通过系统用户进行认证,即在rsync上通过ssh隧道进行传输,类似于scp工具
- 同步操作不在局限于rsync中定义的同步文件夹
- rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件
- rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf
注意: ssh认证方式,不需要服务器和客户端配置rsync配置文件,只需要双方都安装rsync服务,并且也不需要双方启动rsync
//这种方式默认是省略了 -e ssh 的,与下面等价:
rsync -avz /SRC -e ssh root@192.168.25.148:/DEST
-a //文件宿主变化,时间戳不变
-V //显示详细信息的过程
-z //压缩数据传输
[root@rsync ~]# rsync -avz anaconda-ks.cfg -e ssh root@192.168.25.148:/opt/
[root@client ~]# ls /opt/
anaconda-ks.cfg data
//当遇到要修改端口的时候,我们可以:
#修改了ssh 协议的端口,默认是22
rsync -avz /SRC -e "ssh -p2222" root@192.168.25.148:/DEST
4 Rsync工作模式
- 本地文件系统上实现同步;单个主机本地之间的数据传输(此时类似于cp命令的功能)
- 本地主机使用远程shell和远程主机通信;借助rcp,ssh等通道来传输数据(此时类似于 scp命令的功能)
- 本地主机通过网络套接字连接远程主机上的rsync daemon;以守护进程(socket)的方式传输数据(这个是rsync自身的重要的功能)
5 Rsync命令格式和常用命令
//备份本地数据
rsync [OPTION...] SRC... [DEST]
src:要备份的数据信息
dest:备份到什么路径中
[root@rsync ~]# rsync -avz anaconda-ks.cfg /opt/AA
[root@rsync ~]# ls /opt/
AA
//将本地机器的内容拷贝到远程机器
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
SRC 本地要进行远程传输备份的数据
[USER@] 以什么用户身份拉取数据(默认以当前用户)
hosts 指定远程主机IP地址或者主机名称
dest 保存到远程的路径信息
[root@rsync ~]# rsync -avz anaconda-ks.cfg root@192.168.25.148:/opt/
[root@client ~]# ls /opt/
anaconda-ks.cfg data
//将远程机器的内容拷贝到本地机器
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
[USER@] 以什么用户身份拉取数据(默认以当前用户)
注意:需要对端也要有相同的用户(对端用户密码)
hosts 指定远程主机IP地址或者主机名称
SRC 要拉取的数据信息
dest 保存到本地的路径信息
[root@rsync ~]# rsync -avz root@192.168.25.148:/opt/data .
[root@rsync ~]# ls
anaconda-ks.cfg data
//rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步
//同步删除
[root@rsync ~]# rsync -ravz /tmp root@192.168.25.148:/root
[root@client ~]# ls tmp/
abc
systemd-private-3b05b79d36124c93b1114941409b0591-chronyd.service-815bKy
vmware-root_930-2722763397
[root@rsync ~]# rm -rf /tmp/abc
[root@rsync ~]# rsync -ravz /tmp --delete root@192.168.25.148:/root
[root@client ~]# ls tmp/
systemd-private-3b05b79d36124c93b1114941409b0591-chronyd.service-815bKy
vmware-root_930-2722763397
6 安装Rsync命令
// 查看安装rsunc命令需要安装那个包
[root@rsync ~]# yum provides */bin/rsync
CentOS Stream 8 - AppStream 975 kB/s | 15 MB 00:15
CentOS Stream 8 - BaseOS 1.1 MB/s | 12 MB 00:10
CentOS Stream 8 - Extras 7.7 kB/s | 15 kB 00:01
rsync-3.1.3-12.el8.x86_64 : A program for synchronizing files over a network
仓库 :baseos
匹配来源:
文件名 :/usr/bin/rsync
rsync-3.1.3-13.el8.x86_64 : A program for synchronizing files over a network
仓库 :baseos
匹配来源:
文件名 :/usr/bin/rsync
// rsync服务端安装
[root@rsync ~]# yum -y install rsync
[root@rsync ~]# which rsync
/usr/bin/rsync
//客户端安装
[root@client ~]# yum -y install rsync
[root@client ~]# which rsync
/usr/bin/rsync
7 守护进程模式
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-tools工具;目标服务器端安装rsync即可
环境说明
主机 | IP |
---|---|
rsync | 192.168.25.147 |
client | 192.168.25.148 |
目标服务器
// 关闭防火墙与selinux
[root@client ~]# systemctl stop --now firewalld
[root@client ~]# setenforce 0
setenforce: SELinux is disabled
// 安装rsync服务
[root@client ~]# yum -y install rsync
// 编写rsyncd.conf配置文件
[root@client ~]# 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_client] # 自定义同步名称
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 = 172.16.12.128 # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.1.1 # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
EOF
// 设置用户认证密码文件
[root@client ~]# cat /etc/rsync.pass
admin:123456
// 设置文件权限
[root@client ~]# chmod 600 /etc/rsync*
[root@client ~]# ll /etc/rsync*
-rw------- 1 root root 423 10月 11 06:30 /etc/rsyncd.conf
-rw------- 1 root root 13 10月 11 06:31 /etc/rsync.pass
// 安装rsync-daemon包
[root@client ~]# yum list all | grep rsync
rsync.x86_64 3.1.3-13.el8 @baseos
libguestfs-rsync.x86_64 1:1.40.2-28.module_el8.5.0+821+97472045 appstream
rsync-daemon.noarch 3.1.3-13.el8 baseos
//启动rsync服务并设置开机自启动
[root@client ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 128 [::]:22 [::]:*
源服务器
// 关闭防火墙与SELINUX
[root@rsync ~]# systemctl stop --now firewalld
[root@rsync ~]# setenforce 0
// 安装提供inotify-tools的源
[root@rsync ~]# yum -y install epel-release
// 安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@rsync ~]# yum -y install rsync
//创建认证密码文件
[root@rsync ~]# echo '123456' > /etc/rsync.pass
[root@rsync ~]# cat /etc/rsync.pass
123456
//设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@rsync ~]# chmod 600 /etc/rsync.pass
// 在源服务器上创建测试目录,同步到目标服务器
[root@rsync ~]# mkdir -p /root/opt/AAA
[root@rsync ~]# ls opt/
AAA
[root@rsync ~]# rsync -avH --port 873 --progress --delete /root/opt/ admin@192.168.25.148::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_961-4248090753/
deleting vmware-root_947-4021653323/
deleting vmware-root_936-2697532681/
deleting vmware-root_930-2722763397/
deleting systemd-private-a24d4f65c58c4a8f8818a3b829e7fb01-chronyd.service-2indfl/tmp/
deleting systemd-private-a24d4f65c58c4a8f8818a3b829e7fb01-chronyd.service-2indfl/
deleting .font-unix/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
deleting ks-script-zgdhchtq
./
AAA/
sent 74 bytes received 401 bytes 316.67 bytes/sec
total size is 0 speedup is 0.00
[root@client ~]# ls /tmp/
AAA
安装inotify-tools工具,实时触发rsync进行同步
[root@rsync ~]# yum -y install inotify-tools
[root@rsync ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r--. 1 root root 0 10月 11 06:55 max_queued_events
-rw-r--r--. 1 root root 0 10月 11 06:55 max_user_instances
-rw-r--r--. 1 root root 0 10月 11 06:55 max_user_watches
如果有这三个max开头的文件则表示服务器内核支持inotify
//写同步脚本,让脚本自动去检测我们制定的目录下
[root@rsync ~]# mkdir /scripts
[root@rsync ~]# cd /scripts/
[root@rsync scripts]# touch inotify.sh
[root@rsync scripts]# chmod +x inotify.sh
[root@rsync scripts]# ll
总用量 0
-rwxr-xr-x. 1 root root 0 10月 11 07:01 inotify.sh
[root@rsync scripts]# vim inotify.sh
host=192.168.25.148 # 目标服务器的ip(备份服务器)
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@rsync ~]# nohup /scripts/inotify.sh &
[2] 2761
// 在源服务器上生成一个新文件,到目标服务上查看
[root@rsync ~]# touch /etc/AAA
[root@rsync ~]# ls /etc/AAA
/etc/AAA
[root@client ~]# ls /tmp/
etc
[root@client ~]# ls /tmp/etc/AAA
/tmp/etc/AAA
// 查看inotify生成的日志
[root@rsync ~]# tail /tmp/rsync.log
20211011 07:17 /etc/AAACREATE was rsynced
20211011 07:17 /etc/AAACREATE was rsynced
20211011 07:17 /etc/AAAATTRIB was rsynced
20211011 07:17 /etc/AAAATTRIB was rsynced
从日志上可以看到,我们生成了一个AAA文件,并且添加了内容到其里面
// 在原服务器上删除文件查看目标服务器是否同步删除
[root@rsync ~]# rm -rf /etc/AAA
[root@client ~]# ls /tmp/etc/AAA
ls: 无法访问'/tmp/etc/AAA': 没有那个文件或目录
设置脚本开机自动启动
[root@rsync ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 474 12月 1 2020 /etc/rc.d/rc.local
[root@rsync ~]# chmod +x /etc/rc.d/rc.local
[root@rsync ~]# echo 'nohup /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@rsync ~]# cat /etc/rc.d/rc.local
# 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 /scripts/inotify.sh
更多推荐
所有评论(0)