rsync

rsync


rsync简介

rsync官方网站: https://www.samba.org/ftp/rsync/rsync.html

rsync是可以实现增量备份的工具。配合任务计划,rsync能实现定时或间隔同步,配合inotify或sersync,可以实现触发式的实时同步。

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,可以在不同主机之间进行同步,可实现全量备份与增量备份,保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适合用于架构集中式备份或异地备份等应用。同时Rsync支持本地复制,或者与其他SSH、rsync主机同步。

Rsync特性和优点

  • 可以镜像保存整个目录树和文件系统。
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
  • 无须特殊权限即可安装。
  • 快速:第一次同步时 rsync 复制全部内容,但在下一次只传输修改过的文件。
  • 压缩传输:rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
  • 安全:可以使用scp、ssh等方式来传输文件。
  • 支持匿名传输,以方便进行网站镜象。
  • 选择性保持:符号连接,硬链接,文件属性,权限,时间等。

快速性:
第一次同步时 rsync 会将指定目录下全部内容复制同步,但在下一次只传输修改过的内容。

安全性:
可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
选择性保持:符号连接,硬链接,文件属性,权限,时间等。

压缩传输:
rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。

rsync与scp之间的区别:
scp无法备份大量数据,类似Windows的复制。而rsync边复制,边统计,边比较。

常见备份分类

  • 完整备份:每次备份都是从备份源将所有的文件或目录备份到目的地。

  • 差量备份:备份上次完全备份以后有变化的数据(他针对的上次的完全备份,他备份过程中不清除存档属性)。

  • 增量备份:备份上次备份以后有变化的数据(他才不管是那种类型的备份,有变化的数据就备份,他会清除存档属性)。

Rsync工作原理

  • 运行模式和端口:
    • 采用C/S模式(客户端/服务器模式)[ 实际上是一个点到点的传输,直接使用rsync命令即可完成 ]
    • rsync监听的端口:873
  • 四个名词的解释:
    • 发起端:负责发起rsync同步操作的客户机叫做发起端,通知服务器我要备份你的数据。
    • 备份源:负责响应来自客户机rsync同步操作的服务器叫做备份源,需要备份的服务器。
    • 服务端:运行rsyncd服务,一般来说,需要备份的服务器。
    • 客户端:存放备份数据。
数据同步方式
  • 推push:一台主机负责把数据传送给其他主机,服务器开销很大,比较适合后端服务器少的情况。

  • 拉pull:所有主机定时去找一台主机拉数据,可能就会导致数据缓慢。

  • 推:目的主机配置为rsync服务器,源主机周期性的使用rsync命令把要同步的目录推过去(需要备份的机器是客户端,存储备份的机器是服务端)。

  • 拉:源主机配置为rsync服务器,目的主机周期性的使用rsync命令把要同步的目录拉过来(需要备份的机器是服务端,存储备份的机器是客户端)。

两种方案,rsync都有对应的命令来实现。

rsync三种工作方式
rsync的语法:

Local:  rsync [OPTION...] SRC... [DEST]
 
Access via remote shell:
  Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
 
Access via rsync daemon:
  Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
        rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
        rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

(1).本地文件系统上实现同步。命令行语法格式为上述"Local"段的格式。

(2).本地主机使用远程shell和远程主机通信。命令行语法格式为上述"Access via remote shell"段的格式。

(3).本地主机通过网络套接字连接远程主机上的rsync daemon。命令行语法格式为上述"Access via rsync daemon"段的格式。

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.188.128:/DEST 
    -a  //文件宿主变化,时间戳不变
    -z  //压缩数据传输
 
//当遇到要修改端口的时候,我们可以:
rsync -avz /SRC -e "ssh -p1234" root@192.168.188.128:/DEST  
//修改了ssh 协议的端口,默认是22

rsync命令与基本用法

安装rsync

//服务端下载
[root@server ~]# dnf -y install rsync
//客户端下载
[root@client ~]# dnf -y install rsync
//Rsync的命令格式常用的有以下三种:
    rsync [OPTION]... SRC DEST  //本地
    rsync [OPTION]... SRC [USER@]HOST:DEST  //远程推
    rsync [OPTION]... [USER@]HOST:SRC DEST  //远程拉
    
-v:显示rsync过程中详细信息。可以使用"-vvvv"获取更详细信息。
-P:显示文件传输的进度信息。(实际上"-P"="--partial --progress",其中的"--progress"才是显示进度信息的)。
-n --dry-run  :仅测试传输,而不实际传输。常和"-vvvv"配合使用来查看rsync是如何工作的。
-a --archive  :归档模式,表示递归传输并保持文件属性。等同于"-rtopgDl"。
-r --recursive:递归到目录中去。
-t --times:保持mtime属性。强烈建议任何时候都加上"-t",否则目标文件mtime会设置为系统时间,导致下次更新
          :检查出mtime不同从而导致增量传输无效。
