乐于分享
好东西不私藏

开源备份神器 Restic 完全指南:安装、配置与实战

开源备份神器 Restic 完全指南:安装、配置与实战

前言

数据备份这件事,说起来简单做起来难。市面上工具虽多,但要么配置复杂,要么速度感人,要么价格劝退。

今天介绍的开源备份工具 Restic,号称”备份界的瑞士军刀”——快速、安全、跨平台、支持多种存储后端,而且完全免费。

这是它的github参考链接,

https://github.com/restic/restic

先看几个亮点:

  • 增量备份 + 去重:只传变化的数据,节省带宽和存储
  • 端到端加密:数据在客户端加密后再上传,服务商也看不到你的数据
  • 多后端支持:本地目录、SFTP、S3、MinIO、主流公有云平台等
  • 恢复灵活:可恢复整个快照,也可挂载为文件系统按需提取
  • 全平台覆盖:Linux、macOS、Windows 统统支持

安装
Restic 的安装方式非常灵活,几乎覆盖了所有主流平台。我使用的系统Ubuntu 22.04,安装只需要一条命令,
sudo apt install restic
其他平台可以参考Github链接。

核心概念:仓库(Repository)

在开始之前,需要理解 Restic 的核心概念——仓库(Repository)

仓库是 Restic 存储备份数据的地方,包含加密后的文件、元数据和索引。访问仓库需要密码——这个密码极其重要,密码丢失 = 数据永远丢失,没有找回途径。

仓库的初始化命令格式:

restic init --repo <仓库位置>
密码可以通过以下方式提供(按优先级排序):
- 环境变量:  RESTIC_PASSWORD
- 密码文件: --password-file
- 密码命令: --password-command
- 交互式输入:常用这个

使用
Restic的存储后端有很多,这里只演示两种:Local和Minio
场景一:Local本地仓库
1,初始化本地仓库
# 创建仓库在当前目录restic init --repo resticrepo 
系统会提示输入两次密码,成功后会显示:
2,创建备份
比如我需要将目录maintain备份到上步创建的本地仓库resticrepo:
aneirin@xuliat14p:~$ restic --repo resticrepo backup ./maintainenter password for repository: repository 555939fa opened successfully, password is correctcreated new cache in /home/aneirin/.cache/resticno parent snapshot found, will read all filesFiles:         275 new,     0 changed,     0 unmodifiedDirs:           76 new,     0 changed,     0 unmodifiedAdded to the repo: 1.056 MiBprocessed 275 files, 950.681 KiB in 0:00snapshot ce851018 saved

3,恢复

如果后面误删了目录maintain,使用下面命令恢复,

# 列出快照,因为只备份一次,所以只有一个快照aneirin@xuliat14p:~restic --repo resticrepo/ snapshots enter password for repository: repository 555939fa opened successfully, password is correctID        Time                 Host        Tags        Paths----------------------------------------------------------------------------------------ce851018  2026-06-09 10:27:19  xuliat14p               /home/aneirin/maintain----------------------------------------------------------------------------------------1 snapshots############################################################### 恢复指定快照aneirin@xuliat14p:~restic --repo ./resticrepo/ restore ce851018 --target maintainenter password for repository: repository 555939fa opened successfully, password is correctrestoring <Snapshot ce851018 of [/home/aneirin/maintain] at 2026-06-09 10:27:19.616097325 +0800 CST by aneirin@xuliat14p> to maintain
4,其他
也可以选择性恢复,
# 只恢复特定目录restic --repo /data/backup/restic-repo restore latest --target /tmp/restore --include /work# 恢复快照中的特定子目录restic --repo /data/backup/restic-repo restore b023d7a3:/work --target /tmp/restore

挂载为文件系统,

mkdir /mnt/resticrestic --repo /data/backup/restic-repo mount /mnt/restic

比较快照差异,

restic --repo /data/backup/restic-repo diff b023d7a3 40dc1520

查看仓库状态,

restic --repo /data/backup/restic-repo stats
场景二:MinIO对象存储
MinIO 是一个开源的高性能对象存储系统,兼容 Amazon S3 API。通过 MinIO,你可以搭建属于自己的私有云备份方案。Minio的搭建过程可以参考官方文档。
1,创建存储桶

在 MinIO 控制台中创建一个名为 restic-backup 的存储桶(Bucket),或者使用命令行:

mc alias set myminio http://localhost:9000 admin YourPassword123mc mb myminio/restic-backup

2,配置环境变量

export AWS_ACCESS_KEY_ID=adminexport AWS_SECRET_ACCESS_KEY=YourPassword123# 将仓库地址写入环境变量,后续命令更简洁export RESTIC_REPOSITORY=s3:http://localhost:9000/restic-backupexport RESTIC_PASSWORD=your-strong-password

3,初始化MinIO仓库

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

4,备份到MinIO

restic -r s3:http://localhost:9000/restic-backup backup /home/user/documents

或者使用环境变量简化:

restic backup /home/user/documents

5,从MinIO恢复

# 列出快照restic snapshots# 恢复restic restore latest --target /tmp/restore

写到最后

Restic 是一个真正让人安心的备份工具——安装简单、命令直观、功能强大。无论你是个人用户想备份家庭照片,还是运维人员需要保护服务器数据,Restic 都能胜任。

它的设计哲学值得点赞:加密是你的、数据是你的、控制权也是你的。搭配 MinIO 搭建私有备份方案,既避免了云服务商的锁定,又享受了对象存储的可靠性。


如果你觉得这篇文章有帮助,欢迎点赞、转发!