使用 Restic 备份数据
- 2025-01-03 15:34:27
- 丁国栋
- 原创 700
Restic 设计的原则是简单、快速、可验证、安全和高效,适合个人用户和企业环境中的各种备份需求。
- Restic 是一款 GO 语言开发的,开源、免费,快速、高效且安全的跨平台备份工具。
- Restic 支持备份增量数据,并且操作十分简单、可以随时备份和恢复备份。
- Restic 使用加密技术来保证数据安全性,可以将本地数据加密后传输到指定的存储。
- Restic 使用类似Git的版本管理保证数据的完整性,每次备份都是一个快照,用户可以查看、恢复或删除特定时间点的数据状态。
- Restic 支持大多数主流操作系统,比如:Linux、macOS、Windows 以及一些小众的操作系统,比如:FreeBSD 和 OpenBSD 等。
- Restic 支持多种后端存储,可以将备份存储在本地磁盘、SFTP、REST Server、Amazon S3、Minio、OpenStack Swift、Backblaze B2、Microsoft Azure Blob Storage、Google Cloud Storage 等。
- Restic 提供了方便易用的命令行参数,例如排除列表、传输带宽限制等。
Restic 可以通过 SSH 协议将文件存储到其他主机实现异地备份。
Restic 可以通过 S3 协议对接到对象存储实现数据备份和恢复。
案例1:通过restic备份数据到腾讯云对象存储(Cloud Object Storage,COS)
编辑 /etc/profile 文件,添加以下内容
export AWS_ACCESS_KEY_ID=AKIDXXXX export AWS_SECRET_ACCESS_KEY=lDXXXX export RESTIC_PASSWORD='passowrd'
注:restic 仓库地址也可以通过环境变量 RESTIC_REPOSITORY 指定。
初始化仓库
/usr/bin/restic -r s3:https://cos.ap-shanghai.myqcloud.com/BucketName-appid/hostname/data/sites/ init
备份数据
/usr/bin/restic -r s3:https://cos.ap-shanghai.myqcloud.com/BucketName-appid/hostname/data/sites/ backup /data/sites -v --exclude="*.tmp" --exclude=".cache/*" --exclude="*.log"
清理过期的备份
例如仅保留14天可以如下
/usr/bin/restic -r s3:https://cos.ap-shanghai.myqcloud.com/BucketName-appid/hostname/data/sites/ forget --keep-daily 14 --prune -v
案例2:使用restic备份数据到Azure Storage Account
首先需要使用或创建一个Azure Storage Account,使用或创建一个Container,权限设置为Private (no anonymous access) 即可。
要使用resitc备份数据到Azure,尽可能使用较新的restic发布版本,有的发行版自带的restic可能版本较低,无法使用。
例如如果使用版本较低则会提示如下所示的 invalid backend
root@hkweb:~# restic version restic 0.16.4 compiled with go1.22.2 on linux/amd64 root@hkweb:~# restic -r azure:backup:/hkweb/ init enter password for new repository: enter password again: Fatal: create repository at azure:backup:/hkweb/ failed: invalid backend If the repository is in a local directory, you need to add a `local:` prefix root@hkweb:~#安装restic stable的版本
wget https://github.com/restic/restic/releases/download/v0.18.0/restic_0.18.0_linux_amd64.bz2 bzip2 -d restic_0.18.0_linux_amd64.bz2 mv restic_0.18.0_linux_amd64 /usr/bin/restic chmod +x /usr/bin/restic
配置环境变量存储Azure Storage Account认证信息和restic仓库的密码
export AZURE_ACCOUNT_NAME="xxx" # AZURE_ACCOUNT_KEY 可以在 Azure Storage Account|Access keys 找到,Storage account name export AZURE_ACCOUNT_KEY="xxx" # AZURE_ACCOUNT_KEY 可以在 Azure Storage Account|Access keys 找到,key1或key2的key export RESTIC_PASSWORD='password'
初始化仓库
backup是container名称,/hkweb/ 是contianer中一个目录,这个目录可以不存在,restic会自动创建
restic -r azure:backup:/hkweb/ init
备份
/usr/bin/restic -r azure:backup:/hkweb/ backup /data/sites -v --exclude="*.tmp" --exclude=".cache/*" --exclude="*.log" /usr/bin/restic -r azure:backup:/hkweb/ backup /data/backup/mysql -v /usr/bin/restic -r azure:backup:/hkweb/ forget --keep-daily 14 --prune -v
其他常用的命令
查看快照列表 (Snapshots)
列出仓库中所有的备份快照,用于确定需要恢复的版本。
restic snapshots
恢复数据 (Restore)
将指定的快照恢复到目标目录。
restic restore latest --target /path/to/restore # 或者指定具体的快照ID restic restore <snapshot-id> --target /path/to/restore
检查完整性 (Check)
验证仓库数据的完整性,确保没有损坏。
restic check
挂载仓库 (Mount)
将备份仓库挂载到本地文件系统,像浏览普通文件夹一样查看备份内容(需要安装 restic mount依赖)。
restic mount /mnt/point
--
评论列表
匿名
2025-07-13 16:03:32
Email: ****@**** IP: 223.*.*.81 (山东/青岛)
回复
如果是定期执行备份任务除了要做到定期清理之外,还需要注意排他锁的使用,借助排他锁可以实现在任何时段,始终最多只有一个备份任务在执行,而不会出现多个备份任务在同时进行或者等待执行的情况。此时可以使用 flock 工具,结合 -n 参数(非阻塞模式,如果没有获得锁立即退出)使用。
1/1
发表评论