Restic:文件系统备份恢复
如何使用 Restic 定期备份与恢复文件系统数据
Pigsty 已经处理好了 PostgreSQL 数据库本身的备份与恢复,但如何解决普通文件/目录的备份与恢复?
对于各种使用 PostgreSQL 的业务软件来说,(例如 Odoo,GitLab),您可以考虑使用 Restic 定期备份文件系统部分的数据(例如 /data/odoo
) 。
Restic 是一个非常好用的开源备份工具,支持快照,增量备份,加密,压缩等功能,并且可以使用包括 S3/MinIO 在内的多种服务作为备份仓库。 详细信息请参考 restic 文档。
快速上手
Pigsty Infra 仓库提供开箱即用的最新 Restic RPM/DEB 软件包,大部分 Linux 操作系统官方支持的发行版仓库里也提供较旧的版本。
yum install -y restic
apt install -y restic
出于演示目的,使用本地文件目录作为备份仓库,初始化仓库只需要执行一次即可
mkdir -p /data/backups/restic
export RESTIC_REPOSITORY=/data/backups/restic
export RESTIC_PASSWORD=some-strong-password
restic init
接下来,你可以进行备份,查看快照,恢复文件等操作:
restic backup /www/web.cc # 将 /www/web.cc 目录备份到仓库
restic snapshots # 查看备份快照列表
restic restore -t /tmp/web.cc 0b11f778 # 将快照 0b11f778 恢复到 /tmp/web.cc
restic check # 定期检查仓库完整性
完整命令输出
$ restic backup /www/web.cc
repository fcd37256 opened (repository version 2) successfully, password is correct
created new cache in /root/.cache/restic
no parent snapshot found, will read all files
Files: 5934 new, 0 changed, 0 unmodified
Dirs: 1622 new, 0 changed, 0 unmodified
Added to the repository: 1.570 GiB (1.167 GiB stored)
processed 5934 files, 1.694 GiB in 0:20
snapshot 0b11f778 saved
$ restic snapshots # 查看备份快照
repository fcd37256 opened (repository version 2) successfully, password is correct
ID Time Host Tags Paths
------------------------------------------------------------------
0b11f778 2025-03-19 15:25:21 pigsty.cc /www/web.cc
------------------------------------------------------------------
1 snapshots
$ restic backup /www/web.cc
repository fcd37256 opened (repository version 2) successfully, password is correct
using parent snapshot 0b11f778
Files: 0 new, 0 changed, 5934 unmodified
Dirs: 0 new, 0 changed, 1622 unmodified
Added to the repository: 0 B (0 B stored)
processed 5934 files, 1.694 GiB in 0:00
snapshot 06cd9b5c saved
[03-19 15:25:59] root@pigsty.cc:/data/backups
$ restic snapshots # 查看备份快照
repository fcd37256 opened (repository version 2) successfully, password is correct
ID Time Host Tags Paths
------------------------------------------------------------------
0b11f778 2025-03-19 15:25:21 pigsty.cc /www/web.cc
06cd9b5c 2025-03-19 15:25:58 pigsty.cc /www/web.cc
------------------------------------------------------------------
2 snapshots
$ restic restore -t /www/web.cc 0b11f778
repository fcd37256 opened (repository version 2) successfully, password is correct
restoring <Snapshot 0b11f778 of [/www/web.cc] at 2025-03-19 15:25:21.514089814 +0800 HKT by root@pigsty.cc> to /www/web.cc
使用对象存储
你可以使用 许许多多的方式来存储 Restic 的备份数据。这里介绍如何使用 Pigsty 自带的 MinIO 作为备份仓库。
export AWS_ACCESS_KEY_ID=minioadmin # MinIO 默认账号
export AWS_SECRET_ACCESS_KEY=minioadmin # MinIO 默认密码
restic -r s3:http://sss.pigsty:9000/infra init # 利用默认的 infra 桶作为备份目的地