day26-rsync

1.Rsync 简介
1.Rsync可以实现增量的备份(主机和主机之间的数据传输)
2.面试点你们公司用得什么架构 CS/BS架构
Client/Server  客户端和服务端 类似客户端的游戏 王者荣耀 吃鸡 英雄联盟
Browse/Server  浏览器和服务端 类似网页游戏  使用浏览器访问的服务都是BS架构
3.Rsync默认监听端口873
4.全量备份和增量备份
全量备份优缺点: 占用带宽 磁盘IO 占用磁盘空间  数据安全不易丢失
增量备份优缺点:  速度快 占用较少的磁盘IO 节省磁盘空间  数据易丢失(不完整)
2.Rsync 的传输模式
三种传输模式:
本地模式: 类似cp命令   了解
语法结构:
		rsync  -avz 1.txt  /tmp/1.txt

案例1.复制oldboy.txt 到/opt目录
[root@backup:~]#touch oldboy.txt
[root@backup:~]#ll
total 0
-rw-r--r-- 1 root root 0 Jul 31 09:15 oldboy.txt
[root@backup:~]#rsync -avz oldboy.txt /opt/
sending incremental file list
oldboy.txt
[root@backup:~]#ll /opt/
total 0
dr-xr-xr-x 5 root root 79 Mar 24  2023 kylin-sm-package
-rw-r--r-- 1 root root  0 Jul 31 09:15 oldboy.txt
什么是增量: 在执行一次rsync的拷贝动作发现oldboy.txt不会在拷贝。
[root@backup:~]#rsync -avz oldboy.txt /opt/
sending incremental file list

sent 48 bytes  received 12 bytes  120.00 bytes/sec
total size is 0  speedup is 0.00




远程命令模式:
语法结构:
	      上传文件: 推送 push  网盘上传
		  rsync -avz 文件 root@172.16.1.7:/root/
		  下载文件: 拉取 pull  网盘下载
		  rsync -avz root@172.16.1.7:/etc/hosts ./

rsync: 命令
-avz:  参数
root:  对方服务器用户名称(如果不写默认按照当前登录用户的身份去远程连接)
[root@backup:~]#rsync -avz /etc/passwd 172.16.1.7:/root/
Authorized users only. All activities may be monitored and reported.
root@172.16.1.7s password:
@:    分隔符
172.16.1.7: 主机信息 hosts主机名称  域名
[root@backup:~]#cat /etc/hosts	# 做本地域名解析
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.7  web01

# 使用主机名称作为远端服务器的地址
[root@backup:~]#rsync -avz /etc/hosts web01:/root/
# 使用域名作为远端服务器的地址
[root@backup:~]#rsync -avz /etc/passwd www.linuxnc.com:/tmp/


案例1: 将41服务器上的oldboy.txt传输到172.16.1.7root的家目录
[root@backup:~]#rsync -avz oldboy.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:5IRW4TiNlfS+kmXlEpwVnm+uDGASPC0Vj1ncCswZL2Q.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes  # 输入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: 			#输入7的root的密码
sending incremental file list

案例2.将1.7服务器上的hosts文件下载到当前主机的当前目录
[root@backup:~]#rsync -avz root@172.16.1.7:/etc/hosts ./

Authorized users only. All activities may be monitored and reported.
root@172.16.1.7's password:
receiving incremental file list
hosts


案例3.拷贝目录 将41oldboy目录下所有文件传输到1.7的家目录
注意: rsync传输目录时,目录价/ 表示目录下面所有文件,不加/表示目录本身
[root@backup:~]#rsync -avz oldboy/ web01:/root/
Authorized users only. All activities may be monitored and reported.
root@web01's password:
sending incremental file list
./
1.txt
hosts
oldboy.txt

案例4.拷贝目录及目录下所有文件到7
[root@backup:~]#rsync -avz oldboy web01:/root/
Authorized users only. All activities may be monitored and reported.
root@web01's password:
sending incremental file list
oldboy/
oldboy/1.txt
oldboy/hosts
oldboy/oldboy.txt



远程命令模式小结:
1.可以上传文件
  rsync -avz file root@172.16.1.7:/root/
2.可以下载文件
  rsync -avz root@172.16.1.7:/etc/hosts ./
3.Rsync 服务
守护进程模式(CS架构)
1.安装rsync服务 41备份服务器部署
[root@backup:~]#yum -y install rsync

2.配置rsync服务
配置文件: /etc/rsyncd.conf
[root@backup:~]#cat /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup]
comment = welcome to oldboyedu backup!
path = /backup

-----------------------------

uid = rsync
 # 运行进程的用户 虚拟用户,只用来运行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
-----------------
推送语法格式: rsync -avz rsync_backup@172.16.1.41::backup
-----------------

