1、RSYNC数据备份

1.1 rsync服务简介

rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

1.2 rsync特点和优势

  • rsync功能

  • 作为命令,实现本地-远程文件同步

  • 作为服务,实现本地-远程文件同步

  • rsync特点

  • 可以镜像保存整个目录树和文件系统

  • 可以保留原有的权限(permission,mode),owner,group,时间(修改时间,modify time),软硬链接,文件acl,文件属性(attributes)信息等

  • 传输效率高,使用同步算法,只比较变化的

  • 支持匿名传输,方便网站镜像;也可以做验证,加强安全

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

  • rsync同类服务类型

  • sync 同步:刷新文件系统缓存,强制将修改过的数据块写入磁盘,并且更新超级块。

  • async 异步:将数据先放到缓冲区,再周期性(一般是30s)的去同步到磁盘。

  • rsync 远程同步:remote synchronous

  • 常见备份分类

    • 完整备份:每次备份都是从备份源将所有的文件或目录备份到目的地
    • 差量备份:备份上次完全备份以后有变化的数据(他针对的上次的完全备份,他备份过程中不清除存档属性)
    • 增量备份:备份上次备份以后有变化的数据.(他才不管是那种类型的备份,有变化的数据就备份,他会清除存档属性)

注意: 通常结合shell脚本使用,效率更高。官方网站:https://rsync.samba.org/

1.3 rysnc运行模式简介

  • 运行模式和端口

  • 采用C/S模式(客户端/服务器模式)[ 就是一个点到点的传输,直接使用rsync命令 ]

  • 端口873

  • 相关名词解释

  • 发起端:负责发起rsync同步操作的客户机叫做发起端,通知服务器我要备份你的数据

  • 备份源:负责相应来自客户机rsync同步操作的服务器叫做备份源,需要备份的服务器

  • 服务端:运行rsyncd服务,一般来说,需要备份的服务器

  • 客户端:存放备份数据

  • rysncd服务工作原理:rysncd服务通过Xinetd管理,使用rsyncd服务来同步是先通过xinetd监听873号端口,如果rsync进来的是873号端口,那么xinetd就会通知它所管辖的rsync服务来做回应,接下来就是rsync俩服务于之间的通讯,如下图所示:

1.4 数据同步方式

两种数据同步方式:

  • push推方式: 一台主机负责把数据传送给其他主机,服务器开销很大,比较适合后端服务器少的情况
  • pull拉方式: 所有主机定时去找一主机拉数据,可能就会导致数据缓慢

centos7服务搭建常用服务配置之二:Rsync+sersync实现数据实时同步-LMLPHP

  • 常用的数据同步方式:
  • 推:目的主机配置为rsync服务器,源主机周期性的使用rsync命令把要同步的目录推过去(需要备份的机器是客户端,存储备份的机器是服务端)rsync -avz 源路径 user@远端服务器IP:目的路径

[root@centos7-127 tmp]# mkdir test1 # 创建测试文件见test1
[root@centos7-127 tmp]# cp /etc/passwd ./test1/ # 复制/etc/passwd作为测试文件
[root@centos7-127 tmp]# ll ./test1/
总用量 4
-rw-r--r-- 1 root root 2567 9月 17 17:43 passwd
[root@centos7-127 tmp]# rsync -avz /tmp/test1 root@192.168.87.128:/tmp/ # 将127上的/tmp/test1推到128的/tmp目录下
root@192.168.87.128's password: # 输入远端服务器root密码
sending incremental file list
test1/
test1/passwd sent 1,127 bytes received 39 bytes 155.47 bytes/sec
total size is 2,567 speedup is 2.20 # 传输完成
[root@centos7-128 tmp]# ll ./test1 # 在128上查看发现数据推送成功
总用量 4
-rw-r--r-- 1 root root 2567 9月 17 17:43 passwd
  • 拉:源主机配置为rsync服务器,目的主机周期性的使用rsync命令把要同步的目录拉过来(需要备份的机器是服务端,存储备份的机器是客户端)rsync -avz user@远端服务器IP:源路径 目的路径

[root@centos7-127 tmp]# rsync -avz root@192.168.87.128:/tmp/test/ /tmp/test # 从128拉取数据
root@192.168.87.128's password:
receiving incremental file list
./
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt sent 312 bytes received 834 bytes 152.80 bytes/sec # 数据传输完成
total size is 0 speedup is 0.00 [root@centos7-127 tmp]# ll ./test # 查看发现数据拉取成功
总用量 0
-rw-r--r-- 1 root root 0 9月 16 10:38 1.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 2.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 3.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 4.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 5.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 6.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 7.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 8.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 9.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 a.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 b.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 c.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 d.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 e.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 f.txt

2 Rsync实验测试

2.1 实验环境说明

|:-

05-07 15:15
查看更多