Linux下的rsync远程增量备份详解

简介: Linux下的rsync远程增量备份详解

一、rsync工具介绍

1.rsync工具简介

  • rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

2.rsync的用法和选项说明

#rsync 用法

rsync  version 3.1.3  protocol version 31
Copyright (C) 1996-2018 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes, prealloc

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

rsync is a file transfer program capable of efficient remote update
via a fast differencing algorithm.

Usage: rsync [OPTION]... SRC [SRC]... DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
rsync选项说明:

-a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
-z 传输时压缩;
-P 传输进度;
-v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;
参数详解编辑
-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-L, --copy-links 像对待常规文件一样处理软链接
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链接
--safe-links 忽略指向SRC路径目录树以外的链接
-H, --hard-links 保留硬链接
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, --dry-run显示哪些文件将被传输
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件

二、rsync备份方法

1.备份要求

将control端的/data/tmp/下所有文件备份到node1端的user1用户/data/backup/control/目录下

2.备份源路径

[root@control tmp]# pwd
/data/tmp
[root@control tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 30 14:10 file0.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file10.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file11.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file12.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file13.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file14.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file15.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file16.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file17.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file18.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file19.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file1.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file20.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file2.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file3.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file4.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file5.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file6.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file7.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file8.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file9.py

3.进行远程增量备份

[root@control ansible]# rsync -avz /data/tmp/ user1@node1:/data/backup/control/
[root@control ansible]# rsync -avz /data/tmp/ root@node1:/data/backup/control/
sending incremental file list
./
file0.py
file1.py
file10.py
file11.py
file12.py
file13.py
file14.py
file15.py
file16.py
file17.py
file18.py
file19.py
file2.py
file20.py
file3.py
file4.py
file5.py
file6.py
file7.py
file8.py
file9.py

sent 1,151 bytes  received 418 bytes  3,138.00 bytes/sec
total size is 0  speedup is 0.00

三、测试备份结果

  • 在node1查看备份结果
[root@node1 control]# pwd
/data/backup/control
[root@node1 control]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 30 14:10 file0.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file10.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file11.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file12.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file13.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file14.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file15.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file16.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file17.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file18.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file19.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file1.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file20.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file2.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file3.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file4.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file5.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file6.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file7.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file8.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file9.py

[

四、测试增量备份

1.在control端备份源目录新增文件

[root@control tmp]# touch test{1..3}
[root@control tmp]# ls
file0.py   file11.py  file13.py  file15.py  file17.py  file19.py  file20.py  file3.py  file5.py  file7.py  file9.py  test2
file10.py  file12.py  file14.py  file16.py  file18.py  file1.py   file2.py   file4.py  file6.py  file8.py  test1     test3
[root@control tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 30 14:10 file0.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file10.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file11.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file12.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file13.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file14.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file15.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file16.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file17.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file18.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file19.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file1.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file20.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file2.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file3.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file4.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file5.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file6.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file7.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file8.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file9.py
-rw-r--r--. 1 root root 0 May  1 19:36 test1
-rw-r--r--. 1 root root 0 May  1 19:36 test2
-rw-r--r--. 1 root root 0 May  1 19:36 test3
[root@control tmp]# rsync -avz /data/tmp/ root@node1:/data/backup/control/
sending incremental file list
./
test1
test2                      #只同步了新增的三个文件
test3

sent 549 bytes  received 76 bytes  1,250.00 bytes/sec
total size is 0  speedup is 0.00

2.在control端备份源目录修改文件内容

[root@control tmp]# echo 123 >> file1.py 
[root@control tmp]# rsync -avz /data/tmp/ root@node1:/data/backup/control/
sending incremental file list
file1.py                      #只同步了修改内容的文件

sent 486 bytes  received 35 bytes  1,042.00 bytes/sec
total size is 4  speedup is 0.01

3.在control端备份源目录修改文件权限

[root@control tmp]# chmod o+rwx file8.py 
[root@control tmp]# rsync -avz /data/tmp/ root@node1:/data/backup/control/
sending incremental file list

sent 449 bytes  received 19 bytes  936.00 bytes/sec
total size is 4  speedup is 0.01

4.查看备份端情况

  • 查看node1上备份文件变化情况,发现修改权限的文件进行了备份,权限保持了一致。
[root@node1 control]# ll
total 4
-rw-r--r--. 1 root root 0 Apr 30 14:10 file0.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file10.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file11.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file12.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file13.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file14.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file15.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file16.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file17.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file18.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file19.py
-rw-r--r--. 1 root root 4 May  1 19:38 file1.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file20.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file2.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file3.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file4.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file5.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file6.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file7.py
-rw-r--rwx. 1 root root 0 Apr 30 14:10 file8.py
-rw-r--r--. 1 root root 0 Apr 30 14:10 file9.py
-rw-r--r--. 1 root root 0 May  1 19:36 test1
-rw-r--r--. 1 root root 0 May  1 19:36 test2
-rw-r--r--. 1 root root 0 May  1 19:36 test3
相关文章
|
2月前
|
Shell Linux API
【Shell 命令集合 备份压缩 】Linux 解压缩文件 unzip命令 使用指南
【Shell 命令集合 备份压缩 】Linux 解压缩文件 unzip命令 使用指南
59 0
|
2月前
|
算法 Shell Linux
【Shell 命令集合 备份压缩 】Linux 处理lha格式 lha命令 使用指南
【Shell 命令集合 备份压缩 】Linux 处理lha格式 lha命令 使用指南
40 0
|
2月前
|
Shell Linux 编译器
【Shell 命令集合 备份压缩 】Linux 提取zip压缩文件的详细信息 zipinfo命令 使用指南
【Shell 命令集合 备份压缩 】Linux 提取zip压缩文件的详细信息 zipinfo命令 使用指南
36 0
|
2月前
|
存储 算法 Shell
【Shell 命令集合 备份压缩 】⭐⭐Linux 压缩和归档工具 zip命令 使用指南
【Shell 命令集合 备份压缩 】⭐⭐Linux 压缩和归档工具 zip命令 使用指南
36 0
|
2月前
|
存储 Shell Linux
【Shell 命令集合 备份压缩 】Linux 解码uuencode编码的文件 uudecode 命令 使用指南
【Shell 命令集合 备份压缩 】Linux 解码uuencode编码的文件 uudecode 命令 使用指南
33 0
|
2月前
|
存储 Shell Linux
【Shell 命令集合 备份压缩 】Linux 恢复由dump命令创建的备份文件 restore命令 使用指南
【Shell 命令集合 备份压缩 】Linux 恢复由dump命令创建的备份文件 restore命令 使用指南
37 0
|
2月前
|
存储 算法 Shell
【Shell 命令集合 备份压缩 】⭐⭐⭐Linux 解压缩gz工具 gzip命令 使用指南
【Shell 命令集合 备份压缩 】⭐⭐⭐Linux 解压缩gz工具 gzip命令 使用指南
35 0
|
2月前
|
存储 Shell Linux
【Shell 命令集合 备份压缩 】Linux 将二进制文件转换为ASCII码 uuencode命令 使用指南
【Shell 命令集合 备份压缩 】Linux 将二进制文件转换为ASCII码 uuencode命令 使用指南
37 0
|
2月前
|
算法 Shell Linux
【Shell 命令集合 备份压缩 】Linux 解压缩ARJ格式 unarj命令 使用指南
【Shell 命令集合 备份压缩 】Linux 解压缩ARJ格式 unarj命令 使用指南
29 0
|
2月前
|
存储 算法 Shell
【Shell 命令集合 备份压缩 】⭐⭐⭐Linux 解压缩 tar命令 使用指南
【Shell 命令集合 备份压缩 】⭐⭐⭐Linux 解压缩 tar命令 使用指南
29 0