-o --owner:保持owner属性(属主)。
-g --group:保持group属性(属组)。
-p --perms:保持perms属性(权限,不包括特殊权限)。
-D        :是"--device --specials"选项的组合,即也拷贝设备文件和特殊文件。
-l --links:如果文件是软链接文件,则拷贝软链接本身而非软链接所指向的对象。
-z        :传输时进行压缩提高效率。
-R --relative:使用相对路径。意味着将命令行中指定的全路径而非路径最尾部的文件名发送给服务端,包括它们的属性。用法见下文示例。
--size-only :默认算法是检查文件大小和mtime不同的文件,使用此选项将只检查文件大小。
-u --update :仅在源mtime比目标已存在文件的mtime新时才拷贝。注意,该选项是接收端判断的,不会影响删除行为。
-d --dirs   :以不递归的方式拷贝目录本身。默认递归时,如果源为"dir1/file1",则不会拷贝dir1目录,使用该选项将拷贝dir1但不拷贝file1。
--max-size  :限制rsync传输的最大文件大小。可以使用单位后缀,还可以是一个小数值(例如:"--max-size=1.5m")
--min-size  :限制rsync传输的最小文件大小。这可以用于禁止传输小文件或那些垃圾文件。
--exclude   :指定排除规则来排除不需要传输的文件。
--delete    :以SRC为主,对DEST进行同步。多则删之,少则补之。注意"--delete"是在接收端执行的,所以它是在
            :exclude/include规则生效之后才执行的。
-b --backup :对目标上已存在的文件做一个备份,备份的文件名后默认使用"~"做后缀。
--backup-dir:指定备份文件的保存路径。不指定时默认和待备份文件保存在同一目录下。
-e          :指定所要使用的远程shell程序,默认为ssh。
--port      :连接daemon时使用的端口号,默认为873端口。
--password-file:daemon模式时的密码文件,可以从中读取密码实现非交互式。注意,这不是远程shell认证的密码,而是rsync模块认证的密码。
-W --whole-file:rsync将不再使用增量传输,而是全量传输。在网络带宽高于磁盘带宽时,该选项比增量传输更高效。
--existing  :要求只更新目标端已存在的文件,目标端还不存在的文件不传输。注意,使用相对路径时如果上层目录不存在也不会传输。
--ignore-existing:要求只更新目标端不存在的文件。和"--existing"结合使用有特殊功能,见下文示例。
--remove-source-files:要求删除源端已经成功传输的文件。
本地用法
//创建一个def目录和一个1文件,将文件1归档到def
[root@server abc]# touch 1
[root@server abc]# mkdir def
[root@server abc]# rsync -avz 1 def/
sending incremental file list
1

sent 90 bytes  received 35 bytes  250.00 bytes/sec
total size is 0  speedup is 0.00
[root@server abc]# ls def/
1

//将目录def的内容复制到hij里面
[root@server abc]# cd def/
[root@server def]# ls
1
[root@server def]# touch 2 3
[root@server def]# ls
1  2  3
[root@server def]# cd ..
[root@server abc]# mkdir hij
[root@server abc]# rsync -avz def/ hij/
sending incremental file list
./
1
2
3

sent 213 bytes  received 76 bytes  578.00 bytes/sec
total size is 0  speedup is 0.00
[root@server abc]# ls hij/
1  2  3

//备份目录时,使目标目录与源目录数据保持一致;若目标目录有多余的文件则会被删除
[root@server abc]# rm -rf def/3
[root@server abc]# ls def/
1  2
[root@server abc]# rsync -avz --delete def/ hij/
sending incremental file list
deleting 3
./
1
2

sent 161 bytes  received 62 bytes  446.00 bytes/sec
total size is 0  speedup is 0.00
[root@server abc]# ls def/
1  2
[root@server abc]# ls hij/
1  2

.

远程用法
//将本地文件备份到其他主机
[root@server ~]# rsync -avz abc/ root@192.168.188.133:/123
The authenticity of host '192.168.188.133 (192.168.188.133)' can't be established.
ED25519 key fingerprint is SHA256:t3Eg2jjRaYpIFSxn6UKQy7n/VlrLbslGZFxljYJb/QA.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.188.133' (ED25519) to the list of known hosts.
root@192.168.188.133's password:
sending incremental file list
./
1
def/
def/1
def/2
hij/
hij/1
hij/2

sent 386 bytes  received 130 bytes  33.29 bytes/sec
total size is 0  speedup is 0.00
//客户机查看
[root@client ~]# ls /123/
1  def  hij

