Rsync

0. Rsync概述

rsync是一款开源、快速、多功能、可实现全量及增量的本地或远程数据同步份的优秀工具。rsync软件适用于Unix/linux/Windows等多种操作系统平台。

1. Rsync简介

rsync英文称为remote synchronization,从软件的名称就可以看出来,rsync具有可使本地和远程两台主机之间的数据快速复制同步镜像、远程备份的功能,这个功能类似于ssh带的scp命令,但是又优于scp命令的功能,scp每次都是全量拷贝,而rsync可以增量拷贝。当然,rsync还可以在本地主机的不同分区或目录之间全量及曾量的复制数据,这又类似cp命令。但是同样也优于cp命令,cp每次都是全量拷贝,而rsync可以增量拷贝。在同步数据的时候,默认情况下,rsync通过其独特的"quick check"算法,它仅同步大小或者最后修改时间发生变化的文件或目录,当然也可根据权限、属主等属性的变化同步,但是需要制定相应的参数,甚至可以实现只同步一个文件里有变化的内容部分,所以,可是实现快速的同步备份数据。
rsync官方地址:TP
rsync监听端口:873
rsync运行模式:C/S

特点:

1.Rsync可以实现增量的备份(主机和主机之间的数据传输)
2.CS/BS架构
Client/Server 客户端和服务端 类似客户端的游戏 王者荣耀 吃鸡 英雄联盟
Browse/Server 浏览器和服务端 类似网页游戏 使用浏览器访问的服务都是BS架构
3.Rsync默认监听端口873
4.全量备份和增量备份
全量备份优缺点: 占用带宽 磁盘IO 占用磁盘空间 数据安全不易丢失
增量备份优缺点: 速度快 占用较少的磁盘IO 节省磁盘空间 数据易丢失(不完整)

2. Rsync的传输模式

注意: rsync传输目录时,目录加 / 表示目录下面所有文件不包含目录本身 , 不加/表示目录本身及目录下的所有文件

2.1 本地模式

复制文件到指定目录

[root@backup:~]#rsync -avz 1.txt /opt
sending incremental file list
1.txt

sent 84 bytes  received 35 bytes  238.00 bytes/sec
total size is 0  speedup is 0.00
[root@backup:~]#ll /opt 
total 0
-rw-r--r-- 1 root root  0 Mar  4 12:14 1.txt
增量备份:重新执行一次rsync的拷贝动作发现1.txt不会再拷贝了
[root@backup:~]#rsync -avz 1.txt /opt
sending incremental file list

sent 44 bytes  received 12 bytes  112.00 bytes/sec
total size is 0  speedup is 0.00
[root@backup:~]#

2.2 远程模式

2.1.0 语法格式

例子: rsync -avz 文件名 root@172.16.1.7:/root/
rsync: 命令
-avz: 参数
root: 远端主机的用户名(如果不写,以当前主机的用户名访问远端主机)
@: 间隔符
172.16.1.7: 可以是远端主机的IP地址、hosts文件中的主机名、远端主机的域名
/root/: 远端主机的root目录

2.1.1 远程推送

上传文件: 推送 push  网盘上传
rsync -avz 文件名 root@172.16.1.7:/root/

2.2.2 远程拉取

下载文件: 拉取 pull  网盘下载
rsync -avz root@172.16.1.7:/etc/hosts ./

2.2.3 案例

1.将backup主机上的2.txt传输到远端主机的root的家目录

[root@backup:~]#rsync -avz 2.txt root@172.16.1.7:/root/
The authenticity of host '172.16.1.7 (172.16.1.7)' can't be established.
ECDSA key fingerprint is SHA256:eCAGUdcaRh+6goGKg2Z82sveMXjtwrEcuu4m/L5WKjY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts.

Authorized users only. All activities may be monitored and reported.
root@172.16.1.7's password:  # 输入172.16.1.7主机下的root密码
sending incremental file list
2.txt

sent 83 bytes  received 35 bytes  15.73 bytes/sec
total size is 0  speedup is 0.00
[root@backup:~]#

到172.16.1.7主机上成功查看到拷贝文件

2. 将远端主机的文件拷贝到本地当前目录

[root@backup:~]#rsync -avz 172.16.1.7:/root/test.txt ./

Authorized users only. All activities may be monitored and reported.
root@172.16.1.7's password: # 输入172.16.1.7主机下的root密码
receiving incremental file list
test.txt

sent 43 bytes  received 87 bytes  23.64 bytes/sec
total size is 0  speedup is 0.00
[root@backup:~]#ll
-rw-r--r-- 1 root root    0 Apr  6 12:02 test.txt
[root@backup:~]#

3.拷贝目录

