关于 backup-recovery
面向服务器和应用数据管理者的备份与恢复技能,涵盖 rsync、Restic、本地及云存储后端、自动调度和恢复验证。
主要功能
- 01使用 rsync 和 Restic 备份数据
- 02支持本地、S3 和 B2 后端
- 03通过 Restic 加密与去重
- 04使用 systemd timer 或 cron 调度
- 05管理快照和保留策略
- 06恢复数据并检查仓库完整性
适用场景
- 01为服务器或应用数据设计备份方案
- 02定时备份到本地或云存储
- 03误删或灾难后恢复数据
- 04验证备份完整性并测试恢复流程
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ bagelhole/ devops-security-agent-skills --skill backup-recovery安装后在 Claude Code 中输入/backup-recovery确认已加载。
详细介绍
备份与恢复
使用 rsync、Restic 和云存储后端实施全面的备份与恢复策略。涵盖 3-2-1 规则、自动化调度、S3/B2 后端、加密、恢复流程和验证测试。
适用场景
- 为服务器、数据库或应用数据设计备份策略
- 设置 Restic,将数据加密并去重后备份到本地或云存储
- 使用 systemd 定时器或 cron 自动执行备份
- 在意外删除、数据损坏或灾难发生后恢复数据
- 使用备份/恢复工作流在不同环境之间迁移数据
- 验证备份完整性并测试恢复流程
前提条件
- 已安装
rsync(大多数 Linux 发行版均已包含) - 已安装
restic(建议使用 v0.16+) - 已为后端配置云 CLI:S3 使用 AWS CLI,Backblaze B2 使用
b2CLI - 备份目标有足够的存储空间(保留数据时需要源数据大小的 2-3 倍)
- 可通过 SSH 访问远程 rsync 目标
- 用于调度任务的
systemd或cron
3-2-1 备份规则
- 3 份数据副本(1 份主副本 + 2 份备份)
- 2 种不同的存储介质或类型(例如,本地磁盘 + 云存储)
- 1 份异地副本(云存储、远程数据中心)
rsync 备份
基本操作
# Sync a local directory to a backup location
rsync -avz --delete /data/ /backup/data/
# Flags explained:
# -a archive mode (preserves permissions, ownership, timestamps, symlinks)
# -v verbose output
# -z compress data during transfer
# --delete remove files at destination that no longer exist at source
# Sync to a remote server over SSH
rsync -avz -e "ssh -i ~/.ssh/backup_key" /data/ backup@remote:/backups/server01/
# Exclude patterns
rsync -avz --delete \
--exclude='*.tmp' \
--exclude='*.log' \
--exclude='.cache/' \
--exclude='node_modules/' \
/data/ /backup/data/
# Use an exclude file for complex patterns
rsync -avz --delete --exclude-from=/etc/backup-excludes.txt /data/ /backup/data/
# Dry run (preview what would change)
rsync -avzn --delete /data/ /backup/data/
# Limit bandwidth to 50 MB/s and show progress
rsync -avz --bwlimit=50000 --progress /data/ backup@remote:/backups/
使用硬链接进行增量备份
# Incremental: unchanged files hard-linked to previous backup (saves space)
rsync -avz --delete \
--link-dest=/backup/daily/latest \
/data/ /backup/daily/$(date +%Y-%m-%d)/
# Update the 'latest' symlink
ln -snf /backup/daily/$(date +%Y-%m-%d) /backup/daily/latest
# Remove backups older than 30 days
find /backup/daily -maxdepth 1 -type d -name "20*" -mtime +30 -exec rm -rf {} \;
Restic 备份
安装
# Debian / Ubuntu
apt install -y restic
# RHEL / CentOS
dnf install -y restic
# Or download the latest binary
curl -L https://github.com/restic/restic/releases/latest/download/restic_0.17.3_linux_amd64.bz2 \
| bunzip2 > /usr/local/bin/restic
chmod +x /usr/local/bin/restic
# Verify installation
restic version
初始化仓库
# Local repository
restic init --repo /backup/restic-repo
# AWS S3 backend
export AWS_ACCESS_KEY_ID="AKIAEXAMPLE"
export AWS_SECRET_ACCESS_KEY="secretkey"
restic init --repo s3:s3.amazonaws.com/my-backup-bucket
# S3-compatible (MinIO)
export AWS_ACCESS_KEY_ID="minioadmin"
export AWS_SECRET_ACCESS_KEY="miniosecret"
restic init --repo s3:http://minio.example.com:9000/backup-bucket
# Backblaze B2 backend
export B2_ACCOUNT_ID="accountid"
export B2_ACCOUNT_KEY="accountkey"
restic init --repo b2:my-backup-bucket:server01
# SFTP backend
restic init --repo sftp:backup@remote:/backups/server01
# Restic will prompt for a repository password -- store it securely
# Use a password file for automation
echo "my-secure-repo-password" > /etc/restic/password.txt
chmod 600 /etc/restic/password.txt
备份操作
# Basic backup
restic backup /data --repo /backup/restic-repo --password-file /etc/restic/password.txt
# Backup multiple directories
restic backup /data /etc /var/lib/postgresql \
--repo s3:s3.amazonaws.com/my-backup-bucket \
--password-file /etc/restic/password.txt
# Backup with exclusions
restic backup /data \
--exclude='*.tmp' \
--exclude='*.log' \
--exclude-file=/etc/restic/excludes.txt \
--repo /backup/restic-repo \
--password-file /etc/restic/password.txt
# Backup with tags (useful for filtering snapshots later)
restic backup /data \
--tag server01 --tag production --tag daily \
--repo /backup/restic-repo \
--password-file /etc/restic/password.txt
# Backup stdin (e.g., database dump)
pg_dump -U postgres mydb | restic backup --stdin --stdin-filename mydb.sql \
--repo s3:s3.amazonaws.com/my-backup-bucket \
--password-file /etc/restic/password.txt
# Verbose output showing files processed
restic backup /data -v \
--repo /backup/restic-repo \
--password-file /etc/restic/password.txt
快照管理
# List all snapshots (add --tag <tag> to filter)
restic snapshots --repo /backup/restic-repo --password-file /etc/restic/password.txt
# Browse files in the latest snapshot
restic ls latest --repo /backup/restic-repo --password-file /etc/restic/password.txt
# Compare two snapshots
restic diff abc123 def456 --repo /backup/restic-repo --password-file /etc/restic/password.txt
保留策略(forget + prune)
# Apply retention policy and reclaim space
restic forget \
--keep-daily 7 --keep-weekly 4 --keep-monthly 12 --keep-yearly 3 \
--prune \
--repo /backup/restic-repo \
--password-file /etc/restic/password.txt
# Dry run to preview what would be removed
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 \
--dry-run --repo /backup/restic-repo --password-file /etc/restic/password.txt
恢复流程
# Restore the latest snapshot to a target directory
restic restore latest --target /restore \
--repo /backup/restic-repo \
--password-file /etc/restic/password.txt
# Restore a specific snapshot by ID
restic restore abc123 --target /restore \
--repo /backup/restic-repo \
--password-file /etc/restic/password.txt
# Restore only specific files or directories
restic restore latest --target /restore --include "/data/config" \
--repo /backup/restic-repo \
--password-file /etc/restic/password.txt
# Mount a snapshot as a FUSE filesystem (browse and copy individual files)
mkdir -p /mnt/restic
restic mount /mnt/restic --repo /backup/restic-repo --password-file /etc/restic/password.txt &
# Browse: ls /mnt/restic/snapshots/latest/
# Unmount when done: umount /mnt/restic
验证
# Verify repository integrity (checks all data and metadata)
restic check --repo /backup/restic-repo --password-file /etc/restic/password.txt
# Full data verification (reads all pack files -- slow but thorough)
restic check --read-data --repo /backup/restic-repo --password-file /etc/restic/password.txt
# Verify a random subset of data (faster than full read)
restic check --read-data-subset=5% --repo /backup/restic-repo --password-file /etc/restic/password.txt
使用环境文件自动备份
/etc/restic/env
# Repository configuration
export RESTIC_REPOSITORY="s3:s3.amazonaws.com/my-backup-bucket"
export RESTIC_PASSWORD_FILE="/etc/restic/password.txt"
export AWS_ACCESS_KEY_ID="AKIAEXAMPLE"
export AWS_SECRET_ACCESS_KEY="secretkey"
# Optional: set cache directory
export RESTIC_CACHE_DIR="/var/cache/restic"
备份脚本
#!/bin/bash
# /usr/local/bin/restic-backup.sh
set -euo pipefail
source /etc/restic/env
LOG="/var/log/restic-backup.log"
echo "$(date): Starting backup" >> "$LOG"
restic backup /data /etc /var/lib/postgresql \
--exclude-file=/etc/restic/excludes.txt \
--tag "$(hostname)" --tag daily \
--verbose >> "$LOG" 2>&1
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 \
--prune >> "$LOG" 2>&1
# Weekly integrity check (Sundays)
[ "$(date +%u)" -eq 7 ] && restic check --read-data-subset=10% >> "$LOG" 2>&1
echo "$(date): Backup completed" >> "$LOG"
chmod +x /usr/local/bin/restic-backup.sh
定时备份
Systemd 定时器(推荐)
# /etc/systemd/system/restic-backup.service
[Unit]
Description=Restic backup
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
User=root
ExecStart=/usr/local/bin/restic-backup.sh
Nice=10
IOSchedulingClass=idle
# /etc/systemd/system/restic-backup.timer
[Unit]
Description=Run Restic backup daily at 2 AM
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
RandomizedDelaySec=900
[Install]
WantedBy=timers.target
# Enable and start the timer
systemctl daemon-reload
systemctl enable --now restic-backup.timer
# Check timer status
systemctl list-timers restic-backup.timer
# Run manually for testing
systemctl start restic-backup.service
journalctl -u restic-backup.service -f
使用 Restic 备份数据库
# PostgreSQL: stream dump directly into restic (no temp file)
pg_dump -U postgres -Fc mydb | restic backup --stdin --stdin-filename mydb.dump \
--tag postgres --tag mydb \
--repo s3:s3.amazonaws.com/my-backup-bucket \
--password-file /etc/restic/password.txt
# MySQL / MariaDB: stream dump into restic
mysqldump --all-databases --single-transaction | \
restic backup --stdin --stdin-filename all-databases.sql \
--tag mysql \
--repo s3:s3.amazonaws.com/my-backup-bucket \
--password-file /etc/restic/password.txt
# Restore PostgreSQL from restic
restic dump latest mydb.dump \
--repo s3:s3.amazonaws.com/my-backup-bucket \
--password-file /etc/restic/password.txt \
| pg_restore -U postgres -d mydb --clean --if-exists
故障排查
| 症状 | 诊断命令 | 常见修复方法 |
|---|---|---|
| “repository not initialized” | restic cat config --repo <repo> | 先运行 restic init --repo <repo> |
| “wrong password” | 检查环境变量 / 密码文件 | 检查 RESTIC_PASSWORD_FILE 的内容和权限 |
| 备份速度慢 | 使用 restic backup -v 查看进度 | 检查网络带宽;排除不需要备份的大型目录 |
| S3 权限被拒绝 | aws s3 ls s3://bucket/ | 检查 IAM 策略是否包含 s3:GetObject、s3:PutObject |
| “unable to create lock” | restic unlock --repo <repo> | 上一次备份异常中断;解锁仓库 |
| 恢复结果显示空目录 | restic ls <snapshot-id> | 确认快照 ID 正确;检查 --include 路径语法 |
| 仓库增长过大 | restic stats --repo <repo> | 使用更严格的保留策略运行 restic forget --prune |
| 检查因 pack 错误失败 | restic check --read-data | 重建索引:restic rebuild-index;从其他副本恢复 |
相关技能
linux-administration-- 服务器维护和日志管理systemd-services-- 使用 systemd 定时器安排备份object-storage-- 将 S3 和 MinIO 用作备份目标block-storage-- 使用 LVM 快照进行一致性备份nfs-storage-- 备份 NFS 共享数据
评论
支持 Markdown