Loading... # Restic SSH 远程备份实战指南 ## 环境准备 ```bash # 设置仓库变量 export RESTIC_REPOSITORY="sftp:user@host:/path/to/repo" export RESTIC_PASSWORD="your-password" # 或使用 RESTIC_PASSWORD_FILE ``` ## 核心操作 ### 1. 初始化仓库 ```bash restic init ``` ### 2. 日常备份 ```bash # 基础备份 restic backup /data --exclude="*.tmp" # 带标签备份 restic backup /data --tag daily # 静默备份(适合 cron) restic backup /data -q ``` ### 3. 快照管理 ```bash # 列出快照 restic snapshots # 按标签过滤 restic snapshots --tag daily ``` ### 4. 仓库检查 ```bash # 快速检查(仅元数据) restic check # 完整校验(含数据块) restic check --read-data ``` ### 5. 恢复操作 ```bash # 完整恢复 restic restore latest --target /restore # 恢复指定快照 restic restore abc123 --target /restore # 恢复特定文件 restic restore latest --target /restore --include "/data/config/*" ``` ### 6. 恢复为副本(不覆盖原文件) ```bash # 恢复到独立目录 restic restore latest --target /data_restored_$(date +%Y%m%d) # 挂载后选择性恢复 restic mount /mnt/restic & cp -a /mnt/restic/snapshots/latest/data/file.txt /data/file.txt.bak umount /mnt/restic ``` ## 自动化配置 ### Cron 任务示例 ```bash # /etc/cron.d/restic-backup 0 2 * * * root RESTIC_REPOSITORY="sftp:user@host:/repo" RESTIC_PASSWORD_FILE=/etc/restic-pass restic backup /data -q && restic forget --keep-daily 7 --keep-weekly 4 --prune -q ``` ### 快照保留策略 ```bash restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune ``` ## 故障排查 | 问题 | 解决方案 | |------|----------| | SSH 连接超时 | 使用 `-o sftp.command="ssh -o ConnectTimeout=30"` | | 仓库损坏 | `restic repair index` → `restic repair snapshots` | | 锁定残留 | `restic unlock` | ## 性能优化 ```bash # 限制带宽(避免占满网络) restic backup /data --limit-upload 5000 # KB/s # 并发上传 restic backup /data -o sftp.connections=5 ``` --- **关键原则**: 备份 ≠ 完成,验证恢复才是闭环。定期执行 `restic check --read-data` 确保数据可恢复。 最后修改:2026 年 01 月 30 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