Rsync数据镜像备份同步

Rsync是unix系统下的数据镜像备份工具,支持快速完全备份和增量备份,支持本地复制、远程同步;使用之前需要先登录目标服务器进行身份认证,认证后才能进行数据同步,身份认证方式取决于所使用的协议类型,一般使用ssh协议和rsync协议。

rsync特性

  • 能更新整个目录树和文件系统
  • 可以有选择的保留符号链接、硬链接、文件属性、权限、时间等
  • 对于部署,无需任何特殊权限要求
  • 对于多个文件,文件传输效率高
  • 可以使用ssh或自定义端口作为传输端口

rsync工作原理

源地址(文件)、目标地址(文件)以及以哪一方为基准组成,数据的同步方式有推送(上传)、拉取(下载)。

rsync实验演示1 – 上下行同步

序号 IP地址 操作系统 角色 备注
1 10.0.2.13 Ubuntu 20.04.1 LTS rsnyc客户端 数据备份服务器
2 10.0.2.11 CentOS Linux release 7.9.2009 (Core) NFS服务端、rsnyc服务端 数据基准服务器

定期同步的缺点:执行备份时间固定、延期明显、实时性差,当同步源长期不变化,密集的定期任务是不必要的。

root@ubuntu:~# mkdir /filedst # 两台服务器依据角色分别创建目录 /filedst、/filesrc
[root@centos7 ~]# mkdir /filesrc # 两台服务器依据角色分别创建目录 /filedst、/filesrc
[root@centos7 filesrc]# ll # 便于测试,上传文件
total 5840
-rw-r--r-- 1 root root 5828913 Jun 28  2020 Dingtalk
-rw-r--r-- 1 root root  145520 Feb 13 13:54 zabbix-install.sh.x
[root@centos7 ~]# useradd xiaoshuai.zhu
[root@centos7 ~]# passwd xiaoshuai.zhu 
[root@centos7 ~]# setfacl -m u:xiaoshuai.zhu:rwx /filesrc/ # 创建数据同步用户,通过ACL权限管理赋予对应权限,若要实现免密码同步,只需做好ssh密钥对登录

1)将NFS数据同步到Rsync服务器

[root@centos7 ~]# vim /etc/rsyncd.conf
address = 10.0.2.11 # rsync绑定IP
port 873 # 默认服务端口873
log file = /var/log/rsyncd.log # 日志文件位置
pid file = /var/run/rsyncd.pid # 进程号文件位置

[files] # 文件模块名
        comment = fils directory backup # 描述
        path = /filesrc # 目录
        read only = no # 是否允许读取 
        dont cpmpress = *.gz *.bz2 # 哪些文件类型不进行压缩
        auth users = xiaoshuai.zhu # 登录用户名,需自建
        secrets file = /etc/rsyncd_users.db # 认证所需用户密码文件,需自建

[root@centos7 ~]# vim /etc/rsyncd_users.db
xiaoshuai.zhu:Aa123456
[root@centos7 ~]# chmod 600 /etc/rsyncd_users.db
[root@centos7 ~]# rsync --daemon
[root@centos7 ~]# netstat -anptu | grep 873
tcp        0      0 10.0.2.11:873           0.0.0.0:*               LISTEN      1587/rsync
[root@centos7 ~]# setfacl -m u:nobody:rwx /filesrc/ # 映射用户对目录有读权限

2) rsync同步的两种方式

root@ubuntu:~# rsync -avz rsync://xiaoshuai.zhu@10.0.2.11/files /filedst/ # 下行同步(下载)
Password: 
receiving incremental file list
./
Dingtalk
zabbix-install.sh.x
迈克杰克痛.mp4

sent 84 bytes  received 4,952,932 bytes  1,100,670.22 bytes/sec
total size is 8,757,650  speedup is 1.77

root@ubuntu:~# cd /filedst/
root@ubuntu:/filedst# rz
root@ubuntu:/filedst# rsync -avz /filedst/* rsync://xiaoshuai.zhu@10.0.2.11/files # 上行同步(上传)
Password: 
sending incremental file list
你想怎么升华.mp4
rsync: chgrp "/.你想怎么升华.mp4.PuabOA" (in files) failed: Operation not permitted (1)

sent 788,549 bytes  received 133 bytes  121,335.69 bytes/sec
total size is 9,551,126  speedup is 12.11
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1207) [sender=3.1.3]
root@ubuntu:/filedst# 

rsync实验演示2 – 配置 rsync + inotify 单向实时同步

实时同步优点:同步源出现变化,立刻启动备份,只要同步源无变化,则不执行备份,节省资源;inotify可以监控文件系统,并且及时向专门的应用程序发出相关的时间警告,如删除、读、写等。

[root@centos7 filesrc]# yum install -y inotify # 通过yum部署inotify
[root@centos7 scripts]# vim src.sh # 利用rsync + inotifywait结合脚本实现单向实时同步
#!/bin/bash
a="inotifywait -mrq -e create,delete /filesrc"
b="rsync -avz /filesrc/* zhupengfei@10.0.2.13:/home/zhupengfei/filedst" # ubuntu默认禁止root超级用户远程登录ssh的,这里换一个普通用户登录
$a | while read directory event file
do
    $b
done

[root@centos7 scripts]# vim ~/.ssh/config
#Add Hosts below 
Host 10.0.2.13
HostName 10.0.2.13
User zhupengfei
Port 22
IdentityFile /root/.ssh/ubuntu

[root@centos7 scripts]# vim src-key.sh # 利用rsync + inotifywait结合脚本实现单向实时同步,建议配置信任密钥登录
#!/bin/bash
a="inotifywait -mrq -e create,delete /filesrc"
b="rsync -e ssh /root/.ssh/ubuntu13 /filesrc/* zhupengfei@10.0.2.13:/home/zhupengfei/filedst"  # 方法适用于SSH和rsync
$a | while read directory event file
do
    $b
done

实验结果验证:在NFS端创建、删除文件、查看备份端是否正常

rsync实验演示3 – 配置 rsync + unison 双向实时同步

双向实时同步缺点:同步效率较低。

root@ubuntu:~# apt-get install -y unison # 服务端和客户端都需要部署unison
[root@centos7 ~]# yum install -y unison # 服务端和客户端都需要部署unison

[root@centos7 ~]# cat /scripts/src-unison.sh  # 源目录不能使用通配符*,会造成死循环
#!/bin/bash
a="inotifywait -mrq -e create,delete /filesrc"
b="unison -batch /filesrc/ ssh://10.0.2.13://filedst/"
$a | while read directory event file
do
    $b
done

root@ubuntu:~# cat /scripts/src-unison.sh 
#!/bin/bash
a="inotifywait -mrq -e create,delete /filedst"
b="unison -batch /filedst/ ssh://10.0.2.11://filesrc"
$a | while read directory event file
do
        $b
    done

实验结果验证:分别在filesrc、filedst端创建、删除文件,查看是否双向同步,同步略有延迟。

上一篇
下一篇