Skip to content
backup-strategy logo

备份策略设计

backup-strategy

备份策略设计

SKILL.md

Full skill instructions

备份策略设计

概述

3-2-1 策略、备份验证、保留策略设计技能。

3-2-1 备份策略

核心原则

3 - 至少保留 3 份数据副本
2 - 存储在 2 种不同介质上
1 - 至少 1 份异地存储

扩展 3-2-1-1-0:
3 份副本
2 种介质
1 份异地
1 份离线/不可变
0 个错误(验证通过)

实施示例

# 本地备份(副本 1)
tar -czvf /​backup/​local/​data_$(date +%Y%m%d).tar.gz /​data

# NAS 备份(副本 2,不同介质)
rsync -avz /​backup/​local/ nas:/​backup/

# 云备份(副本 3,异地)
aws s3 sync /​backup/​local/ s3://backup-bucket/

备份类型

完整备份

# 每周完整备份
tar -czvf /​backup/​full_$(date +%Y%m%d).tar.gz /​data

增量备份

# 基于时间戳
tar -czvf /​backup/​incr_$(date +%Y%m%d).tar.gz \
    --newer-mtime="1 day ago" /​data

# 基于快照文件
tar -czvf /​backup/​incr.tar.gz -g /​backup/​snapshot.snar /​data

差异备份

# 基于完整备份时间
tar -czvf /​backup/​diff_$(date +%Y%m%d).tar.gz \
    --newer-mtime="$(cat /​backup/​last_full_date)" /​data

保留策略

GFS 策略

# Grandfather-Father-Son
# 日备份:保留 7 天
# 周备份:保留 4 周
# 月备份:保留 12 个月
# 年备份:保留 7 年

#!/​bin/​bash
BACKUP_DIR="/​backup"
DATE=$(date +%Y%m%d)
DOW=$(date +%u)  # 1-7
DOM=$(date +%d)  # 01-31

# 日备份
tar -czvf ${BACKUP_DIR}/​daily/​backup_${DATE}.tar.gz /​data

# 周备份(周日)
if [ "$DOW" -eq 7 ]; then
    cp ${BACKUP_DIR}/​daily/​backup_${DATE}.tar.gz ${BACKUP_DIR}/​weekly/
fi

# 月备份(1号)
if [ "$DOM" -eq "01" ]; then
    cp ${BACKUP_DIR}/​daily/​backup_${DATE}.tar.gz ${BACKUP_DIR}/​monthly/
fi

# 清理
find ${BACKUP_DIR}/​daily -mtime +7 -delete
find ${BACKUP_DIR}/​weekly -mtime +28 -delete
find ${BACKUP_DIR}/​monthly -mtime +365 -delete

滚动保留

#!/​bin/​bash
# 保留最近 N 个备份
BACKUP_DIR="/​backup"
KEEP=10

ls -1t ${BACKUP_DIR}/​*.tar.gz | tail -n +$((KEEP+1)) | xargs -r rm

备份验证

完整性检查

# 校验和验证
md5sum backup.tar.gz > backup.md5
md5sum -c backup.md5

# tar 测试
tar -tzvf backup.tar.gz > /​dev/​null

# gzip 测试
gzip -t backup.tar.gz

恢复测试

#!/​bin/​bash
# 定期恢复测试
TEST_DIR="/​tmp/​restore_test"
mkdir -p $TEST_DIR

# 解压测试
tar -xzvf /​backup/​latest.tar.gz -C $TEST_DIR

# 验证文件数量
ORIG_COUNT=$(find /​data -type f | wc -l)
REST_COUNT=$(find $TEST_DIR -type f | wc -l)

if [ "$ORIG_COUNT" -eq "$REST_COUNT" ]; then
    echo "验证通过"
else
    echo "验证失败:文件数量不匹配"
fi

rm -rf $TEST_DIR

常见场景

场景 1:企业备份方案

#!/​bin/​bash
# 综合备份脚本
CONFIG="/​etc/​backup/​config"
LOG="/​var/​log/​backup.log"

log() { echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> $LOG; }

# 数据库备份
log "开始数据库备份"
mysqldump --all-databases | gzip > /​backup/​db_$(date +%Y%m%d).sql.gz

