前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux下的rsync远程增量备份详解

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

原创
作者头像
江湖有缘
发布2023-11-08 21:26:59
4970
发布2023-11-08 21:26:59
举报
文章被收录于专栏:Linux成长之路Linux成长之路

一、rsync工具介绍

1.rsync工具简介

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

2.rsync的用法和选项说明

代码语言:shell
复制
#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]
代码语言:shell
复制
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.备份源路径

代码语言:shell
复制
[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.进行远程增量备份

代码语言:shell
复制
[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查看备份结果
代码语言:txt
复制
[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端备份源目录新增文件

代码语言:shell
复制
[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
代码语言:shell
复制
[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端备份源目录修改文件内容

代码语言:shell
复制
[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端备份源目录修改文件权限

代码语言:shell
复制
[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上备份文件变化情况,发现修改权限的文件进行了备份,权限保持了一致。
代码语言:shell
复制
[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

我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、rsync工具介绍
    • 1.rsync工具简介
      • 2.rsync的用法和选项说明
      • 二、rsync备份方法
        • 1.备份要求
          • 2.备份源路径
            • 3.进行远程增量备份
            • 三、测试备份结果
            • 四、测试增量备份
              • 1.在control端备份源目录新增文件
                • 2.在control端备份源目录修改文件内容
                  • 3.在control端备份源目录修改文件权限
                    • 4.查看备份端情况
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                    http://www.vxiaotou.com