database-backup
Installation
Review the source and dependencies with a prompt. A package is available to download.
Install with an AI assistant
Copy the prompt so your assistant can review the pinned revision and dependencies first.
View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "database-backup" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-8ec4b7d0b366de819e0758441d7d3ca454ef721c55abb63bee987c12abc2e1b5. Verify the upstream source and version against https://skillhub.cn/skills/user_3c6cb52e/database-backup-skillhub before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.
Source and setup
skillhub.cn
Setup guidance pending
See the original source for supported tools, dependencies, and permissions.
Installation test pending
A skill on the SkillHub platform (SkillHub skill).
Introduction source: third_party · user_3c6cb52e · 1.0.0 Reference
SkillPi interpretation
- Tasks
- Translation pending; see source documentation.
- Testing
- Not tested
Browse files
Frontmatter
name: database-backup
description: 使用全量、增量和时间点恢复策略创建、计划并验证数据库备份。Database Backup
此技能使AI代理能够在PostgreSQL、MySQL和MongoDB上规划和执行数据库备份策略。该代理选择适当的备份类型(全量、增量、差异或时间点),生成带压缩和加密的备份脚本,通过cron或类似工具配置自动化调度,定义保留策略,并通过恢复测试验证备份完整性。
Workflow
-
评估备份需求: 确定数据库类型、大小、可接受的数据丢失窗口(RPO)以及可接受的停机时间(RTO)。识别备份是否必须一致(应用级锁或快照),以及数据库是否可以容忍备份期间短暂锁定。
-
选择备份策略: 根据需求选择合适的备份类型。全量备份捕获整个数据库,最简单恢复但创建速度最慢。增量备份仅捕获自上次备份以来的更改,节省时间和存储空间。差异备份捕获自上次全量备份以来的更改,提供中间平衡。时间点恢复(PITR)使用预写日志或oplog恢复到任意时刻,提供最低的RPO。
-
生成备份脚本: 生成调用目标数据库正确备份工具的shell脚本。包括压缩(gzip、lz4或zstd)、可选加密(GPG或OpenSSL)、时间戳文件名和带退出码的错误处理。脚本应记录输出以供监控。
-
配置调度与保留策略: 使用cron、systemd定时器或任务调度器设置自动化执行。定义保留策略(例如,保留7天每日备份、4周每周备份、12月每月备份),并实现过期备份清理以管理存储使用情况。
-
验证备份完整性: 每次备份后,验证文件非空且校验和有效。定期在预发环境中进行测试恢复,确认备份确实可恢复。在验证失败时发出警报。
-
记录与监控: 记录备份计划、保留策略、存储位置和恢复流程。使用Prometheus、Datadog或简单邮件通知等工具设置错过备份或备份失败的监控告警。
支持的技术
- PostgreSQL: pg_dump, pg_basebackup, WAL归档用于PITR
- MySQL: mysqldump, mysqlpump, Percona XtraBackup, 二进制日志用于PITR
- MongoDB: mongodump, mongorestore, 文件系统快照, oplog用于PITR
- 压缩: gzip, lz4, zstd, pigz
- 加密: GPG, OpenSSL AES-256
- 存储: 本地磁盘、AWS S3、Google Cloud Storage、Azure Blob Storage
Usage
向代理提供您的数据库类型、名称和连接详细信息。指定您的RPO/RTO要求、首选存储目的地以及是否需要加密。代理将生成完整的备份解决方案,包括脚本、调度、保留策略和验证步骤。
Examples
示例 1:使用Cron计划的PostgreSQL全量备份
请求: 设置一个夜间PostgreSQL数据库的全量备份,保留30天,压缩并上传到本地备份目录。
#!/usr/bin/env bash
# pg_backup.sh — Nightly PostgreSQL full backup
set -euo pipefail
DB_NAME="production"
DB_USER="backup_user"
BACKUP_DIR="/var/backups/postgresql"
RETENTION_DAYS=30
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${BACKUP_DIR}/${DB_NAME}_${TIMESTAMP}.sql.gz"
mkdir -p "$BACKUP_DIR"
echo "[$(date)] Starting backup of database '${DB_NAME}'..."
pg_dump -U "$DB_USER" -h localhost -Fc "$DB_NAME" | gzip > "$BACKUP_FILE"
# Verify the backup file is non-empty
if [ ! -s "$BACKUP_FILE" ]; then
echo "[$(date)] ERROR: Backup file is empty. Aborting." >&2
exit 1
fi
FILE_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
echo "[$(date)] Backup complete: ${BACKUP_FILE} (${FILE_SIZE})"
# Clean up backups older than retention period
find "$BACKUP_DIR" -name "${DB_NAME}_*.sql.gz" -mtime +${RETENTION_DAYS} -delete
echo "[$(date)] Expired backups removed (older than ${RETENTION_DAYS} days)."
Cron条目(每天凌晨2:00运行):
0 2 * * * /usr/local/bin/pg_backup.sh >> /var/log/pg_backup.log 2>&1
示例 2:MongoDB备份与恢复流程
请求: 备份MongoDB副本集并记录恢复流程。
备份脚本:
#!/usr/bin/env bash
# mongo_backup.sh — MongoDB replica set backup
set -euo pipefail
MONGO_URI="mongodb://backup_user:secret@rs1.example.com:27017,rs2.example.com:27017/admin?replicaSet=rs0"
BACKUP_DIR="/var/backups/mongodb"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
DUMP_DIR="${BACKUP_DIR}/dump_${TIMESTAMP}"
mkdir -p "$BACKUP_DIR"
echo "[$(date)] Starting mongodump..."
mongodump --uri="$MONGO_URI" --oplog --out="$DUMP_DIR"
# Compress the dump directory
tar -czf "${DUMP_DIR}.tar.gz" -C "$BACKUP_DIR" "dump_${TIMESTAMP}"
rm -rf "$DUMP_DIR"
echo "[$(date)] Backup complete: ${DUMP_DIR}.tar.gz"
# Retain only the last 14 backups
ls -t ${BACKUP_DIR}/dump_*.tar.gz | tail -n +15 | xargs -r rm --
恢复流程:
# 1. Extract the backup archive
tar -xzf /var/backups/mongodb/dump_20250115_020000.tar.gz -C /tmp/
# 2. Restore to the target MongoDB instance
mongorestore --uri="mongodb://admin:secret@localhost:27017" \
--oplogReplay --drop /tmp/dump_20250115_020000/
# 3. Verify collections and document counts
mongosh --eval "db.adminCommand({listDatabases: 1})"
最佳实践
- 定期测试恢复 — 从未恢复过的备份是不可信的。安排每月一次的预发环境恢复演练。
- 使用GPG或AES-256加密静态和传输中的备份,尤其是在异地存储或云存储时。切勿将加密密钥与备份一同存放。
- 使用--oplog标志(MongoDB)或WAL归档(PostgreSQL) 启用时间点恢复,显著减少潜在数据丢失。
- 将备份存储在独立的故障域中 — 不同服务器、可用区或云区域,以防止单个基础设施故障同时破坏数据库和其备份。
- 使用告警监控备份作业 — 沉默不是成功。对缺失备份、零字节文件或校验和不匹配发出告警。
- 记录完整的恢复流程 包括确切命令、预期时间及责任人,以便在压力下恢复时无需猜测。
Edge Cases
- 大数据库超出磁盘空间: 对于大于本地存储容量的数据库,直接将备份流式传输到对象存储(例如
pg_dump | gzip | aws s3 cp - s3://bucket/backup.gz)以避免本地磁盘耗尽。 - 备份期间活跃写入流量: 使用带有--snapshot的pg_dump或MongoDB的--oplog获取一致的时间点备份,即使在持续写入时也可使用。对于MySQL,使用Percona XtraBackup进行InnoDB热备份而无需锁定。
- 加密数据库的备份: 如果启用了透明数据加密(TDE),确保加密密钥单独备份,并且备份过程能捕获可恢复格式的加密数据。
- 跨区域复制延迟: 从副本备份时,在开始备份前验证复制延迟为零或接近零,以避免捕获陈旧数据。
- 备份凭证轮换: 将凭证存储在密钥管理器(Vault、AWS Secrets Manager)中而不是硬编码在脚本中,并确保备份脚本能处理凭证轮换而无需手动更新。
Current listing version
1.0.0
Safety and assessment details
Quality assessment
Out of 100 · Assessment report from SkillPi LAB.
Highlights
- 覆盖 PostgreSQL、MySQL、MongoDB 三大主流数据库的完整备份解决方案
- 支持全量、增量、差异及时间点恢复(PITR)多种策略,满足不同 RPO/RTO 需求
- 提供端到端自动化流程,包括脚本生成、cron 调度、保留策略与恢复验证
Highlights
- 触发描述清晰明确,涵盖备份类型、调度、验证等核心功能维度
- 边界清晰,无与其他运维类 Skill 的重叠风险
Highlights
- Workflow 分 6 阶段逻辑清晰,具备完整出口条件与错误处理
- 提供两个高质量端到端示例(PostgreSQL 与 MongoDB),含完整脚本与恢复流程
- 包含最佳实践与 Edge Cases 处理,增强实用性
Highlights
- 使用标准数据库备份工具(pg_dump、mongodump 等),来源可靠
- 脚本具备压缩、加密、校验、清理等完整功能,错误处理健全
- 覆盖大数据库、活跃写入、加密环境等关键异常场景
The arithmetic mean of the first four original scores, rounded to a whole number.
Static text checks
100 / 100 · skillpi-20261123-no-r3-v1
No current rules matched the text.
This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.
Permissions and checks before use
Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.
-
Delete files
Confirm the absolute path and scope and keep a recoverable backup. Do not run example commands against important directories.
Mentioned in main document · lines 102 -
Accounts, secrets or authorization
Use a dedicated least-privilege account. Keep secrets out of public content and command logs; verify the receiving service and tenant.
Mentioned in main document · lines 127, 137, 139
Security rating and access scope
safe · Static evidence review; not a runtime safety guarantee.
Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.
User discussion
No verified reviews yet.