# 文件备份
log "开始文件备份"
tar -czvf /​backup/​files_$(date +%Y%m%d).tar.gz /​data

# 同步到 NAS
log "同步到 NAS"
rsync -avz /​backup/ nas:/​backup/

# 上传到云
log "上传到云存储"
aws s3 sync /​backup/ s3://backup-bucket/

# 验证
log "验证备份"
gzip -t /​backup/​*.gz

# 清理
log "清理旧备份"
find /​backup -mtime +7 -delete

log "备份完成"

场景 2:备份监控

#!/​bin/​bash
# 检查备份状态
BACKUP_DIR="/​backup"
MAX_AGE=86400  # 24小时

LATEST=$(ls -1t ${BACKUP_DIR}/​*.tar.gz 2>/​dev/​null | head -1)

if [ -z "$LATEST" ]; then
    echo "CRITICAL: 无备份文件"
    exit 2
fi

AGE=$(($(date +%s) - $(stat -c %Y "$LATEST")))

if [ $AGE -gt $MAX_AGE ]; then
    echo "WARNING: 备份超过 24 小时"
    exit 1
fi

echo "OK: 最新备份 $(basename $LATEST)"
exit 0

策略对比

策略存储空间恢复速度复杂度
完整高快低
增量低慢高
差异中中中
GFS中中中

最佳实践

1. 自动化备份,避免人工遗漏
2. 定期验证备份可恢复性
3. 加密敏感数据备份
4. 监控备份状态和空间
5. 文档化恢复流程
6. 定期演练恢复过程

More DevOps & CI/CD skills

Project scaffolding, deployment configuration, and CI/CD setup for Google ADK agents.

6K 472.6K
View

Set up tracing, logging, and monitoring for deployed ADK agents across Cloud Trace, BigQuery, and third-party platforms.

6K 472.6K
View

Enterprise Azure infrastructure architect generating Bicep or Terraform from workload descriptions.

1.5K 454.3K
View
azure-kubernetes logo
DevOps & CI/CD

azure-kubernetes

Plan and configure production-ready Azure Kubernetes Service clusters with Day-0 and Day-1 best practices.

1.5K 447.1K
View

Raw mechanical interfaces fusing Swiss typographic print with military terminal aesthetics. Rigid grids, extreme type scale contrast, utilitarian color, analog degradation effects. For data-heavy dashboards, portfolios, or editorial sites that need to feel like declassified blueprints.

92.7K 354.9K
View
just-scrape logo
DevOps & CI/CD

just-scrape

Web search, scraping, extraction, crawling, and monitoring via ScrapeGraph AI CLI.

69 245K
View

Skill for working with Firebase Hosting (Classic). Use this when you want to deploy static web apps, Single Page Apps (SPAs), or simple microservices. Do NOT use for Firebase App Hosting.

462 159.7K
View

Deploy and manage web apps with Firebase App Hosting. Use this skill when deploying Next.js/Angular apps with backends.

462 159.1K
View
deploy-to-vercel logo
DevOps & CI/CD

deploy-to-vercel

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

31.9K 146.6K
View
programmatic-seo logo
DevOps & CI/CD

programmatic-seo

Build SEO-optimized pages at scale using templates, data, and proven playbook patterns.

53.3K 140.1K
View

Design and build isolated, reusable Convex backend components with clear boundaries and app-facing wrappers.

63 123.5K
View

Deploy and manage projects on Vercel using token-based authentication. Use when working with Vercel CLI using access tokens rather than interactive login — e.g. "deploy to vercel", "set up vercel", "add environment variables to vercel".

31.9K 116.1K
View

Coding & apps AI tools

Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View
I
Coding & apps

Imagica

Build a no-code AI app in minutes.

Freemium
View
E
Coding & apps

Emergent.sh

An IDE for code migration from legacy to modern frameworks through coding agents.

Freemium
View
Wonder Dynamics logo
Coding & apps

Wonder Dynamics

Automate CGI animation in live-action scenes

Paid
View
M
Coding & apps

Mixo

Launch a website in seconds with AI.

Paid
View
AI Code Convert logo
Coding & apps

AI Code Convert

Streamline Your Coding Experience with AI Code Helper

Free
View