1.当拷贝的目录不加 / 时,会将目录及目录下所有文件全部拷贝
[root@backup:~]#ls young/
1.txt  2.txt  3.txt  4.txt  5.txt
[root@backup:~]#rsync -avz young 172.16.1.7:/root/
Authorized users only. All activities may be monitored and reported.
root@172.16.1.7's password: 
sending incremental file list
young/
young/1.txt
young/2.txt
young/3.txt
young/4.txt
young/5.txt

sent 329 bytes  received 115 bytes  80.73 bytes/sec
total size is 0  speedup is 0.00
[root@backup:~]#

在172.16.1.7主机的root目录下查看
[root@web01:~]#ll
total 0
drwxr-xr-x 2 root root 71 Apr  6 12:08 young
[root@web01:~]#ls young/
1.txt  2.txt  3.txt  4.txt  5.txt
[root@web01:~]#

2.当拷贝的目录加 / 时,仅将目录下的所有文件全部拷贝
[root@backup:~]#ls young2
1.txt  2.txt  3.txt  4.txt  5.txt
[root@backup:~]#rsync -avz young2/ 172.16.1.7:/root/
Authorized users only. All activities may be monitored and reported.
root@172.16.1.7's password: 
sending incremental file list
./
1.txt
2.txt
3.txt
4.txt
5.txt

sent 305 bytes  received 114 bytes  64.46 bytes/sec
total size is 0  speedup is 0.00
在172.16.1.7主机的root目录下查看
[root@web01:~]#ls
1.txt  2.txt  3.txt  4.txt  5.txt  young
[root@web01:~]#

3. Rsync服务

相关文件:

/etc/rsyncd.conf # rsync服务的配置文件

1.安装rsync服务 41备份服务器部署
[root@backup:~]#yum -y install rsync

2.配置rsync服务
[root@backup:~]#cat /etc/rsyncd.conf
uid = rsync # 运行进程的用户
gid = rsync # 运行进程的用户组
port = 873  # 监听端口
fake super = yes           # 无需让rsync以root身份运行,允许接收文件的完整属性
use chroot = no            # 禁锢推送的数据至某个目录,不允许跳出该目录
max connections = 200      # 最大连接数
timeout = 600              # 超时时间
ignore errors              # 忽略错误信息
read only = false          # 对备份数据可读写
list = false               # 不允许查看模块信息
auth users = rsync_backup  # 定义虚拟用户,作为连接认证用户
secrets file = /etc/rsync.passwd  # 定义rsync服务用户连接认证密码文件路径
log file = /var/log/rsyncd.log
#####################################
[backup]                   # 定义模块名
comment = welcome to oldboyedu backup!  # 模块注释信息
path = /backup             # 定义接受备份数据目录

---------------修改配置文件需要重启服务
[root@backup:~]#systemctl restart rsyncd

3.根据配置创建必要的信息
1)创建用户rsync
[root@backup:~]#useradd -M -s /sbin/nologin rsync
[root@backup:~]#id rsync
uid=1000(rsync) gid=1000(rsync) groups=1000(rsync)
2)配置用户名和密码写入到密码文件中
[root@backup:~]#echo rsync_backup:123456 > /etc/rsync.passwd
[root@backup:~]#cat /etc/rsync.passwd
rsync_backup:123456
修改密码文件权限为600
[root@backup:~]#chmod 600 /etc/rsync.passwd
[root@backup:~]#ll /etc/rsync.passwd
-rw------- 1 root root 20 Jul 31 10:25 /etc/rsync.passwd
3)创建目录
[root@backup:~]#mkdir /backup
更改目录属主属组为rsync用户
[root@backup:~]#chown rsync.rsync /backup/ 
[root@backup:~]#ll -d /backup/
drwxr-xr-x 2 rsync rsync 6 Apr  6 12:24 /backup/
[root@backup:~]#

4.启动rsync服务
[root@backup:~]#systemctl start rsyncd
[root@backup:~]#systemctl enable rsyncd
检查端口是否启动
[root@backup:~]#netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address        
tcp        0      0 0.0.0.0:22              0.0.0.0:*              
tcp        0      0 0.0.0.0:873             0.0.0.0:*              
tcp6       0      0 :::22                   :::*                   
tcp6       0      0 :::873                  :::*                   
udp        0      0 127.0.0.1:323           0.0.0.0:*              
udp6       0      0 ::1:323                 :::*                   
[root@backup:~]#

5.测试rsync服务
客户端测试:
推送:语法格式 最后两个冒号,后面是模块的名称而不是路径。
rsync -avz  文件名 rsync_backup@172.16.1.41::backup
[root@web01:~]#touch test1.txt
[root@web01:~]#rsync -avz test1.txt rsync_backup@172.16.1.41::backup
Password: 
sending incremental file list
test1.txt