3.根据配置创建必要的信息
1)创建用户rsync
[root@backup:~]#useradd -M -s /sbin/nologin rsync
[root@backup:~]#id rsync
uid=1001(rsync) gid=1001(rsync) groups=1001(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
[root@backup:~]#ll /backup/
total 0
[root@backup:~]#ll -d /backup
drwxr-xr-x 2 root root 6 Jul 31 10:26 /backup
修改目录的属主属组为rsync用户
[root@backup:~]#chown rsync.rsync /backup/
[root@backup:~]#ll -d /backup/
drwxr-xr-x 2 rsync rsync 6 Jul 31 10:26 /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         State       PID/Program name
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      6318/rsync


5.测试rsync服务
客户端测试:
推送:语法格式 最后两个冒号,后面是模块的名称而不是路径。
    rsync -avz rsync_backup@172.16.1.41::backup

[root@web01:~]#rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup
Password:
sending incremental file list
hosts
sent 140 bytes  received 43 bytes  73.20 bytes/sec
total size is 158  speedup is 0.86


下载: 从backup服务器中下载文件
[root@web01:~]#rsync -avz rsync_backup@172.16.1.41::backup/hosts  .
4.Rsync 参数
1.rsync排除推送的文件 --exclude
[root@backup:~]#touch {1..3}.txt
[root@backup:~]#ll
total 0
-rw-r--r-- 1 root root  0 Jul 31 10:46 1.txt
-rw-r--r-- 1 root root  0 Jul 31 10:46 2.txt
-rw-r--r-- 1 root root  0 Jul 31 10:46 3.txt
drwxr-xr-x 2 root root 50 Jul 31 09:58 oldboy
[root@backup:~]#rsync -avz *.txt --exclude=1.txt root@web01:/root/
Authorized users only. All activities may be monitored and reported.
root@web01's password:
sending incremental file list
2.txt
3.txt

2.排除多个文件使用 ——exclude-from=file
[root@backup:~]#cat /tmp/1.log
1.txt
2.txt
[root@backup:~]#rsync -avz *.txt --exclude-from=/tmp/1.log root@web01:/root/
Authorized users only. All activities may be monitored and reported.
root@web01's password:
sending incremental file list
3.txt

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


4.--delete 两端数据一致
1)我有啥,你必须和我一样
第一步: 41
[root@backup:~]#mkdir oldboy
[root@backup:~]#touch oldboy{1.txt,hosts,oldboy.txt}
第二步: 41拷贝oldboy目录到web01
[root@backup:~]#rsync  -avz oldboy web01:/root/
第三步: 在web01oldboy目录下创建10个文件
[root@web01:~]#touch oldboy/{1..10}.log
[root@web01:~]#ll oldboy/
total 8
-rw-r--r-- 1 root root   0 Jul 31 11:10 10.log
-rw-r--r-- 1 root root   0 Jul 31 11:10 1.log
-rw-r--r-- 1 root root  44 Jul 15 09:57 1.txt
-rw-r--r-- 1 root root   0 Jul 31 11:10 2.log
-rw-r--r-- 1 root root   0 Jul 31 11:10 3.log
-rw-r--r-- 1 root root   0 Jul 31 11:10 4.log
-rw-r--r-- 1 root root   0 Jul 31 11:10 5.log
-rw-r--r-- 1 root root   0 Jul 31 11:10 6.log
-rw-r--r-- 1 root root   0 Jul 31 11:10 7.log
-rw-r--r-- 1 root root   0 Jul 31 11:10 8.log
-rw-r--r-- 1 root root   0 Jul 31 11:10 9.log
-rw-r--r-- 1 root root 158 Jun 23  2020 hosts
-rw-r--r-- 1 root root   0 Jul 31 09:15 oldboy.txt

最后执行同步的命令: 结果是web01新创建的文件被删除。
[root@backup:~]#rsync  -avz --delete oldboy web01:/root/



2)你有啥,我必须和你一样
1,2,3步骤和上面一样
在重新创建10个文件
[root@web01:~]#touch oldboy/{1..10}.log
[root@web01:~]#ll oldboy/
total 8
-rw-r--r-- 1 root root   0 Jul 31 11:18 10.log
-rw-r--r-- 1 root root   0 Jul 31 11:18 1.log
-rw-r--r-- 1 root root  44 Jul 15 09:57 1.txt
-rw-r--r-- 1 root root   0 Jul 31 11:18 2.log
-rw-r--r-- 1 root root   0 Jul 31 11:18 3.log
-rw-r--r-- 1 root root   0 Jul 31 11:18 4.log
-rw-r--r-- 1 root root   0 Jul 31 11:18 5.log
-rw-r--r-- 1 root root   0 Jul 31 11:18 6.log
-rw-r--r-- 1 root root   0 Jul 31 11:18 7.log
-rw-r--r-- 1 root root   0 Jul 31 11:18 8.log
-rw-r--r-- 1 root root   0 Jul 31 11:18 9.log
-rw-r--r-- 1 root root 158 Jun 23  2020 hosts
-rw-r--r-- 1 root root   0 Jul 31 09:15 oldboy.txt

执行同步命令: 结果41服务器上oldboy多了10个文件和远端服务器一样
[root@backup:~]#rsync -avz --delete web01:/root/oldboy ./




5.使用密码文件传输 --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 Jul 31 11:24 /etc/rsync.pass
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

6.RSYNC_PASSWORD变量
[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
知识点重点小结:
1.远程命令模式 两台主机之间相互传送数据
# 将1.txt推送到41服务器的root目录
rsync -avz 1.txt 172.16.1.41:/root/
# 将41的hosts文件下载到当前主机当前位置
rsync -avz 172.16.1.41:/etc/hosts ./

2.rsync服务配置流程
安装
配置
创建必要的数据
启动
测试

3.重要的参数
RSYNC_PASSWORD 写脚本必用
export RSYNC_PASSWORD=123456

--exclude=file
--delete 慎重
--bwlimit=1M
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值