开源备份软件Restic简单教程

本文最后更新于 130 天前, 如有失效请评论区留言.

开源备份软件Restic简单教程

Restic 是一个快速、安全、高效的备份工具,特别适用于存储在不同位置的数据

安装

该工具提供多平台安装部署

macOS

brew install restic

Debian

apt update
apt install restic

Alpine

apk add restic

其他

其他系统可以参考官网文档

升级

除了包安装的方式升级外,还支持自身升级

restic self-update

补全命令行

执行如下命令可以使我们在使用 restic 时参数自动补全,提升使用效率

restic generate --bash-completion /etc/bash_completion.d/restic

存储库

安装好了Restic后,还需要配置下存储方式(本地或者远程), 配置过程中都会要你输入密码。

记住密码很重要!如果你失去了它,你就不会 能够访问存储库中存储的数据.

为了自动向 restic 提供存储库密码,有几个选项支持配置:

  • 设置环境变量RESTIC_PASSWORD
  • 通过选项或环境变量使用密码指定文件的路径--password-file RESTIC_PASSWORD_FILE
  • 配置在需要密码时通过 option 或环境变量--password-command RESTIC_PASSWORD_COMMAND

接下来介绍一下我常用的存储方式

Local本地存储

备份到本地/data/restic-repo

restic init --repo /data/restic-repo
enter password for new repository:
enter password again:
created restic repository b8107af4fd at /data/restic-repo

Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.

S3协议存储

远程备份, 备份到支持S3协议的对象存储中,如minio

export AWS_ACCESS_KEY_ID=<MY_ACCESS_KEY>
export AWS_SECRET_ACCESS_KEY=<MY_SECRET_ACCESS_KEY>

restic -r s3:http://localhost:9000/restic init

利用rclone

restic本身支持的存储库有限,但是rclone支持多,于是乎支持rclone集成

restic -r rclone:foo:bar init

更多可以参考官方文档 other-services-via-rclone

备份

目录中特定时间点的内容在 Restic 中称为快照

以本地备份为例进行演示说明:

# 第一次备份数据
restic -r /data/restic-repo backup /etc/sb
enter password for repository:
repository b8107af4 opened successfully, password is correct
created new cache in /root/.cache/restic

Files:          77 new,     0 changed,     0 unmodified
Dirs:           56 new,     0 changed,     0 unmodified
Added to the repo: 113.555 KiB

processed 77 files, 65.949 KiB in 0:00
snapshot aa2f8849 saved

# 再次运行backup命令,Retic将创建数据的另一个快照
restic -r /data/restic-repo backup /etc/sb
enter password for repository:
repository b8107af4 opened successfully, password is correct

Files:           0 new,     0 changed,    77 unmodified
Dirs:            0 new,     0 changed,    56 unmodified
Added to the repo: 0 B

processed 77 files, 65.949 KiB in 0:00
snapshot c8b23ba8 saved

也可以备份同一个存储库中的单个文件

restic -r /data/restic-repo --verbose backup /etc/sb/hy2.yaml
open repository
enter password for repository:
repository b8107af4 opened successfully, password is correct
lock repository
load index files
start scan on [/etc/sb/hy2.yaml]
start backup on [/etc/sb/hy2.yaml]
scan finished in 0.207s: 1 files, 265 B

Files:           1 new,     0 changed,     0 unmodified
Dirs:            2 new,     0 changed,     0 unmodified
Data Blobs:      1 new
Tree Blobs:      3 new
Added to the repo: 1.330 KiB

processed 1 files, 265 B in 0:00
snapshot 822f2e6c saved

相关常用参数

--exclude 指定一次或多次排除一个或多个项
--exclude-file指定一次排除包含特殊文件的文件夹
--exclude-if-present foo 排除文件夹包含名为 foo 的文件,不支持文件名的通配符
--exclude-larger-than size指定一次以排除大于给定大小的文件

操作使用存储库

列出所有快照

restic -r /data/restic-repo snapshots
enter password for repository:
repository b8107af4 opened successfully, password is correct
ID        Time                 Host        Tags        Paths
-----------------------------------------------------------------------
aa2f8849  2024-07-14 06:54:47  nat4                    /etc/sb
c8b23ba8  2024-07-14 06:57:08  nat4                    /etc/sb
822f2e6c  2024-07-14 06:59:27  nat4                    /etc/sb/hy2.yaml
-----------------------------------------------------------------------
3 snapshots

还支持过滤策略