sent 92 bytes  received 43 bytes  30.00 bytes/sec
total size is 0  speedup is 0.00
[root@web01:~]#
下载: 从backup服务器中下载文件
[root@web01:~]#rsync -avz rsync_backup@172.16.1.41::backup/hosts ./
Password: 
receiving incremental file list
hosts

sent 43 bytes  received 150 bytes  55.14 bytes/sec
total size is 158  speedup is 0.82
[root@web01:~]#ll
total 4
-rw-r--r-- 1 root root 158 Apr  6 12:31 hosts
-------------------------------------------------------------------------
若是拷贝模块下的目录,则:
当拷贝的目录不加 / 时,会将目录及目录下所有文件全部拷贝
当拷贝的目录加 / 时,仅将目录下的所有文件全部拷贝
-------------------------------------------------------------------------

4. Rsync参数

4.1 --exclude

rsync排除推送的文件 --exclude
[root@backup:~]#touch {1..3}.txt
[root@backup:~]#ll
total 0
-rw-r--r-- 1 root root 0 Apr  6 12:41 1.txt
-rw-r--r-- 1 root root 0 Apr  6 12:41 2.txt
-rw-r--r-- 1 root root 0 Apr  6 12:41 3.txt
[root@backup:~]#rsync -avz *.txt --exclude=1.txt root@172.16.1.7:/root/
Authorized users only. All activities may be monitored and reported.
root@172.16.1.7's password: 
sending incremental file list
2.txt
3.txt
sent 136 bytes  received 54 bytes  34.55 bytes/sec
total size is 0  speedup is 0.00
[root@backup:~]#

4.2 --exclude-from=file

排除多个文件使用 --exclude-from=file
[root@backup:~]#cat 1.log 
1.txt
3.txt
[root@backup:~]#ll
total 4
-rw-r--r-- 1 root root 12 Apr  6 12:44 1.log
-rw-r--r-- 1 root root  0 Apr  6 12:41 1.txt
-rw-r--r-- 1 root root  0 Apr  6 12:41 2.txt
-rw-r--r-- 1 root root  0 Apr  6 12:41 3.txt
[root@backup:~]#rsync -avz *.txt --exclude-from=/root/1.log root@172.16.1.7:/root
Authorized users only. All activities may be monitored and reported.
root@172.16.1.7's password: 
Permission denied, please try again.
root@172.16.1.7's password: 
sending incremental file list
2.txt
sent 84 bytes  received 35 bytes  8.21 bytes/sec
total size is 0  speedup is 0.00
[root@backup:~]#
在172.16.1.7的主机上查看
[root@web01:~]#ll
total 0
-rw-r--r-- 1 root root 0 Apr  6 12:41 2.txt
[root@web01:~]#

4.3 --bwlimit

使用限速功能传输文件 --bwlimit=1M  默认kb
[root@backup:~]#rsync -avzP --bwlimit=1M 04.mp4 web01:/root/
Authorized users only. All activities may be monitored and reported.
root@web01's password: 
sending incremental file list
04.mp4
     15,302,656  13%    1.12MB/s    0:01:28 

4.4 --delete

--delete 两端数据一致,文件名谁在前,就以谁为准
1)服务器有啥,客户端必须和服务器一样
第一步: 41
[root@backup:~]#ll young/
total 4
-rw-r--r-- 1 root root   0 Apr  6 12:51 1.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 2.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 3.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 4.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 5.txt
-rw-r--r-- 1 root root 158 Apr  6 12:51 hosts
第二步: web01在/root/目录下创建文件
[root@web01:~]#touch {6..10}.txt
root@web01:~]#ll
total 0
-rw-r--r-- 1 root root 0 Apr  6 12:52 10.txt
-rw-r--r-- 1 root root 0 Apr  6 12:52 6.txt
-rw-r--r-- 1 root root 0 Apr  6 12:52 7.txt
-rw-r--r-- 1 root root 0 Apr  6 12:52 8.txt
-rw-r--r-- 1 root root 0 Apr  6 12:52 9.txt


最后执行同步的命令: 结果是web01新创建的文件被删除。
[root@backup:~]#rsync -avz --delete young/ 172.16.1.7:/root/
在172.16.1.7主机上查看
[root@web01:~]#ll
total 4
-rw-r--r-- 1 root root   0 Apr  6 12:51 1.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 2.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 3.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 4.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 5.txt
-rw-r--r-- 1 root root 158 Apr  6 12:51 hosts
[root@web01:~]#


