- --backup-dir:将有变化的文件进行更新同时把旧版本保存在指定的目录中,从而实现增量备份。
- 4-10.4 内容:
通过 rsync --backup-dir 指定备份文件存储的路径。 - 操作步骤:
1、新建 backup 目录用作备份文件的目录(备份源文件的目录)。新建 backups_dir 目录用作指定储存 backup 目录的备份文件。(当 backup 目录需要更新备份文件时,已存在的备份文件会存储一份到指定的 backups_dir 目录。然后再更新备份文件)
[root@localhost test]# mkdir backup ## 新建 backup 目录
[root@localhost test]#
[root@localhost test]# mkdir backups_dir ## 新建 backups_dir 目录
[root@localhost test]#
[root@localhost test]# tree
.
├── backup
├── backups_dir
└── SRC
├── demo1.txt
├── demo2.txt
└── demo3.txt
3 directories, 3 files
2、 ll SRC/,查询 SRC 目录下的文件详情。demo1.txt、demo2.txt 和 demo3.txt 的属性都是一样。
[root@localhost test]#
[root@localhost test]# ll SRC/
total 0
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]#
3、rsync -a -b / --backup 备份 SRC 目录下的 demo1.txt 和 demo2.txt 文件到 backup目录下。
## rsync -a -b / --backup 备份 SRC 目录下的 demo1.txt 和 demo2.txt 文件到 backup目录下。
[root@localhost test]# rsync -a -b SRC/demo1.txt backup/
[root@localhost test]# rsync -a --backup SRC/demo2.txt backup/
[root@localhost test]#
[root@localhost test]# tree ## 目录结构
.
├── backup ## backup 目录下已有 demo1.txt 和 demo2.txt 文件
│ ├── demo1.txt
│ └── demo2.txt
├── backups_dir
└── SRC
├── demo1.txt
├── demo2.txt
└── demo3.txt
3 directories, 5 files
## backup目录下的 demo1.txt 和 demo2.txt 和 SRC 目录下的 demo1.txt 和 demo2.txt 一致
[root@localhost test]# ll backup
total 0
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo2.txt
[root@localhost test]#
[root@localhost test]# ll SRC/
total 0
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]#
4、vim 编辑 SRC 目录下的 demo1.txt 和 demo2.txt 文件。
[root@localhost test]#
[root@localhost test]# vim SRC/demo1.txt ## SRC/demo1.txt 输入内容 demo1 保存退出
demo1
~
~
~
:wq!
[root@localhost test]# vim SRC/demo2.txt ## SRC/demo2.txt 输入内容 demo2 保存退出
demo2.txt
~
~
:wq!
## SRC目录下的 demo1.txt、demo2.txt 的大小和时间属性已经改变
## demo1.txt 时间 22:41,demo2.txt 时间 22:47
## demo1.txt 大小 6,demo2.txt 大小 10
[root@localhost test]# ll SRC/
total 8
-rw-rw-rw-. 1 root root 6 Aug 8 22:41 demo1.txt
-rw-rw-rw-. 1 root root 10 Aug 8 22:47 demo2.txt
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]#
5、rsync -a -b / --backup 更新备份 SRC 目录下的 demo1.txt 和 demo2.txt 文件到 backup目录下并通过 --backup-dir 指定 backup 目录原有的备份文件转存到 backups_dir 文件作一份原始备份。
## --backup-dir=/root/test/backups_dir,指定备份目录已有的文件备份到这个路径下,
## 原始备份完成后,再更新备份目录内容。案例的备份目录是 backup 目录,原始备份目录是 backups_dir。
[root@localhost test]# rsync -a -b --backup-dir=/root/test/backups_dir SRC/demo1.txt backup/
[root@localhost test]#
[root@localhost test]# rsync -a -backup --backup-dir=/root/test/backups_dir SRC/demo2.txt backup/
[root@localhost test]#
[root@localhost test]# tree
.
├── backup ## 更新后的 demo1.txt 和 demo1.txt 文件。
│ ├── demo1.txt
│ └── demo2.txt
├── backups_dir ## 原始备份文件目录 backups_dir 已有旧版的 demo1.txt 和 demo1.txt 文件。
│ ├── demo1.txt
│ └── demo2.txt
└── SRC
├── demo1.txt
├── demo2.txt
└── demo3.txt
3 directories, 7 files
[root@localhost test]# ll SRC/
total 8
-rw-rw-rw-. 1 root root 6 Aug 8 22:41 demo1.txt
-rw-rw-rw-. 1 root root 10 Aug 8 22:47 demo2.txt
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]#
[root@localhost test]# ll backup ## 更新后的 demo1.txt 和 demo1.txt 文件。
total 8
-rw-rw-rw-. 1 root root 6 Aug 8 22:41 demo1.txt
-rw-rw-rw-. 1 root root 10 Aug 8 22:47 demo2.txt
[root@localhost test]#
[root@localhost test]# ll backups_dir/ ## 旧版的 demo1.txt 和 demo1.txt 文件。
total 0
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-rw-rw-. 1 root root 0 Jul 22 22:44 demo2.txt
[root@localhost test]#

过程分析:
1、SRC目录下的 demo1.txt 和 demo2.txt 源文件全属性备份到backup 目录下。
2、当SRC目录下的 demo1.txt 和 demo2.txt 源文件发生大小和时间变化的时候再全属性备份到 backup 目录下。此时,通过 --backup-dir 参数指定 /root/test/backups_dir 路径。这个路径的 backups_dir 目录是存储备份目录 backup 已有的备份文件。相当于原始备份。
3、当 backup 目录已有的备份文件转存到 backups_dir 后,就会更新备份 SRC目录下的 demo1.txt 和 demo2.txt 。
网友评论