restic -r /data/restic-repo snapshots --path "/etc/sb"
enter password for repository:
repository b8107af4 opened successfully, password is correct
ID        Time                 Host        Tags        Paths
--------------------------------------------------------------
aa2f8849  2024-07-14 06:54:47  nat4                    /etc/sb
c8b23ba8  2024-07-14 06:57:08  nat4                    /etc/sb
--------------------------------------------------------------
2 snapshots

验证

运行检查命令来验证所有数据都正确地存储在存储库中了,应该定期运行此命令,以确保存储库的内部结构没有错误

restic check -r /data/restic-repo

using temporary cache in /tmp/restic-check-cache-106234855
enter password for repository:
repository b8107af4 opened successfully, password is correct
created new cache in /tmp/restic-check-cache-106234855
create exclusive lock for repository
load indexes
check all packs
check snapshots, trees and blobs
no errors were found

默认情况下不检测数据库磁盘上文件是否被修改,以使用restic命令也验证存储库中包文件的完整性

restic -r /data/restic-repo check --read-data
using temporary cache in /tmp/restic-check-cache-770072337
enter password for repository:
repository b8107af4 opened successfully, password is correct
created new cache in /tmp/restic-check-cache-770072337
create exclusive lock for repository
load indexes
check all packs
check snapshots, trees and blobs
read all data
[0:00] 100.00%  4 / 4 packs
no errors were found

从备份恢复数据

从快照中恢复数据

restic -r /data/restic-repo restore c8b23ba8 --target /tmp/restore-work
enter password for repository:
repository b8107af4 opened successfully, password is correct
restoring <Snapshot c8b23ba8 of [/etc/sb] at 2024-07-14 06:57:08.982881207 -0400 EDT by root@nat4> to /tmp/restore-work

恢复部分文件

restic -r /data/restic-repo restore latest --target /tmp/restore-work2 --path "/etc/sb" --host nat4
enter password for repository:
repository b8107af4 opened successfully, password is correct
restoring <Snapshot c8b23ba8 of [/etc/sb] at 2024-07-14 06:57:08.982881207 -0400 EDT by root@nat4> to /tmp/restore-work2

标准输出

我的简单理解就是导出文件,用的不多

restic -r /data/restic-repo dump latest /etc/sb/hy2.yaml > /tmp/hy2.yaml

删除备份的快照

指定快照ID来删除快照forget ID

root@nat4:~# restic -r /data/restic-repo snapshots
enter password for repository:
repository b8107af4 opened successfully, password is correct
ID        Time                 Host        Tags        Paths
-----------------------------------------------------------------------
aa2f8849  2024-07-14 06:54:47  nat4                    /etc/sb
c8b23ba8  2024-07-14 06:57:08  nat4                    /etc/sb
822f2e6c  2024-07-14 06:59:27  nat4                    /etc/sb/hy2.yaml
-----------------------------------------------------------------------
3 snapshots
root@nat4:~# restic -r /data/restic-repo forget c8b23ba8
enter password for repository:
repository b8107af4 opened successfully, password is correct
[0:00] 100.00%  1 / 1 files deleted
root@nat4:~# restic -r /data/restic-repo snapshots
enter password for repository:
repository b8107af4 opened successfully, password is correct
ID        Time                 Host        Tags        Paths
-----------------------------------------------------------------------
aa2f8849  2024-07-14 06:54:47  nat4                    /etc/sb
822f2e6c  2024-07-14 06:59:27  nat4                    /etc/sb/hy2.yaml
-----------------------------------------------------------------------
2 snapshots

虽然上述命令将快照删除了,但文件引用的数据仍然存储在存储库中, 要清除未引用的数据,必须运行prune命令

restic -r /data/restic-repo prune

当然也可以综合这两步一起操作

# 仅保留最新的快照
restic forget  -r /data/restic-repo --keep-last 1 --prune

根据策略删除快照

根据策略删除快照这个用法会多一些

  • 可以指定保留多少小时/每日/每周/每月和每年快照,以及删除多少其他快照
  • 可以使用 --dry-run 参数来测试需要删除的文件和目录列表
--keep-last n 保留最后(最新)n个快照
--keep-hourly n 不删除快照的最后 n 个小时;每小时只保留最后一个快照
--keep-daily n 不删除快照的最后 n 个天;每小时只保留最后一个快照
--keep-weekly n 不删除快照的最后 n 个周;每小时只保留最后一个快照

更多的可以参考官方文档060_forget

保留每天一个快照,最多保留最近的 4 天的快照。举个例子,如果你有 10 天的备份,这个选项会删除除了最近 4 天以外的每天的备份

restic -r /data/restic-repo  forget --keep-daily 4 --dry-run

最后

到这里基本的使用基本就完成了,其他还是挺简单的。

Sponsor

Like this article? $1 reward

Comments