//将其他主机的内容远程备份到本地
[root@server ~]# rsync -avz  root@192.168.188.133:/root/wdnmd .
root@192.168.188.133's password:
receiving incremental file list
wdnmd/
wdnmd/1
wdnmd/2
wdnmd/3
wdnmd/4
wdnmd/5

sent 123 bytes  received 315 bytes  175.20 bytes/sec
total size is 0  speedup is 0.00
[root@server ~]# ls wdnmd/
1  2  3  4  5

rsync+inotify

Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。

环境说明:

服务器类型IP地址应用操作系统
源服务器192.168.188.128rsync
inotify-tools
脚本
Linux centos8
目标服务器192.168.188.129rsyncLinux centos8

实验要求

  • 把源服务器上/etc目录实时同步到目标服务器的/tmp/下
在目标服务器上做以下操作:
//关闭防火墙与SELINUX
[root@client ~]# systemctl disable --now firewalld
[root@client ~]# setenforce 0
setenforce: SELinux is disabled
[root@client ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux

//安装rsync服务端软件
[root@client ~]# dnf -y install rsync rsync-daemon

//设置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.passwd  # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
>
> [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.188.128  # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
> hosts deny = 192.168.188.132  # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
> EOF


//创建用户认证文件
[root@client ~]# echo 'admin:123456' > /etc/rsync.passwd
[root@client ~]# cat /etc/rsync.passwd
admin:123456

//设置文件权限
[root@client ~]# chmod 600 /etc/rsync.passwd
[root@client ~]# ll /etc/rsync.passwd
-rw------- 1 root root 13 Sep 25 14:24 /etc/rsync.passwd

//启动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 -anlt
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@server ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@server ~]# setenforce 0
[root@server ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux

//安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@server ~]# dnf -y install rsync

//创建认证密码文件
[root@server ~]# echo '123456' > /etc/rsync.pass
[root@server ~]# cat /etc/rsync.pass
123456

//设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@server ~]# echo '123456' > /etc/rsync.passwd
[root@server ~]# chmod 600 /etc/rsync.passwd
[root@server ~]# cat /etc/rsync.passwd
123456
[root@server ~]# ll /etc/rsync.passwd
-rw-------. 1 root root 7 Sep 25 14:29 /etc/rsync.passwd

//在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@server ~]# mkdir -p /root/etc/test
[root@server ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.188.129::etc_from_client --password-file=/etc/rsync.passwd
sending incremental file list
./
test/

sent 77 bytes  received 27 bytes  208.00 bytes/sec
total size is 0  speedup is 0.00

#在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
[root@client ~]# ls /tmp/
test

#安装inotify-tools工具
[root@server ~]# dnf -y install epel-release
[root@server ~]# dnf -y install inotify-tools

#写同步脚本
[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 Sep 25 14:34 /scripts/inotify.sh
[root@server ~]# vim /scripts/inotify.sh
host=192.168.188.129      # 目标服务器的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@server ~]# nohup bash /scripts/inotify.sh &
[1] 10871
[root@server ~]# nohup: ignoring input and appending output to 'nohup.out'

[root@server ~]# ps -aux | grep inotify
root       10871  0.0  0.3  12724  3064 pts/0    S    14:39   0:00 bash /scripts/inotify.sh
root       10872  0.0  0.2   6636  1756 pts/0    S    14:39   0:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root       10873  0.0  0.0  12724   264 pts/0    S    14:39   0:00 bash /scripts/inotify.sh
root       10875  0.0  0.1  12136  1100 pts/0    R+   14:39   0:00 grep --color=auto inotify

#在源服务器创建文件测试
[root@server ~]# touch /etc/zhang
[root@server ~]# echo 'hello,world!' > /etc/zhang

#查看inotify生成的日志
[root@server ~]# tail /tmp/rsync.log
20220925 14:42 /etc/zhangCREATE was rsynced
20220925 14:42 /etc/zhangATTRIB was rsynced
20220925 14:42 /etc/zhangMODIFY was rsynced

#在目标服务器查看文件是否同步
[root@client ~]# cat /tmp/etc/zhang
hello,world!

//配置脚本开机自启

[root@server ~]# chmod +x /etc/rc.d/rc.local
[root@server ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 474 Oct  5  2021 /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
#
# 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

//到目标服务器上去查看是否把新生成的文件自动传上去了:

[root@client tmp]# pwd
/tmp
[root@client tmp]# ls
etc  test

[root@client tmp]# cat etc/zhang
hello,world!
 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

//到目标服务器上去查看是否把新生成的文件自动传上去了:

[root@client tmp]# pwd
/tmp
[root@client tmp]# ls
etc  test

[root@client tmp]# cat etc/zhang
hello,world!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随便投投

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值