异地容灾备份机制
1.yum安装服务
Rsync本地模式和远程模式
纯通过rsync的命令,来实现,数据目录A 拷贝到数据目录B(增量备份)
默认走ssh协议
也就是模拟cp的用法 很简单
[root@rsync-41 ~]# yum -y install rsync
语法:
rsync 参数 源路径 目标路径
参数解释
-v 详细模式输出
-a 归档模式,递归的方式传输文件,并保持文件的属性,等同于 -rlptgoD
-r 递归拷贝目录
-l 保留软链接
-p 保留原有权限
-t 保留原有时间(修改)
-g 保留属组权限
-o 保留属主权限
-D 等于--devices --specials 表示支持b,c,s,p类型的文件
-R 保留相对路径
-H 保留硬链接
-A 保留ACL策略
-e 指定要执行的远程shell命令
-E 保留可执行权限
-X 保留扩展属性信息 a属性
比较常用的组合参数
rsync -avzP
-a 保持文件原有属性
-v 显示传输细节情况
-z 对传输数据压缩传输
-P 显示文件传输的进度信息
你在命令行里,执行命令,喜欢看到命令的执行过程
-avzP
脚本里面?
bash xxx.sh
rsync -az
2.本地模式传输
rsync实现增量备份
cp实现全量备份
例子:
#拷贝单个文件
[root@rsync-41 ~]# rsync -avzP /var/log/messages /opt
sending incremental file list
messages
935,974 100% 45.33MB/s 0:00:00 (xfr#1, to-chk=0/1)
sent 124,224 bytes received 35 bytes 248,518.00 bytes/sec
total size is 935,974 speedup is 7.53
#在拷贝大文件时需要进行限速,因为会占用磁盘IO
--bwlimit=10 #单位默认为M
#生成大文件
[root@rsync-41 ~]# dd if=/dev/zero of=/var/log/my.log bs=500M count=10
10+0 records in
10+0 records out
5242880000 bytes (5.2 GB) copied, 16.9679 s, 309 MB/s
#不做限速
[root@rsync-41 ~]# rsync -avzP /var/log/my.log /opt/
sending incremental file list
my.log
216,694,784 4% 68.53MB/s 0:01:11
#限速为10M/s
[root@rsync-41 ~]# rsync -avzP /var/log/my.log /opt/ --bwlimit=10
sending incremental file list
my.log
135,266,304 2% 10.13MB/s 0:08:12
#拷贝目录下的数据并携带目录本身
[root@rsync-41 ~]# rsync -avzP /var/log /opt
[root@rsync-41 ~]# ls /opt/
log
#仅拷贝目录数据
[root@rsync-41 ~]# rsync -avzP /var/log/ /opt
[root@rsync-41 ~]# ls /opt/
anaconda boot.log btmp dmesg firewalld lastlog maillog rhsm samba spooler tuned yum.log
#增量备份拷贝
[root@rsync-41 ~]# mkdir /opt/test1
[root@rsync-41 ~]# mkdir /opt/test2
[root@rsync-41 ~]# echo 123 >/opt/test1/1.txt
[root@rsync-41 ~]# echo 456 >/opt/test1/2.txt
[root@rsync-41 ~]# rsync -avzP /opt/test1/ /opt/test2/
sending incremental file list
./
1.txt
4 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)
2.txt
4 100% 3.91kB/s 0:00:00 (xfr#2, to-chk=0/3)
sent 178 bytes received 57 bytes 470.00 bytes/sec
total size is 8 speedup is 0.03
[root@rsync-41 ~]# rsync -avzP /opt/test1/ /opt/test2/ #相同文件再次传输无效
sending incremental file list
sent 83 bytes received 12 bytes 190.00 bytes/sec
total size is 8 speedup is 0.08
[root@rsync-41 ~]# echo 456 >>/opt/test1/1.txt
[root@rsync-41 ~]# rsync -avzP /opt/test1/ /opt/test2/ #修改内容后进行增量传输
sending incremental file list
1.txt
8 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)
sent 136 bytes received 35 bytes 342.00 bytes/sec
total size is 12 speedup is 0.07
#无差异拷贝
#用参数--delete
[root@rsync-41 ~]# ls /opt/test2/
1.txt 2.txt
[root@rsync-41 ~]# rm -f /opt/test1/* #删除test1内容以防混淆
[root@rsync-41 ~]# ls /opt/test1/
[root@rsync-41 ~]# touch /opt/test1/test{1..5}.txt
[root@rsync-41 ~]# ls /opt/test1/
test1.txt test2.txt test3.txt test4.txt test5.txt
#进行无差异传输,第一步会将/opt/test1/与/opt/test2/文件夹中内容删除
#再将内容进行传输,达到两个目录保持一致,无差异
[root@rsync-41 ~]# rsync -avzP /opt/test1/ /opt/test2/ --delete
sending incremental file list
deleting 2.txt
deleting 1.txt
./
test1.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=4/6)
test2.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=3/6)
test3.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=2/6)
test4.txt
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=1/6)
test5.txt
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=0/6)
sent 319 bytes received 132 bytes 902.00 bytes/sec
total size is 0 speedup is 0.00
3.远程模式
把rsync-41 /root下的数据
拷贝到 nfs-31 /opt下
-a 保留源文件属性
-v 显示传输过程
-z 压缩传输速率
-P 显示传输的进度
#若nfs-31机器上无命令rsync会报错,安装即可
rsync-41
[root@rsync-41 ~]# rsync -avzP /root/ root@172.16.1.31:/opt/
root@172.16.1.31's password:
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(226) [sender=3.1.2]
nfs-31
[root@nfs-31 ~]# yum -y install rsync
#传输命令
推送
[root@rsync-41 ~]# rsync -avzP /root/ root@172.16.1.31:/opt/
或
拉取
[root@nfs-31 ~]# rsync -avzP root@172.16.1.41:/root/ /opt/
#无差异传输
#不同主机之间同步数据 --delete
rsync -avzP --delete root@172.16.1.41:/root /tmp/
#坑:如果/和一个空目录进行完全同步,那么效果和删根一样
#坑:传输过程不限速导致带宽被占满 ,--bwlimit=50
远程传输 nfs-31下的 /tmp/2G.log 备份到 rsync-41的/opt下
rsync -avzP /tmp/2G.log root@172.16.1.41:/opt
远程备份文件,且改名
[root@nfs-31 /tmp]#rsync -avzP /tmp/2G.log root@172.16.1.41:/opt/2G.logggggggggggggggggggggg
为什么需要服务模式 Rsync 借助 SSH 协议同步数据存在的缺陷:
1.使用系统用户(不安全) /etc/passwd
2.使用普通用户(会导致权限不足情况)
3.守护进程传输方式: rsync 自身非常重要的功能(不使用系统用户,更加安全)
由于这种方式需要传输的机器上也装有rsync软件,且在传输时需要输入登录密码,不太方便
排除文件进行发送
使用rsyncd服务认证形式的语法,吧数据发过去
rsync -avzP 源路径 虚拟用户@主机名或ip::模块名
使用普通rsync远程备份的语法,用的是ssh协议,使用、/etc/passwd
rsync -avzP 原路径 系统用户名@主机名或ip:目标路径
排除某文件,排除png图片文件,传输
[root@nfs-31 /tmp]#rsync -avzP --exclude=*.png /tmp/ root@rsync-41:/tmp/
# 学github gitlab要把代码上传到代码仓库中,但是要排除某些特殊文件夹,比如.git,总之就是排除隐藏文件
[root@nfs-31 /tmp]#rsync -avzP --exclude=别走啊唐长老 --exclude=.* /tmp/ root@rsync-41:/tmp/