rsync服务
1. rsync简介
rsync 是linux
系统下一个功能强大的数据镜像备份工具,用于在不同的系统之间进行文件备份和传输。它可以在本地和远程主机之间同步文件和目录,并且仅传输已更改的部分,从而减少传输的时间和带宽消耗。rsync 支持通过 SSH 和 rsh 协议进行安全传输,并具有强大的可定制性和灵活性。
2. rsync特性
rsync
支持很多特性:
- 可以镜像保存整个目录树和文件系统
- 可以很容易做到保持原来文件的权限、时间、软硬链接等等
- 无须特殊权限即可安装
- 快速:第一次同步时
rsync
会复制全部内容,但在下一次只传输修改过的文件。rsync
在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽 - 安全:可以使用
scp
、ssh
等方式来传输文件,当然也可以通过直接的socket
连接 - 支持匿名传输,以方便进行网站镜像
3.rsync 的主要特点包括:
- 增量传输:rsync 仅传输已更改的文件部分,而不是整个文件,以提高传输效率。
- 本地和远程传输:可以在本地计算机之间或本地计算机与远程计算机之间进行文件传输。
- 支持和保持文件权限、时间戳、链接等元数据的同步。
- 支持压缩传输,可以降低传输数据量,提高传输速度。
- 支持通过 SSH 进行加密传输,确保数据安全。
- 支持支持自定义筛选规则,选择性地同步文件和目录。
- 支持增量备份和实现备份策略的灵活配置。
- 在网络中断或传输中断的情况下,可以重新启动传输而不会重复已完成的部分。
4. 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@172.16.12.129:/DEST
-a //文件宿主变化,时间戳不变
-z //压缩数据传输
//免密登录
[root@CentOS8 ~]# ssh-keygen -t rsa
回车
[root@CentOS8 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.195.137
5.rsync命令
//Rsync的命令格式常用的有以下三种:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
//对应于以上三种命令格式,rsync有三种不同的工作模式:
1)拷贝本地文件。
[root@CentOS8 ansible]# ls
ansible.cfg hosts_inventory roles
[root@CentOS8 ansible]# rsync hosts_inventory host
[root@CentOS8 ansible]# ll
total 28
-rw-r--r--. 1 root root 20022 Jul 28 11:30 ansible.cfg
-rw-r--r-- 1 root root 31 Sep 30 12:40 host
-rw-r--r--. 1 root root 31 Jul 28 11:13 hosts_inventory
drwxr-xr-x. 2 root root 6 Jul 28 11:12 roles
2)将本地机器的内容拷贝到远程机器。
[root@CentOS8 ansible]# rsync -avz host root@192.168.195.137:/root/inventory
sending incremental file list
host
sent 111 bytes received 35 bytes 292.00 bytes/sec
total size is 31 speedup is 0.21
[root@centos2 ~]# ls
anaconda-ks.cfg inventory
3)将远程机器的内容拷贝到本地机器。
[root@CentOS8 ~]# ls
anaconda-ks.cfg ansible apache.sh lamp lamp.tar.gz nohup.out
[root@CentOS8 ~]# rsync -avz root@192.168.195.137:/root/inventory /root/
receiving incremental file list
inventory
sent 43 bytes received 116 bytes 318.00 bytes/sec
total size is 31 speedup is 0.19
[root@CentOS8 ~]# ls
anaconda-ks.cfg ansible apache.sh inventory lamp lamp.tar.gz nohup.out
注:此用法相对用的较少,更多的用法是和inotify一起使用,以上三种实现效果更多的是用scp命令来实现
//rsync常用选项:
-a, --archive //归档
-v, --verbose //显示过程
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步
6. 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
同步,这样刚好解决了同步数据的实时性问题。
环境说明:
服务器类型 | IP地址 | 应用 | 操作系统 |
---|---|---|---|
源服务器 | 172.16.12.128 | rsync inotify-tools 脚本 | centos8/redhat8 |
目标服务器 | 172.16.12.129 | rsync | centos8/redhat8 |
配置rsync服务时需先将免密登录设置好
[root@CentOS8 ~]# ssh-keygen -t rsa
回车
[root@CentOS8 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.195.137
需求:
- 把源服务器上/etc目录实时同步到目标服务器的/tmp/下
配置目标服务器:
//关闭防火墙和selinux
[root@centos2 ~]# systemctl stop firewalld.service
[root@centos2 ~]# vim /etc/selinux/config
[root@centos2 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
//配置yum源,选择阿里源CentOS8的源
[root@centos2 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
//安装rsync服务的包
[root@centos2 ~]# yum -y install rsync
[root@centos2 ~]# which rsync
/usr/bin/rsync
//安装rsync守护进程的软件包,在centos8和redhat8的系统中,rsync的配置文件在此包里面,若是7的版本则不用
[root@centos2 ~]# yum -y install rsync-daemon.noarch
//设置rsyncd.conf配置文件,在配置文件后面加上这十九行
[root@centos2 ~]# vim /etc/rsyncd.conf
[root@centos2 ~]# tail -19 /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 # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
[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 = 192.168.195.136 # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
//创建用户认证文件,并设置权限,不能让其他人看到
[root@centos2 ~]# vim /etc/rsync.pass
[root@centos2 ~]# cat /etc/rsync.pass
admin:123456
[root@centos2 ~]# chmod 600 /etc/rsync.pass
[root@centos2 ~]# ll /etc/rsync.pass
-rw-------. 1 root root 13 Sep 29 01:11 /etc/rsync.pass
//启动rsync服务,查看端口号873是否开启
[root@centos2 ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@centos2 ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2023-09-29 01:12:56 CST; 5s ago
Main PID: 91988 (rsync)
Tasks: 1 (limit: 11339)
Memory: 272.0K
CGroup: /system.slice/rsyncd.service
└─91988 /usr/bin/rsync --daemon --no-detach
Sep 29 01:12:56 CentOS8 systemd[1]: Started fast remote file copy program daemon.
[root@centos2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 80 *:3306 *:*
配置源服务器:
//关闭防火墙和selinux
[root@CentOS8 ~]# systemctl stop firewalld.service
[root@CentOS8 ~]# vim /etc/selinux/config
[root@CentOS8 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
//配置yum源,选择阿里源CentOS8的源
[root@CentOS8 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
//安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@CentOS8 ~]# yum -y install rsync
//创建认证密码文件,源主机上面只放密码,不放用户
[root@CentOS8 ~]# vim /etc/rsync.pass
[root@CentOS8 ~]# cat /etc/rsync.pass
123456
//设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@CentOS8 ~]# chmod 600 /etc/rsync.pass
[root@CentOS8 ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 Sep 29 01:24 /etc/rsync.pass
//在源服务器上测试,运行以下命令
[root@CentOS8 ~]# rsync -avH --port 873 --progress --delete /root/lamp admin@192.168.195.137::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
lamp/
lamp/.lamp-version.sh.swp
16,384 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=11/13)
lamp/lamp-version.sh
11,664 100% 11.12MB/s 0:00:00 (xfr#2, to-chk=10/13)
lamp/files/
lamp/files/apr-1.7.4.tar.gz
1,122,147 100% 89.18MB/s 0:00:00 (xfr#3, to-chk=8/13)
lamp/files/apr-util-1.6.3.tar.gz
556,623 100% 31.23MB/s 0:00:00 (xfr#4, to-chk=7/13)
lamp/files/httpd-2.4.56.tar.gz
9,769,650 100% 133.10MB/s 0:00:00 (xfr#5, to-chk=6/13)
lamp/files/httpd-2.4.57.tar.gz
9,773,385 100% 74.57MB/s 0:00:00 (xfr#6, to-chk=5/13)
lamp/files/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
676,676,298 100% 165.51MB/s 0:00:03 (xfr#7, to-chk=4/13)
lamp/files/oniguruma-6.9.4.tar.gz
582,597 100% 629.36kB/s 0:00:00 (xfr#8, to-chk=3/13)
lamp/files/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
48,540 100% 52.38kB/s 0:00:00 (xfr#9, to-chk=2/13)
lamp/files/php-7.4.33.tar.gz
16,869,345 100% 16.02MB/s 0:00:01 (xfr#10, to-chk=1/13)
lamp/files/php-8.2.10.tar.gz
19,104,774 100% 180.39MB/s 0:00:00 (xfr#11, to-chk=0/13)
sent 734,711,634 bytes received 245 bytes 163,269,306.44 bytes/sec
total size is 734,531,407 speedup is 1.00
//运行完成后,在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
//在目标机上查看/tmp目录
[root@centos2 ~]# ls /tmp
lamp
[root@centos2 ~]# ls /tmp/lamp/
files lamp-version.sh abc
//此时我们在源主机上删除lamp目录中的files目录中的abc文件,测试目标主机中是否也将abc文件删除
[root@CentOS8 ~]# rm -f lamp/abc
[root@CentOS8 ~]# ls lamp/
files lamp-version.sh
[root@CentOS8 ~]# rsync -avH --port 873 --progress --delete /root/lamp admin@192.168.195.137::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting lamp/abc
lamp/
sent 463 bytes received 37 bytes 333.33 bytes/sec
total size is 734,531,407 speedup is 1,469,062.81
//查看目标主机的lamp目录
[root@centos2 ~]# ls /tmp/lamp/
files lamp-version.sh
6.1编写脚本,使目录、文件能够实时同步
//安装inotify-tools工具,实时触发rsync进行同步
//查看服务器内核是否支持inotify
[root@CentOS8 ~]# ll /proc/sys/fs/inotify
total 0
-rw-r--r--. 1 root root 0 Sep 30 00:14 max_queued_events
-rw-r--r--. 1 root root 0 Sep 30 00:14 max_user_instances
-rw-r--r--. 1 root root 0 Sep 30 00:14 max_user_watches
//如果有这三个max开头的文件则表示服务器内核支持inotify
//安装inotify-tools
[root@CentOS8 ~]# yum -y install epel-release
[root@CentOS8 ~]# yum -y install inotify-tools
//写同步脚本,让脚本自动去检测我们制定的目录下,文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@CentOS8 ~]# mkdir /scripts
[root@CentOS8 ~]# cd /scripts
[root@CentOS8 scripts]# touch inotify.sh
[root@CentOS8 scripts]# chmod +x inotify.sh
[root@CentOS8 scripts]# vim inotify.sh
[root@CentOS8 scripts]# cat inotify.sh
#!/bin/bash
host=192.168.195.137 # 目标服务器的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
//启动脚本,将脚本放在后台运行,加上nohup,为了让终端关闭后重新开启一个终端能够让脚本自动运行
[root@CentOS8 scripts]# nohup bash inotify.sh &
[1] 2070
[root@CentOS8 scripts]# nohup: ignoring input and appending output to 'nohup.out'
[root@CentOS8 scripts]# jobs
[1]+ Running nohup bash inotify.sh &
//查看inotify进程
[root@CentOS8 scripts]# ps -ef | grep inotify
root 2070 1579 0 00:43 pts/0 00:00:00 bash inotify.sh
root 2071 2070 0 00:43 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 2072 2070 0 00:43 pts/0 00:00:00 bash inotify.sh
root 2075 1579 0 00:45 pts/0 00:00:00 grep --color=auto inotify
//在源服务器上生成一个新文件
[root@CentOS8 ~]# touch /etc/abc
[root@CentOS8 ~]# ls /etc/abc
/etc/abc
[root@CentOS8 ~]# echo 'hello world' > /etc/abc
//在目标主机上查看
[root@centos2 ~]# ls /tmp/etc/abc
/tmp/etc/abc
[root@centos2 ~]# cat /tmp/etc/abc
hello world
//关闭终端后重新打开,查看脚本进程是否在运行
[root@CentOS8 ~]# ps -ef | grep inotify
root 921 1 0 01:07 ? 00:00:00 /bin/bash /scripts/inotify.sh
root 925 921 0 01:07 ? 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 926 921 0 01:07 ? 00:00:00 /bin/bash /scripts/inotify.sh
root 1555 1519 0 01:08 pts/0 00:00:00 grep --color=auto inotify
//再次测试,向先前创建的/etc/abc文件中添加一行内容
[root@CentOS8 ~]# echo 'ftx' >> /etc/abc
[root@CentOS8 ~]# cat /etc/abc
hello world
ftx
//进入目标机查看
[root@centos2 ~]# cat /tmp/etc/abc
hello world
ftx
6.3设置脚本开机自启
//此文件是系统启动后执行的最后一个文件,我们将我们执行脚本的命令放在这个文件中,并赋予该文件执行权限即可
[root@CentOS8 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Nov 9 2019 /etc/rc.local -> rc.d/rc.local
[root@CentOS8 ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 474 Nov 9 2019 /etc/rc.d/rc.local
[root@CentOS8 ~]# chmod +x /etc/rc.d/rc.local
[root@CentOS8 ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 474 Nov 9 2019 /etc/rc.d/rc.local
[root@CentOS8 ~]# vim /etc/rc.d/rc.local
[root@CentOS8 ~]# head /etc/rc.d/rc.local
#!/bin/bash
nohup /scripts/inotify.sh &
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# 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.
#
//重启系统,查看脚本是否还在运行
[root@CentOS8 ~]# reboot
[root@CentOS8 ~]# ps -ef | grep inotify
root 915 1 0 11:30 ? 00:00:00 /bin/bash /scripts/inotify.sh
root 920 915 0 11:30 ? 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 921 915 0 11:30 ? 00:00:00 /bin/bash /scripts/inotify.sh
root 1499 1477 0 11:31 pts/0 00:00:00 grep --color=auto inotify
[root@CentOS8 ~]# echo 'hello' >> /etc/abc
[root@CentOS8 ~]# cat /etc/abc
hello world
ftx
hello
//查看目标机的/tmp/etc/abc文件
[root@centos2 ~]# cat /tmp/etc/abc
hello world
ftx
hello
配置至此,说明我们的服务已经配置完成
总体而言,rsync 是一个非常实用的文件同步工具,适用于各种场景,包括有限带宽的网络环境、大量文件的备份和传输、跨网络的文件同步等。