BorgBackup(简称 Borg)是一款基于命令行的增量备份工具。Github地址:https://github.com/borgbackup/borg
官网上所述的优势如下:

  • Space efficient storage of backups. - 高效存储
  • Secure, authenticated encryption. - 可加密
  • Compression: LZ4, zlib, LZMA, zstd (since borg 1.1.4). - 支持多种压缩算法
  • Mountable backups with FUSE. - 可使用FUSE异地本非
  • Easy installation on multiple platforms: Linux, macOS, BSD, … - 支持多个平台
  • Free software (BSD license). - 开源
  • Backed by a large and active open source community.

CentOS下安装

borgbackup在EPEL中已有提供,CentOS 7下使用如下命令安装:

1
2
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y borgbackup

简单使用

BorgBackup备份有两层概念,分为repository和archive, 顾名思义,repository就是一个备份仓库,archive就是每次备份。每次备份时,工具会检查各个archive,只备份有变动的数据。

BorgBackup几个基本的命令如下:

1
2
3
4
5
6
borg init -- 初始化一个仓库
borg create -- 创建一个archive到仓库中
borg list -- 列出所有的仓库或者某个仓库中某个archive的内容
borg extract -- 还原某个archive
borg delete -- 手动删除某个archive
borg config -- 获取或者设置某个配置

