Skip to main content

A Django app to backup PostgreSQL databases to S3

Project description

Django PostgreSQL Backup

A reusable Django app to automatically backup PostgreSQL databases to S3.

Features

  • Scheduled or manual backups of PostgreSQL databases
  • S3 storage of backups
  • Automatic cleanup of old backups
  • Configurable scheduling

Installation

pip install django-postgres-backup

Or add to your requirements.txt:

django-postgres-backup==0.1.0

Configuration

Add to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # other apps
    'django_backup',
]

Configure your settings.py with the following settings:

# Database settings
DB_HOST = 'localhost'
DB_PORT = '5432'
DB_NAME = 'your_database'
DB_USER = 'postgres'
DB_PASSWORD = 'your_password'

# S3 settings
AWS_STORAGE_BUCKET_NAME = 'your-bucket-name'
AWS_ACCESS_KEY_ID = 'your-access-key'
AWS_SECRET_ACCESS_KEY = 'your-secret-key'

# Backup settings
BACKUP_DIR = 'database_backups'
BACKUP_SCHEDULER_ENABLED = True  # Set to False to disable automatic backups
BACKUP_UPLOAD_TO_S3 = True       # Set to False to disable S3 upload
BACKUP_CLEANUP_ENABLED = True    # Set to False to disable automatic cleanup
BACKUP_KEEP_COUNT = 2            # Number of backups to keep

# Optional: Server ownership settings
IS_LOCAL = False                # Set to True for local development
SERVER_USER = 'www-data'        # User who should own backup files
SERVER_GROUP = 'www-data'       # Group who should own backup files

# Backup schedule (uses APScheduler cron syntax)
BACKUP_SCHEDULE = {
    'hour': '0',     # Run at midnight
    'minute': '0',
}

Usage

Automatic Backups

If BACKUP_SCHEDULER_ENABLED is set to True, backups will run automatically according to your schedule.

Manual Backups

Run a backup manually:

python manage.py run_backup

Options:

  • --upload: Force upload to S3 (even if disabled in settings)
  • --cleanup: Force cleanup of old backups
  • --keep-count=N: Specify number of backups to keep when cleaning up

How It Works

  1. The app creates backups using PostgreSQL's pg_dump tool
  2. If pg_dump is not available, it falls back to using a Docker container
  3. Backups are stored locally in the directory specified by BACKUP_DIR
  4. If enabled, backups are uploaded to the S3 bucket
  5. Old backups are cleaned up, keeping only the most recent ones

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_postgres_backup_ganaf-0.1.2.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_postgres_backup_ganaf-0.1.2-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file django_postgres_backup_ganaf-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django_postgres_backup_ganaf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 01466e13c4b40630dd0c77eac05ae4bf77ad71fb618281f606f7c0e7f9313064
MD5 c49ecd60198b19791f4a1b381363da7b
BLAKE2b-256 ca1a3abc258aae0ec695d8569d7d9eb8d8e012e79c59c35693057d34733d0058

See more details on using hashes here.

File details

Details for the file django_postgres_backup_ganaf-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_postgres_backup_ganaf-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6e896485235a82ed8bb04d96fadbcca6937c4b7f949a19c64b35c209c1cac9da
MD5 7d76fbe576989c98a90eb1421a5fc993
BLAKE2b-256 d0192166c248f070d7d8832ca3c5cfb3acc9a9e1c3e5e9e9eaaf5fa2a158c8f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page