关于 frappe-ops-backup
面向 Frappe 运维人员的备份与灾难恢复技能,汇总 bench 备份、还原、验证及自动化流程,并强调定期测试还原。
主要功能
- 01数据库及文件备份
- 02站点还原与迁移
- 03定时及 S3 备份
- 04加密与备份验证
- 05多站点备份策略
适用场景
- 01更新或迁移前创建备份
- 02将生产站点还原到测试环境
- 03验证备份文件可成功还原
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ impertio-studio/ frappe_claude_skill_package --skill frappe-ops-backup安装后在 Claude Code 中输入/frappe-ops-backup确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/.gitkeepanti-patterns.mdexamples.mdworkflows.md详细介绍
备份与灾难恢复
Frappe 通过 bench 内置备份和恢复命令。更新、迁移或执行任何破坏性操作前,务必先备份。未经测试恢复的备份不算备份。
快速参考
# Database-only backup (default)
bench backup
# Full backup with public + private files
bench backup --with-files
# Backup specific site
bench --site mysite.com backup --with-files
# Backup with compression (.tgz instead of .tar)
bench backup --with-files --compress
# Backup only specific DocTypes
bench backup --only "Sales Invoice,Purchase Invoice"
# Backup excluding specific DocTypes
bench backup --exclude "Error Log,Activity Log"
# Custom backup path
bench backup --backup-path /mnt/backups/
# Restore from backup
bench --site mysite.com restore /path/to/backup.sql.gz
# Restore with files
bench --site mysite.com restore /path/to/backup.sql.gz \
--with-public-files /path/to/files.tar \
--with-private-files /path/to/private-files.tar
# Setup automated backups (cron)
bench setup backups
备份内容
| 组件 | 默认 | 使用 --with-files 时 | 位置 |
|---|---|---|---|
| 数据库(SQL 转储) | 是 | 是 | sites/{site}/private/backups/ |
| 站点配置 | 是 | 是 | sites/{site}/private/backups/ |
| 公共文件 | 否 | 是 | sites/{site}/public/files/ |
| 私有文件 | 否 | 是 | sites/{site}/private/files/ |
备份文件命名格式:{datetime}_{hash}_{site}-database.sql.gz
备份决策树
What do you need to back up?
|
+-- Quick database snapshot before a change?
| +-- bench backup (database only, fast)
|
+-- Full backup before upgrade or migration?
| +-- bench backup --with-files --compress
|
+-- Automated daily backups?
| +-- bench setup backups (cron-based)
| +-- OR S3 Backup Settings (cloud storage)
|
+-- Offsite / cloud backup?
| +-- S3 Backup Settings DocType (built-in)
| +-- OR custom script with rclone/aws-cli
|
+-- Partial backup (specific DocTypes)?
| +-- bench backup --only "DocType1,DocType2"
|
+-- Disaster recovery?
| +-- Full backup + files + tested restore procedure
| +-- See Disaster Recovery section below
bench backup:所有选项
bench backup [OPTIONS]
# Path options
--backup-path PATH # Save all backup files to this directory
--backup-path-db PATH # Custom path for database dump
--backup-path-conf PATH # Custom path for site config backup
--backup-path-files PATH # Custom path for public files archive
--backup-path-private-files PATH # Custom path for private files archive
# Filter options
--only, --include, -i DOCTYPES # Include ONLY these DocTypes (comma-separated)
--exclude, -e DOCTYPES # Exclude these DocTypes (comma-separated)
--ignore-backup-conf # Ignore include/exclude from site config
# Flags
--with-files # Include public and private files
--compress # Use .tgz format (gzip compressed tar)
--verbose # Show detailed output
安全功能:如果备份失败(出现任何异常),系统会自动删除部分生成的文件,避免不完整的备份占用磁盘空间。
bench restore:所有选项
bench --site [site-name] restore [OPTIONS] SQL_FILE_PATH
# SQL_FILE_PATH: path to .sql or .sql.gz file
# Can be relative to sites/ directory or absolute path
# Options
--db-root-username USERNAME # MariaDB/PostgreSQL root username
--db-root-password PASSWORD # MariaDB/PostgreSQL root password
--db-name NAME # Use custom database name
--admin-password PASSWORD # Set administrator password after restore
--install-app APP_NAME # Install app after restore
--with-public-files PATH # Restore public files (.tar or .tgz)
--with-private-files PATH # Restore private files (.tar or .tgz)
# Flags
--force # Bypass downgrade warnings (NOT recommended)
重要:不支持降级。将较新版本的备份恢复到较旧版本时会触发警告。除非你了解后果,否则切勿使用 --force 绕过此限制。
自动备份
基于 Cron 的备份(bench setup backups)
# Sets up daily backup cron job
bench setup backups
# This adds to crontab:
# 0 */6 * * * cd /home/frappe/frappe-bench && bench backup --with-files
S3 备份设置(内置 DocType)
在 ERPNext 中配置:设置 > S3 备份设置
| 字段 | 描述 |
|---|---|
| 启用 | 切换是否启用自动 S3 备份 |
| S3 存储桶名称 | 目标存储桶 |
| AWS 访问密钥 ID | IAM 凭据 |
| AWS 秘密访问密钥 | IAM 密钥 |
| 区域 | AWS 区域(例如,eu-west-1) |
| 频率 | 每日、每周 |
| 备份文件 | 包含公共/私有文件 |
# Programmatic S3 backup trigger
from frappe.integrations.offsite_backup_utils import send_email
import frappe
# The S3 backup runs via scheduled job when enabled
# To trigger manually:
from frappe.integrations.doctype.s3_backup_settings.s3_backup_settings import take_backups_s3
take_backups_s3()
自定义备份脚本
#!/bin/bash
# custom-backup.sh — Daily backup with rotation and offsite copy
set -e
BENCH_DIR="/home/frappe/frappe-bench"
BACKUP_DIR="/mnt/backups/frappe"
RETENTION_DAYS=30
S3_BUCKET="s3://my-frappe-backups"
DATE=$(date +%Y-%m-%d_%H%M)
cd $BENCH_DIR
# Create backup
bench backup --with-files --compress --backup-path "$BACKUP_DIR/$DATE/"
# Sync to S3
aws s3 sync "$BACKUP_DIR/$DATE/" "$S3_BUCKET/$DATE/"
# Remove local backups older than retention period
find "$BACKUP_DIR" -type d -mtime +$RETENTION_DAYS -exec rm -rf {} +
# Verify latest backup exists on S3
aws s3 ls "$S3_BUCKET/$DATE/" || echo "WARNING: S3 sync failed!"
备份加密
静态加密备份
# Encrypt backup with GPG (symmetric)
bench backup --with-files --compress
gpg --symmetric --cipher-algo AES256 \
sites/mysite/private/backups/latest-database.sql.gz
# Decrypt for restore
gpg --decrypt backup.sql.gz.gpg > backup.sql.gz
bench --site mysite.com restore backup.sql.gz
使用 OpenSSL 加密
# Encrypt
openssl enc -aes-256-cbc -salt -pbkdf2 \
-in backup.sql.gz -out backup.sql.gz.enc
# Decrypt
openssl enc -d -aes-256-cbc -pbkdf2 \
-in backup.sql.gz.enc -out backup.sql.gz
务必将加密密钥/密码与备份分开存放。切勿将解密密钥存放在加密备份所在的位置。
恢复流程
完整站点恢复
# 1. Stop services (traditional deployment)
sudo supervisorctl stop all
# 2. Restore database
bench --site mysite.com restore \
/path/to/20240115_backup-database.sql.gz \
--db-root-password YOUR_DB_ROOT_PASSWORD \
--admin-password NEW_ADMIN_PASSWORD
# 3. Restore files
bench --site mysite.com restore \
/path/to/20240115_backup-database.sql.gz \
--with-public-files /path/to/20240115_backup-files.tar \
--with-private-files /path/to/20240115_backup-private-files.tar
# 4. Run migrations (if version differs)
bench --site mysite.com migrate
# 5. Clear cache
bench --site mysite.com clear-cache
# 6. Restart services
sudo supervisorctl start all
恢复到新站点
# Create new site from backup (useful for staging/testing)
bench new-site staging.example.com \
--db-root-password YOUR_DB_ROOT_PASSWORD \
--admin-password STAGING_PASSWORD
bench --site staging.example.com restore \
/path/to/production-backup.sql.gz \
--with-public-files /path/to/files.tar \
--with-private-files /path/to/private-files.tar
bench --site staging.example.com migrate
Docker 恢复
# Copy backup into container
docker cp backup.sql.gz frappe-backend:/tmp/
# Restore
docker compose exec backend \
bench --site mysite.com restore /tmp/backup.sql.gz \
--db-root-password $DB_ROOT_PASSWORD
# Cleanup
docker compose exec backend rm /tmp/backup.sql.gz
备份验证
务必定期测试恢复。只有能够成功恢复的备份才有效。
#!/bin/bash
# verify-backup.sh — Test restore to verify backup integrity
set -e
BACKUP_SQL="/mnt/backups/frappe/latest/database.sql.gz"
TEST_SITE="backup-test.localhost"
BENCH_DIR="/home/frappe/frappe-bench"
cd $BENCH_DIR
# Create temporary test site
bench new-site $TEST_SITE --db-root-password $DB_ROOT_PASSWORD --admin-password test
# Restore backup
bench --site $TEST_SITE restore $BACKUP_SQL --db-root-password $DB_ROOT_PASSWORD
# Run basic verification
bench --site $TEST_SITE migrate
bench --site $TEST_SITE console <<'EOF'
import frappe
count = frappe.db.count("User")
print(f"User count: {count}")
assert count > 0, "No users found — backup may be corrupt"
print("Backup verification PASSED")
EOF
# Cleanup test site
bench drop-site $TEST_SITE --db-root-password $DB_ROOT_PASSWORD --force
echo "Backup verification complete"
多站点备份策略
#!/bin/bash
# backup-all-sites.sh — Backup every site in the bench
set -e
BENCH_DIR="/home/frappe/frappe-bench"
cd $BENCH_DIR
for site in $(bench --site all list-apps 2>/dev/null | grep -oP '^\S+'); do
echo "Backing up $site..."
bench --site "$site" backup --with-files --compress
done
灾难恢复计划模板
1. PREVENTION
- Automated daily backups (bench setup backups OR S3)
- Offsite copies (S3, GCS, or remote server)
- Encrypted backups for sensitive data
- Backup retention: minimum 30 days
2. DETECTION
- Monitor backup cron job (check /var/log/syslog)
- Verify backup file sizes (alert if < expected)
- Weekly automated restore test
3. RECOVERY (RTO target: < 4 hours)
a. Provision new server (or use standby)
b. Install Frappe/ERPNext (same version as backup)
c. Restore from latest verified backup
d. Run migrations
e. Update DNS to point to new server
f. Verify functionality
4. DOCUMENTATION
- Backup locations and credentials
- Encryption key storage (separate from backups)
- Step-by-step restore procedure
- Contact list for escalation
版本差异
| 功能 | v14 | v15 | v16 |
|---|---|---|---|
--compress 标志 | 是 | 是 | 是 |
--only / --exclude | 是 | 是 | 是 |
| S3 备份设置 | 是 | 是 | 是 |
| 站点级日志 | v13+ | 是 | 是 |
partial-restore 命令 | 否 | 是 | 是 |
参考文件
| 文件 | 内容 |
|---|---|
| examples.md | 完整的备份/恢复脚本 |
| anti-patterns.md | 常见备份错误 |
| workflows.md | 分步备份工作流 |
相关技能
frappe-ops-deployment— 生产环境部署(更新工作流中包含备份)frappe-ops-performance— 性能调优frappe-ops-bench— Bench CLI 参考frappe-ops-upgrades— 版本升级流程(必须备份)
评论
支持 Markdown