一个简单的备份和恢复的流程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 创建仓库存储目录
$ mkdir -p /opt/backup/
# 新建仓库
$ borg init --encryption=repokey /opt/backup/borg_sample
# 创建测试备份内容
# 会被修改的文件file_change.txt,不会被修改的文件file_static.txt,二进制大文件random.dump
$ mkdir -p /opt/data
$ cd /opt/data
$ echo "here is the first line of file 1" >> file_change.txt
$ echo "here is the static file" >> file_static.txt
$ dd if=/dev/urandom of=random.dump bs=10M count=10
10+0 records in
10+0 records out
104857600 bytes (105 MB) copied, 0.950948 s, 110 MB/s
$
# 第一次备份
$ borg create --stats /opt/backup/borg_sample::first /opt/data/
------------------------------------------------------------------------------
Archive name: first
Archive fingerprint: 92bf20bca7a1d620d92f831e763601ca63ff951944de81146332ad12e93bb787
Time (start): Tue, 2019-09-24 04:06:51
Time (end): Tue, 2019-09-24 04:06:52
Duration: 0.82 seconds
Number of files: 3
Utilization of max. archive size: 0%
------------------------------------------------------------------------------
Original size Compressed size Deduplicated size
This archive: 104.86 MB 105.27 MB 105.27 MB
All archives: 104.86 MB 105.27 MB 105.27 MB
Unique chunks Total chunks
Chunk index: 48 48
------------------------------------------------------------------------------
$
# 修改备份内容,修改file_change.txt, 新增file_new.txt,新增random_2.dump
$ cd /opt/data
$ echo "here is the second line of file 1" >> file_change.txt
$ echo "here is new file for second backup" >> file_new.txt
$ dd if=/dev/urandom of=random_2.dump bs=10M count=10
10+0 records in
10+0 records out
104857600 bytes (105 MB) copied, 0.599837 s, 175 MB/s
$
# 第二次备份
$ borg create --stats /opt/backup/borg_sample::second /opt/data/
------------------------------------------------------------------------------
Archive name: second
Archive fingerprint: a423a94e8a8f4352e72c0951e6a408f4f4f6d5f362518dcbcba77b9005dafa12
Time (start): Tue, 2019-09-24 04:10:55
Time (end): Tue, 2019-09-24 04:10:56
Duration: 1.26 seconds
Number of files: 5
Utilization of max. archive size: 0%
------------------------------------------------------------------------------
Original size Compressed size Deduplicated size
This archive: 209.72 MB 210.55 MB 105.28 MB
All archives: 314.58 MB 315.82 MB 210.55 MB
Unique chunks Total chunks
Chunk index: 92 137
------------------------------------------------------------------------------
$
# 列出所有的备份
$ borg list /opt/backup/borg_sample/
first Tue, 2019-09-24 04:06:51 [92bf20bca7a1d620d92f831e763601ca63ff951944de81146332ad12e93bb787]
second Tue, 2019-09-24 04:10:55 [a423a94e8a8f4352e72c0951e6a408f4f4f6d5f362518dcbcba77b9005dafa12]
$
# 列出第一次备份的内容
$ borg list /opt/backup/borg_sample::first
drwxr-xr-x root root 0 Tue, 2019-09-24 04:03:45 opt/data
-rw-r--r-- root root 33 Tue, 2019-09-24 04:03:45 opt/data/file_change.txt
-rw-r--r-- root root 24 Tue, 2019-09-24 04:03:45 opt/data/file_static.txt
-rw-r--r-- root root 104857600 Tue, 2019-09-24 04:03:46 opt/data/random.dump
$
# 将第一次备份导出, 查看file_change.txt内容,只包含第一次的内容
$ mkdir -p /opt/restore/first
$ cd /opt/restore/first
$ borg extract --list /opt/backup/borg_sample::first
opt/data
opt/data/file_change.txt
opt/data/file_static.txt
opt/data/random.dump
$
$ cat opt/data/file_change.txt
here is the first line of file 1
$
# 将第二次备份导出, 查看file_change.txt内容,其中包含了第二次新增加的内容
# 也包含了第一次新增的file_new.txt和random_2.dump
$ mkdir -p /opt/restore/second
$ cd /opt/restore/second
$ borg extract --list /opt/backup/borg_sample::second
opt/data
opt/data/file_change.txt
opt/data/file_static.txt
opt/data/random.dump
opt/data/file_new.txt
opt/data/random_2.dump
$
$ cat opt/data/file_change.txt
here is the first line of file 1
here is the second line of file 1
$
# 第一次备份是100M,第二次是200M, 但由于是增量备份的,random.dump没有改变,因此仓库的总容量只有200M
$ du -sh /opt/backup/borg_sample/
201M /opt/backup/borg_sample/
$
# 删除第一次备份,恢复第二次的备份
# 可以看到再第一次和第二次备份中都有的file_static.txt和random.dump都可以恢复出来。
# 恢复出来的数据和之前恢复的数据是一样的
$ borg delete /opt/backup/borg_sample::first
$ borg list /opt/backup/borg_sample
second Tue, 2019-09-24 04:10:55 [a423a94e8a8f4352e72c0951e6a408f4f4f6d5f362518dcbcba77b9005dafa12]
$
$ mkdir -p /opt/restore/second_back
$ cd /opt/restore/second_back
$ borg extract --list /opt/backup/borg_sample::second
opt/data
opt/data/file_change.txt
opt/data/file_static.txt
opt/data/random.dump
opt/data/file_new.txt
opt/data/random_2.dump
$
$ diff -r /opt/restore/second /opt/restore/second_back
$
$ cat opt/data/file_change.txt
here is the first line of file 1
here is the second line of file 1
$
$ cat opt/data/file_static.txt
here is the static file
$

自动备份

官网上的一个自动备份的shell脚本automating-backups
可按照实际使用要求进行修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh
# Setting this, so the repo does not need to be given on the commandline:
export BORG_REPO=ssh://username@example.com:2022/~/backup/main
# Setting this, so you won't be asked for your repository passphrase:
export BORG_PASSPHRASE='XYZl0ngandsecurepa_55_phrasea&&123'
# or this to ask an external program to supply the passphrase:
export BORG_PASSCOMMAND='pass show backup'
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
info "Starting backup"
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--exclude-caches \
--exclude '/home/*/.cache/*' \
--exclude '/var/cache/*' \
--exclude '/var/tmp/*' \
\
::'{hostname}-{now}' \
/etc \
/home \
/root \
/var \
backup_exit=$?
info "Pruning repository"
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:
borg prune \
--list \
--prefix '{hostname}-' \
--show-rc \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
prune_exit=$?
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup and Prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
info "Backup and/or Prune finished with warnings"
else
info "Backup and/or Prune finished with errors"
fi
exit ${global_exit}

详细使用

各个命令更细化的命令,官网文档中有非常详细的描述https://borgbackup.readthedocs.io/en/stable/usage/general.html

Reference

留言