Create and manage database backups across environments
This skill implements backup creation, verification, metadata tracking, and retention management. </CONTEXT>
<CRITICAL_RULES>
{
"operation": "create-backup",
"parameters": {
"environment": "production",
"label": "pre-v2-migration",
"reason": "before major schema changes",
"compression": true,
"working_directory": "/mnt/c/GitHub/myorg/myproject"
}
}
Follow the workflow file at workflow/create-backup.md for detailed step-by-step instructions for backup creation.
High-level process:
This skill routes to backup tool handlers based on configuration:
handler-db-prisma): pg_dump/mysqldump backupshandler-cloud-aws): RDS snapshots (future)handler-cloud-gcp): Cloud SQL backups (future)Handler is determined from configuration:
.fractary/plugins/faber-db/config.json.database.hosting and .backup.methodHandler Operations:
create-backup - Create database backupverify-backup - Verify backup integritylist-backups - List available backupsdelete-backup - Delete backup file<COMPLETION_CRITERIA> You are complete when:
If any step fails:
Output structured messages:
Start:
🎯 STARTING: Backup Manager
Environment: production
Operation: create-backup
Label: pre-v2-migration
───────────────────────────────────────
During execution, log key steps:
End (success):
✅ COMPLETED: Backup Manager
Environment: production
Backup ID: backup-20250124-140000-pre-v2-migration
File: .fractary/plugins/faber-db/backups/production/backup-20250124-140000-pre-v2-migration.sql.gz
Size: 12.3 GB
Retention: 90 days (expires: 2025-04-24)
───────────────────────────────────────
To restore from this backup:
/faber-db:restore production --from backup-20250124-140000-pre-v2-migration
Return JSON:
Success:
{
"status": "success",
"operation": "create-backup",
"environment": "production",
"result": {
"backup_id": "backup-20250124-140000-pre-v2-migration",
"label": "pre-v2-migration",
"file_path": ".fractary/plugins/faber-db/backups/production/backup-20250124-140000-pre-v2-migration.sql.gz",
"size_bytes": 13212876800,
"size_human": "12.3 GB",
"compressed": true,
"format": "sql",
"migrations": {
"applied_count": 24,
"last_migration": "20250124130000_add_api_keys"
},
"retention_days": 90,
"expires_at": "2025-04-24T14:00:00Z",
"verification": {
"integrity_check": "passed",
"file_readable": true
}
},
"message": "Backup created successfully for production"
}
Error:
{
"status": "error",
"operation": "create-backup",
"environment": "production",
"error": "Insufficient disk space",
"result": {
"required_space_gb": 25,
"available_space_gb": 15,
"backup_size_estimate_gb": 48
},
"recovery": {
"suggestions": [
"Free up disk space: df -h",
"Clean up old backups: /faber-db:cleanup-backups --expired",
"Use compression: --compression flag (default for production)",
"Use cloud storage: Configure S3 in config.json"
]
}
}
<ERROR_HANDLING>
Common errors and handling:
Insufficient Disk Space:
{
"status": "error",
"error": "Insufficient disk space for backup",
"result": {
"required_space_gb": 50,
"available_space_gb": 25
},
"recovery": {
"suggestions": [
"Free up disk space",
"Use compression: --compression flag",
"Clean up old backups: /faber-db:cleanup-backups",
"Configure cloud storage in config.json"
]
}
}
Backup Tool Not Found:
{
"status": "error",
"error": "pg_dump not found - PostgreSQL client required",
"recovery": {
"suggestions": [
"Install PostgreSQL client: sudo apt-get install postgresql-client",
"macOS: brew install postgresql",
"Windows: Download from postgresql.org",
"After installation, retry: /faber-db:backup dev"
]
}
}
Database Connection Failed:
{
"status": "error",
"error": "Cannot connect to database for backup",
"prisma_output": "Connection refused at localhost:5432",
"recovery": {
"suggestions": [
"Verify database is running",
"Check connection string: echo $PROD_DATABASE_URL",
"Test connection: psql $PROD_DATABASE_URL",
"Verify network/VPN access"
]
}
}
Backup Verification Failed:
{
"status": "error",
"error": "Backup verification failed - file may be corrupted",
"result": {
"backup_file": "backup-20250124-140000.sql",
"expected_size_min_mb": 100,
"actual_size_mb": 12,
"file_readable": false
},
"recovery": {
"suggestions": [
"Delete corrupted backup file",
"Retry backup creation",
"Check disk space during backup",
"Verify database connection stability"
]
}
}
Backup Directory Not Writable:
{
"status": "error",
"error": "Cannot write to backup directory",
"result": {
"backup_directory": ".fractary/plugins/faber-db/backups/production/",
"permissions": "dr-xr-xr-x",
"required": "drwxrwxr-x"
},
"recovery": {
"suggestions": [
"Fix directory permissions: chmod 775 .fractary/plugins/faber-db/backups",
"Or create directory: mkdir -p .fractary/plugins/faber-db/backups/production",
"Verify user has write access"
]
}
}
</ERROR_HANDLING>
Called by migration-deployer before protected deployments:
{
"skill": "backup-manager",
"operation": "create-backup",
"parameters": {
"environment": "production",
"reason": "pre-migration",
"label": "before-migration-deployment"
}
}
Returns backup_id for potential rollback:
{
"status": "success",
"result": {
"backup_id": "backup-20250124-140000-pre-migration"
}
}
Provides backup listing for rollback target selection:
{
"skill": "backup-manager",
"operation": "list-backups",
"parameters": {
"environment": "production",
"sort": "desc",
"limit": 5
}
}
Returns available backups:
{
"status": "success",
"result": {
"backups": [
{
"backup_id": "backup-20250124-140000",
"created_at": "2025-01-24T14:00:00Z",
"migrations_count": 24
}
]
}
}
For actual backup creation:
Determine backup method from configuration:
Invoke handler based on hosting provider:
{
"skill": "handler-db-prisma",
"operation": "create-backup",
"parameters": {
"environment": "production",
"database_url_env": "PROD_DATABASE_URL",
"output_file": ".fractary/plugins/faber-db/backups/production/backup-20250124-140000.sql",
"compression": true,
"format": "sql"
}
}
Handler returns:
The backups.json file tracks all backups:
{
"schema_version": "1.0",
"backups": [
{
"backup_id": "backup-20250124-140000-pre-v2-migration",
"environment": "production",
"database_name": "myapp_prod",
"created_at": "2025-01-24T14:00:00Z",
"label": "pre-v2-migration",
"reason": "before major schema changes",
"file_path": ".fractary/plugins/faber-db/backups/production/backup-20250124-140000-pre-v2-migration.sql.gz",
"size_bytes": 13212876800,
"compressed": true,
"compression_ratio": 0.256,
"format": "sql",
"backup_method": "pg_dump",
"migrations": {
"applied_count": 24,
"last_migration": "20250124130000_add_api_keys",
"migrations_list": [
"20250120100000_initial_schema",
"...",
"20250124130000_add_api_keys"
]
},
"retention_days": 90,
"expires_at": "2025-04-24T14:00:00Z",
"status": "valid",
"verification": {
"integrity_check": "passed",
"file_readable": true,
"file_exists": true,
"verified_at": "2025-01-24T14:00:05Z"
},
"created_by": "backup-manager",
"tags": ["production", "pre-migration", "v2-launch"]
}
]
}
Backup IDs follow a consistent format:
backup-YYYYMMDD-HHMMSS[-label]
Examples:
backup-20250124-140000 - Auto-generatedbackup-20250124-140000-pre-migration - With pre-migration labelbackup-20250124-140000-pre-v2-migration - With custom labelThe timestamp ensures uniqueness and chronological sorting.
Before creating backup, estimate required space:
SELECT pg_database_size('myapp_prod')df -hExample:
Database size: 45 GB
Buffer (20%): 9 GB
Total estimate: 54 GB
With compression (70% reduction): 16 GB
Available space: 50 GB
Result: ✓ Sufficient space
Backups are automatically managed based on retention:
Cleanup process:
Manual override:
/faber-db:backup production --retention 180 # 180 days
Automatic safety backups before:
Safety backups have extended retention (90 days minimum).