搬瓦工 VPS 部署 Restic 增量备份方案教程
Restic 是一款快速、安全且高效的开源备份工具,使用 Go 语言编写,支持增量备份、数据加密和去重存储。Restic 可以将备份数据存储到本地磁盘、SFTP 服务器、S3 兼容存储等多种后端。对于运行在搬瓦工 VPS 上的网站和应用,建立可靠的备份方案至关重要。本教程将详细介绍 Restic 的安装、配置和自动化备份流程。
一、Restic 核心特性
- 增量备份:只备份变化的数据块,大幅节省存储空间和传输时间。
- 加密存储:所有备份数据使用 AES-256 加密,密码由用户掌控。
- 数据去重:基于内容定义的分块(CDC)算法,跨文件和跨快照去重。
- 多后端支持:本地目录、SFTP、S3、B2、Azure Blob、Google Cloud Storage 等。
- 快照管理:每次备份创建一个快照,支持按时间保留策略自动清理。
二、安装 Restic
# Ubuntu/Debian 安装
apt update
apt install restic -y
# 或下载最新版(推荐)
wget https://github.com/restic/restic/releases/download/v0.17.0/restic_0.17.0_linux_amd64.bz2
bzip2 -d restic_0.17.0_linux_amd64.bz2
mv restic_0.17.0_linux_amd64 /usr/local/bin/restic
chmod +x /usr/local/bin/restic
# 验证
restic version
三、初始化备份仓库
3.1 本地仓库
export RESTIC_REPOSITORY=/backup/restic-repo
export RESTIC_PASSWORD="your_backup_password_2026"
mkdir -p $RESTIC_REPOSITORY
restic init
3.2 SFTP 远程仓库
export RESTIC_REPOSITORY="sftp:backup-server:/backup/vps-backup"
export RESTIC_PASSWORD="your_backup_password_2026"
restic init
3.3 S3 兼容存储(如 MinIO、Backblaze B2)
export AWS_ACCESS_KEY_ID="your_access_key"
export AWS_SECRET_ACCESS_KEY="your_secret_key"
export RESTIC_REPOSITORY="s3:https://s3.example.com/bucket-name/restic"
export RESTIC_PASSWORD="your_backup_password_2026"
restic init
四、执行备份
4.1 基本备份
# 备份单个目录
restic backup /opt/app
# 备份多个目录
restic backup /opt/app /var/www /etc/nginx
# 备份时排除特定文件
restic backup /opt/app \
--exclude="*.log" \
--exclude="node_modules" \
--exclude=".git" \
--exclude="*.tmp"
# 使用排除文件
cat > /opt/restic/excludes.txt <<'EOF'
*.log
*.tmp
*.cache
node_modules
.git
__pycache__
*.pyc
EOF
restic backup /opt/app --exclude-file=/opt/restic/excludes.txt
4.2 数据库备份
# 备份 MySQL/MariaDB
mysqldump -u root --all-databases | restic backup --stdin --stdin-filename mysql-all.sql
# 备份 PostgreSQL
pg_dumpall -U postgres | restic backup --stdin --stdin-filename postgres-all.sql
# 备份 Docker 卷中的数据库
docker exec postgres pg_dumpall -U postgres | restic backup --stdin --stdin-filename docker-postgres.sql
五、快照管理
# 列出所有快照
restic snapshots
# 按路径筛选快照
restic snapshots --path /opt/app
# 查看快照详细信息
restic stats
# 查看某个快照中的文件列表
restic ls latest
# 对比两个快照的差异
restic diff snapshot_id_1 snapshot_id_2
六、数据恢复
# 恢复最新快照到指定目录
restic restore latest --target /opt/restore
# 恢复特定快照
restic restore abc12345 --target /opt/restore
# 只恢复特定文件或目录
restic restore latest --target /opt/restore \
--include "/opt/app/config"
# 挂载快照为文件系统浏览
apt install fuse -y
mkdir -p /mnt/restic
restic mount /mnt/restic &
# 然后可以在 /mnt/restic/snapshots/ 中浏览所有快照
七、保留策略与自动清理
# 按时间保留策略清理旧快照
restic forget \
--keep-last 5 \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 12 \
--keep-yearly 2 \
--prune
# 说明:
# --keep-last 5 保留最新的 5 个快照
# --keep-daily 7 保留最近 7 天每天一个快照
# --keep-weekly 4 保留最近 4 周每周一个快照
# --keep-monthly 12 保留最近 12 个月每月一个快照
# --keep-yearly 2 保留最近 2 年每年一个快照
# --prune 同时清理不再引用的数据块
八、自动化备份脚本
mkdir -p /opt/restic
cat > /opt/restic/backup.sh <<'EOF'
#!/bin/bash
set -euo pipefail
# 配置
export RESTIC_REPOSITORY="sftp:backup-server:/backup/vps-backup"
export RESTIC_PASSWORD_FILE="/opt/restic/.password"
EXCLUDE_FILE="/opt/restic/excludes.txt"
LOG_FILE="/var/log/restic-backup.log"
LOCK_FILE="/tmp/restic-backup.lock"
# 防止重复运行
if [ -f "$LOCK_FILE" ]; then
echo "备份正在运行中,跳过" >> "$LOG_FILE"
exit 0
fi
trap "rm -f $LOCK_FILE" EXIT
touch "$LOCK_FILE"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}
log "开始备份..."
# 备份数据库
docker exec postgres pg_dumpall -U postgres > /tmp/db-backup.sql 2>/dev/null
restic backup /tmp/db-backup.sql --tag database 2>> "$LOG_FILE"
rm -f /tmp/db-backup.sql
# 备份应用数据
restic backup /opt/app /var/www /etc/nginx \
--exclude-file="$EXCLUDE_FILE" \
--tag files 2>> "$LOG_FILE"
log "备份完成"
# 清理旧快照
restic forget \
--keep-last 5 \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
--prune 2>> "$LOG_FILE"
log "清理完成"
# 验证仓库完整性(每周一次)
if [ "$(date +%u)" -eq 1 ]; then
restic check 2>> "$LOG_FILE"
log "仓库完整性检查通过"
fi
EOF
chmod +x /opt/restic/backup.sh
# 创建密码文件
echo "your_backup_password_2026" > /opt/restic/.password
chmod 600 /opt/restic/.password
# 添加定时任务(每天凌晨 3 点执行)
echo "0 3 * * * /opt/restic/backup.sh" | crontab -
九、监控备份状态
# 检查最近备份时间
restic snapshots --latest 1
# 查看仓库统计信息
restic stats --mode raw-data
# 验证仓库完整性
restic check
# 查看备份日志
tail -50 /var/log/restic-backup.log
十、常见问题
备份速度慢
首次备份需要上传全部数据,后续增量备份速度会很快。如果网络带宽有限,可以先在本地完成首次备份,再传到远程仓库。
仓库损坏
# 检查并修复仓库
restic check --read-data
restic rebuild-index
总结
Restic 是搬瓦工 VPS 上理想的备份方案,安装简单、性能优秀、安全可靠。如果需要更高级的去重功能,可以参考 BorgBackup 教程;需要图形化管理,可以参考 Duplicati 教程。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,购买链接:bwh81.net。