Skip to content

Commit b907ea9

Browse files
committed
ops/storage: Backup example with restic + backblaze b2
1 parent 660a6ca commit b907ea9

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

docs/ops/storage/backup.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- 使用数据库的 dump 工具(例如 `mysqldump`)备份数据库。
1818
- 使用 [Btrfs](./filesystem.md#btrfs-snapshot)[ZFS](./zfs.md#snapshot) 或 LVM 提供的快照功能。Btrfs 与 ZFS 提供了 send/receive 功能以便网络传输。
1919
- 使用 `rclone` 同步到云存储(例如 OneDrive、S3 对象存储等)。
20-
- 使用诸如 [Duplicity](https://duplicity.gitlab.io/)[BorgBackup](https://www.borgbackup.org/) 等备份工具。
20+
- 使用诸如 [Duplicity](https://duplicity.gitlab.io/)[BorgBackup](https://www.borgbackup.org/)[restic](https://restic.net/) 等备份工具。
2121
- 特定平台可能有专用的备份工具,例如虚拟化平台 Proxmox VE 的 [Proxmox Backup Server](https://proxmox.com/en/products/proxmox-backup-server/overview)
2222

2323
!!! warning "单纯的快照与 RAID 都不是备份"
@@ -288,3 +288,109 @@ rsync 支持利用 [rrsync 脚本][rrsync.1]限制使用 SSH 连接的用户只
288288
### 文件备份 {#rsync-backup}
289289

290290
Rsync 本身不是完整的备份工具,其没有版本管理功能,因此如果某个文件被误删除/修改,那么 rsync 会将这个变化同步到备份中。不过基于 rsync 高效复制文件的能力,有工具实现了基于 rsync 和文件系统硬链接功能的备份,例如 Linux Mint 的 [Timeshift](https://github.com/linuxmint/timeshift) 项目就通过硬链接实现不同时间点备份的去重操作,而 rsync 负责文件的复制。
291+
292+
## 备份配置示例 {#backup-examples}
293+
294+
以下是一些实际使用的备份配置示例(其中一些严格意义来说,不是完整的备份方案),以供读者参考。
295+
296+
### 使用 restic 备份到 Backblaze B2 {#restic-backblaze}
297+
298+
restic 是一款现代的备份工具,支持使用包括本地、SFTP、S3 等多种后端存储备份数据,同时支持加密、多主机备份、版本管理等功能。Backblaze B2 云对象存储则以实惠的价格(截至写作时,每月存储开销 6 美元/TB;下载量超过存储 3 倍后流量收费 0.01 美元/GB)与对自动化程序友好的 API 成为了个人用户的热门选择。
299+
300+
首先在 Backblaze 处创建桶和 Application Key,记录下 Bucket 名称和 Application Key 的 ID 和 Key。
301+
302+
!!! note "为每台主机设置单独的 Application Key"
303+
304+
为了安全起见,建议为每台需要备份的主机创建单独的 Application Key,以便单独吊销。
305+
306+
之后设置环境变量并初始化仓库:
307+
308+
```shell
309+
export B2_ACCOUNT_ID="your_account_id"
310+
export B2_ACCOUNT_KEY="your_account_key"
311+
export RESTIC_REPOSITORY="b2:your_bucket_name:/"
312+
export RESTIC_PASSWORD="your_strong_password" # restic 使用这个密码加密备份数据
313+
export RESTIC_HOST="your_hostname" # 用于区分不同主机的备份
314+
315+
restic init
316+
```
317+
318+
!!! tip "也可使用 S3 兼容模式连接 Backblaze B2"
319+
320+
事实上,由于使用的第三方库的错误处理问题,[restic 官方文档目前更建议使用 S3 兼容模式连接 Backblaze B2](https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html#backblaze-b2):
321+
322+
```shell
323+
export AWS_ACCESS_KEY_ID="your_account_id"
324+
export AWS_SECRET_ACCESS_KEY="your_account_key"
325+
# 以下 endpoint 需要根据桶所在的实际区域修改
326+
export RESTIC_REPOSITORY="s3:https://s3.us-east-005.backblazeb2.com/your_bucket_name"
327+
# ...
328+
```
329+
330+
有关 S3 兼容模式的注意事项,请参阅上述文档。
331+
332+
之后就可以备份了,不过先让我们排除一些不需要备份的目录:
333+
334+
```shell title="excludes"
335+
.local/share/Trash/
336+
.cache/
337+
.local/share/Steam/steamapps/
338+
target/
339+
node_modules/
340+
.cargo/registry/
341+
.cargo/git/
342+
```
343+
344+
以上的排除项是针对备份个人电脑的家目录设置的,排除了回收站、缓存、Steam 游戏库、Rust 与 Node.js 的依赖与构建产物等目录。可以根据自己的实际情况调整。
345+
346+
之后就可以执行备份了:
347+
348+
```shell
349+
restic backup \
350+
--one-file-system \
351+
--exclude-file=/path/to/excludes \
352+
/home/username
353+
```
354+
355+
备份完成后,可以使用以下命令查看备份状态:
356+
357+
```shell
358+
# 查看所有的备份
359+
restic snapshots
360+
# 查看某个备份下的所有文件
361+
restic ls <snapshot_id>
362+
# 使用 FUSE 挂载备份
363+
restic mount /path/to/mountpoint
364+
```
365+
366+
备份积累到一定程度后,可以参考[以下命令](https://restic.readthedocs.io/en/latest/060_forget.html#removing-snapshots-according-to-a-policy)清理旧的备份:
367+
368+
```shell
369+
# 每个主机保留最近 100 个备份
370+
restic forget --group-by=host --keep-last 100
371+
```
372+
373+
建议定时进行备份,以下是一个参考的 systemd timer 配置。其中 `/etc/restic/env` 存储环境变量信息(记得限制权限!),`/usr/local/bin/restic-backup.sh` 则是调用上述备份命令的脚本。
374+
375+
```ini title="restic-backup.service"
376+
[Unit]
377+
Description=Backup to B2 by restic
378+
379+
[Service]
380+
Type=oneshot
381+
EnvironmentFile=/etc/restic/env
382+
ExecStart=/usr/local/bin/restic-backup.sh
383+
```
384+
385+
```ini title="restic-backup.timer"
386+
[Unit]
387+
Description=Run restic backup daily with random delay
388+
389+
[Timer]
390+
OnCalendar=daily
391+
RandomizedDelaySec=3600
392+
Persistent=true
393+
394+
[Install]
395+
WantedBy=timers.target
396+
```

0 commit comments

Comments
 (0)