Loading... # Restic 本地目录备份实战指南 ## 初始化仓库 ```bash # 创建备份仓库 restic init --repo /backup/repo # 使用密码文件(推荐用于自动化) restic init --repo /backup/repo --password-file /etc/restic/password ``` ## 日常备份 ```bash # 基础备份 restic -r /backup/repo backup /data # 带排除规则 restic -r /backup/repo backup /data \ --exclude="*.tmp" \ --exclude-file=/etc/restic/excludes.txt # 带标签(便于管理) restic -r /backup/repo backup /data --tag daily --tag production ``` ## 检查与验证 ```bash # 快速检查仓库结构 restic -r /backup/repo check # 完整校验(含数据完整性) restic -r /backup/repo check --read-data # 抽样校验(生产环境推荐) restic -r /backup/repo check --read-data-subset=5% ``` ## 查看快照 ```bash # 列出所有快照 restic -r /backup/repo snapshots # 按标签过滤 restic -r /backup/repo snapshots --tag daily # 查看快照内容 restic -r /backup/repo ls latest restic -r /backup/repo ls abc123 /data/config ``` ## 恢复数据 ```bash # 恢复到原路径 restic -r /backup/repo restore latest --target / # 恢复到指定目录 restic -r /backup/repo restore latest --target /restore # 恢复特定文件/目录 restic -r /backup/repo restore latest --target /restore --include /data/config # 从指定快照恢复 restic -r /backup/repo restore abc123 --target /restore ``` ## 恢复为副本(不覆盖原文件) ```bash # 方法1:恢复到独立目录 restic -r /backup/repo restore latest --target /data_replica_$(date +%Y%m%d) # 方法2:挂载后复制(推荐,按需取用) mkdir -p /mnt/restic restic -r /backup/repo mount /mnt/restic & cp -a /mnt/restic/snapshots/latest/data /data_replica fusermount -u /mnt/restic ``` ## 清理与维护 ```bash # 按策略保留快照 restic -r /backup/repo forget \ --keep-daily 7 \ --keep-weekly 4 \ --keep-monthly 6 \ --prune # 仅标记删除(不清理空间) restic -r /backup/repo forget --keep-last 10 # 单独清理未引用数据 restic -r /backup/repo prune ``` ## 自动化脚本示例 ```bash #!/bin/bash export RESTIC_REPOSITORY=/backup/repo export RESTIC_PASSWORD_FILE=/etc/restic/password restic backup /data --tag auto --quiet restic forget --keep-daily 7 --keep-weekly 4 --prune --quiet restic check --quiet || echo "Check failed" | mail -s "Restic Alert" admin@example.com ``` ## 核心原则 | 场景 | 命令 | 关键参数 | |------|------|----------| | 备份 | `backup` | `--exclude`, `--tag` | | 验证 | `check` | `--read-data-subset` | | 恢复原位 | `restore` | `--target /` | | 恢复副本 | `restore` | `--target /新路径` | | 清理 | `forget --prune` | `--keep-*` | **要点**:restic 自动去重、加密、增量。保持仓库定期 `check`,`forget` 后务必 `prune` 释放空间。 最后修改:2026 年 01 月 30 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