Skip to main content

Database Backup

Production-Ready

Automated database backup solution with multi-database support, S3-compatible storage, and seamless Django-RQ integration for scheduled backups.

Quick Navigation

For Developers

For DevOps


Why Database Backup?

Data is your most valuable asset. Django-CFG provides a zero-configuration backup solution that:

  • Auto-detects database type - PostgreSQL, MySQL, SQLite
  • Integrates with Django-RQ - Automatic scheduled backups
  • Supports cloud storage - AWS S3, Cloudflare R2, MinIO
  • Provides retention policies - Automatic cleanup of old backups
  • Includes admin interface - Visual backup management

Architecture


Key Features

FeatureDescription
Multi-DatabasePostgreSQL (pg_dump), MySQL (mysqldump), SQLite (VACUUM INTO)
Auto-DetectionAutomatically detects database engine from Django settings
Cloud StorageS3-compatible: AWS S3, Cloudflare R2, MinIO, DigitalOcean Spaces
Compressiongzip, bz2, xz, or none
EncryptionOptional GPG-compatible encryption
SchedulingCron expressions via Django-RQ (automatic if RQ enabled)
RetentionDaily/weekly/monthly rotation with size limits
NotificationsTelegram/email on success/failure
Admin UIFull Django admin for backup management
REST APIProgrammatic backup/restore operations

Quick Start

1. Enable Backup Module

config.py
from django_cfg import (
DjangoConfig,
BackupConfig,
BackupStorageConfig,
BackupScheduleConfig,
)

class MyConfig(DjangoConfig):
# Enable backup with minimal configuration
backup: BackupConfig = BackupConfig(
enabled=True,
storage=BackupStorageConfig(backend="local"),
schedule=BackupScheduleConfig(cron="0 2 * * *"), # Daily at 2 AM
)

2. Run Migrations

python manage.py migrate db_backup

3. Create Your First Backup

# Manual backup
python manage.py db_backup

# List backups
python manage.py db_backup --list

That's it! If Django-RQ is enabled, backups will run automatically on schedule.


How It Works

Automatic Scheduling

When both BackupConfig and DjangoRQConfig are enabled:

Without Django-RQ

If RQ is not enabled, use management commands or external cron:

# Crontab entry
0 2 * * * cd /app && python manage.py db_backup --all

Management Commands

Create Backup

# Backup default database
python manage.py db_backup

# Backup specific database
python manage.py db_backup --database=analytics

# Backup all databases
python manage.py db_backup --all

# With verbose output
python manage.py db_backup -v 2

List Backups

# List recent backups
python manage.py db_backup --list

# Show backup details
python manage.py db_backup --info <backup_id>

Restore Backup

# Restore to original database
python manage.py db_restore <backup_id>

# Restore to different database
python manage.py db_restore <backup_id> --target=test_db

# Force restore without confirmation
python manage.py db_restore <backup_id> --force

Cleanup Old Backups

python manage.py db_backup --cleanup

Recovery Procedures

Standard Recovery

  1. List available backups

    python manage.py db_backup --list
  2. Verify backup integrity

    python manage.py db_backup --info <backup_id>
  3. Restore database

    python manage.py db_restore <backup_id>

Point-in-Time Recovery

For PostgreSQL with WAL archiving, combine with pg_basebackup for PITR capability.

Disaster Recovery Checklist

  • Verify backup exists in storage
  • Check backup file integrity (checksum)
  • Stop application services
  • Restore database
  • Run migrations if needed
  • Verify data integrity
  • Restart application services

REST API

Endpoints

MethodEndpointDescription
GET/cfg/db/backups/List all backups
GET/cfg/db/backups/{id}/Get backup details
POST/cfg/db/backups/create/Create new backup
POST/cfg/db/backups/{id}/restore/Restore from backup
POST/cfg/db/backups/cleanup/Run cleanup
GET/cfg/db/databases/List databases

Example: Create Backup via API

curl -X POST /cfg/db/backups/create/ \
-H "Authorization: Token <token>" \
-H "Content-Type: application/json" \
-d '{"database_alias": "default"}'

Database Support

PostgreSQL

Uses pg_dump with custom format for efficient backup and pg_restore for recovery:

# Backup (automatic)
pg_dump --format=custom --no-password -h host -p port -U user dbname

# Restore (automatic)
pg_restore --no-password --clean --if-exists -h host -p port -U user -d dbname

Requirements: pg_dump and pg_restore must be installed (included with PostgreSQL).

MySQL / MariaDB

Uses mysqldump for backup and mysql for restore:

# Backup (automatic)
mysqldump --single-transaction --routines --triggers -h host -P port -u user -p dbname

# Restore (automatic)
mysql -h host -P port -u user -p dbname < backup.sql

Requirements: mysqldump and mysql CLI tools must be installed.

SQLite

Uses VACUUM INTO for consistent backups (SQLite 3.27+):

VACUUM INTO '/path/to/backup.db'

Falls back to file copy with locking for older SQLite versions.


Comparison with Alternatives

FeatureDjango-CFG Backupdjango-dbbackupManual Scripts
ConfigurationPydantic modelDjango settingsCustom
Auto-schedulingDjango-RQ integrationExternal cronExternal cron
S3 SupportBuilt-inPluginCustom
Admin InterfaceBuilt-inNoneNone
REST APIBuilt-inNoneCustom
Multi-databaseAutomaticManual configManual
Type SafetyFull PydanticNoneNone

See Also