2)客户端有啥,服务器必须和客户端一样
[root@web01:~]#ll
total 4
-rw-r--r-- 1 root root   0 Apr  6 12:58 10.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 1.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 2.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 3.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 4.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 5.txt
-rw-r--r-- 1 root root   0 Apr  6 12:58 6.txt
-rw-r--r-- 1 root root   0 Apr  6 12:58 7.txt
-rw-r--r-- 1 root root   0 Apr  6 12:58 8.txt
-rw-r--r-- 1 root root   0 Apr  6 12:58 9.txt
-rw-r--r-- 1 root root 158 Apr  6 12:51 hosts
[root@web01:~]#
执行同步命令: 结果41服务器上young多了10个文件和远端服务器一样
[root@backup:~]#rsync -avz --delete 172.16.1.7:/root/ ./young/
[root@backup:~]#ll young/
total 4
-rw-r--r-- 1 root root   0 Apr  6 12:58 10.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 1.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 2.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 3.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 4.txt
-rw-r--r-- 1 root root   0 Apr  6 12:51 5.txt
-rw-r--r-- 1 root root   0 Apr  6 12:58 6.txt
-rw-r--r-- 1 root root   0 Apr  6 12:58 7.txt
-rw-r--r-- 1 root root   0 Apr  6 12:58 8.txt
-rw-r--r-- 1 root root   0 Apr  6 12:58 9.txt
-rw-r--r-- 1 root root 158 Apr  6 12:51 hosts
[root@backup:~]#

4.5 --password-file

使用密码文件传输 --password-file=密码文件
守护进程举例:
1)将密码写入文件中
[root@web01:~]#echo 123456 > /etc/rsync.pass
2)授权600权限
[root@web01:~]#chmod 600 /etc/rsync.pass 
[root@web01:~]#ll /etc/rsync.pass
-rw------- 1 root root 7 Apr  6 13:02 /etc/rsync.pas
3)使用指定密码的参数推送
[root@web01:~]#rsync -avz 3.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.pass
sending incremental file list
3.txt
案例.免交互使用密码参数
[root@web01:~]#cat backup.sh 
tar zcf /opt/etc.tar.gz /etc
rsync -avz /opt/etc.tar.gz rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.pass

RSYNC_PASSWORD变量

RSYNC_PASSWORD变量 # rsync服务专用变量
[root@web01:~]#export RSYNC_PASSWORD=123456
[root@web01:~]#touch 4.txt
[root@web01:~]#rsync -avz 4.txt rsync_backup@172.16.1.41::backup
sending incremental file list
4.txt

sent 88 bytes  received 43 bytes  87.33 bytes/sec
total size is 0  speedup is 0.00
[root@web01:~]#

[root@web01:~]#cat backup.sh 
export RSYNC_PASSWORD=123456
tar zcf /opt/etc.tar.gz /etc
rsync -avz /opt/etc.tar.gz rsync_backup@172.16.1.41::backup

5. Rsync备份案例

可重复使用。
1.客户端提前准备存放的备份的目录,目录规则如下:/backup/nfs_172.16.1.31_2018-09-02
需求: 定时任务配置文件 /etc/passwd
目录创建方式:
[root@web01:~]#mkdir -p /backup/`hostname`_`hostname -I|awk '{print $1}'`_`date +%F`
[root@web01:~]#ll /backup/
total 0
drwxr-xr-x 2 root root 6 Jul 31 11:37 web01_10.0.0.7_2024-07-31


2.客户端在本地打包备份(系统配置文件、应用配置等)拷贝至/backup/nfs_172.16.1.31_2018-09-02
[root@web01:~]#code_dir=/backup/`hostname`_`hostname -I|awk '{print $1}'`_`date +%F`
[root@web01:~]#echo $code_dir
/backup/web01_10.0.0.7_2024-07-31
[root@web01:~]#tar zcf $code_dir/etc.tar.gz /etc
tar: Removing leading `/' from member names
[root@web01:~]#ll $code_dir/
total 5220
-rw-r--r-- 1 root root 5342620 Jul 31 11:38 etc.tar.gz
[root@web01:~]#ll /backup/web01_10.0.0.7_2024-07-31/
total 5220
-rw-r--r-- 1 root root 5342620 Jul 31 11:38 etc.tar.gz


3.客户端最后将备份的数据进行推送至备份服务器
rsync -avz $code_dir rsync_backup@172.16.1.41::backup 


4.客户端每天凌晨1点定时执行该脚本
00 01 * * * root  sh xxx.sh   # 测试每分钟执行一次


5.客户端服务器本地保留最近7天的数据, 避免浪费磁盘空间 写入脚本
模拟7天前的时间 date -s 
find /backup/* -mtime +7|xargs rm -rf

posted @ 2025-04-06 14:55  Sempiterno  阅读(4)  评论(0)    收藏  举报