Skip to content

kisugez/Database-Backup-Utility-Tool

Repository files navigation

Database-backup-utility

A CLI tool for automated database backup and restore across MySQL, PostgreSQL, and MongoDB — with local and cloud storage backends.

Python MySQL PostgreSQL MongoDB AWS S3 License

diagram-export-6-10-2026-11_35_19-PM

What it does

dbbackup wraps the native dump tools (mysqldump, pg_dump, mongodump) with a consistent CLI interface, adds gzip compression, handles upload/download to four storage backends, and provides a restore path that mirrors the backup path exactly. The goal is a single command to back up any database to anywhere, and a single command to bring it back.


Technical decisions worth knowing

Strategy pattern for database connectorsDatabaseConnector is an abstract base class. Each database type (MySQLConnector, PostgreSQLConnector, MongoDBConnector) implements the same interface: backup_full, backup_table, restore_full, restore_table, get_database_size, get_tables. The BackupManager and RestoreManager never know which database they're talking to.

Strategy pattern for storage backends — same design for storage. StorageHandler ABC, four concrete implementations (local, S3, GCS, Azure). The managers call upload and download — storage routing is handled entirely by the factory.

Temp directory isolation — all intermediate files (pre-compression backup, decompressed restore) go into a tempfile.mkdtemp() directory that is cleaned up in a finally block and __del__. A crash mid-backup doesn't leave uncompressed database dumps sitting on disk.

Password handling — PostgreSQL credentials are passed via the PGPASSWORD environment variable rather than as CLI flags, so they don't appear in process lists. MySQL uses --password= (mysqldump's own convention). Neither ends up in logs.

Config layering — configuration can come from a JSON file, a YAML file, environment variables, or direct CLI flags. The Config.from_file(), Config.from_env(), and Config.from_dict() classmethods all produce the same Config dataclass, which gets validated once before anything runs.


Install

git clone https://github.com/kisugez/Database-Backup-Utility-Tool.git
cd Database-Backup-Utility-Tool
pip install -r requirements.txt
pip install -e .

Prerequisites — the relevant native CLI tools must be in your PATH:

Database Required tools
MySQL mysql, mysqldump
PostgreSQL psql, pg_dump, pg_restore
MongoDB mongodump, mongorestore

Usage

Test your connection first

dbbackup test-connection \
  --db-type postgresql \
  --host localhost --port 5432 \
  --username postgres --password yourpass \
  --database mydb

Backup to local storage

dbbackup backup \
  --db-type mysql \
  --host localhost --username root --password yourpass \
  --database mydb \
  --storage-path ./backups

Backup to S3

dbbackup backup \
  --db-type postgresql \
  --host localhost --username postgres --password yourpass \
  --database mydb \
  --storage-type s3 \
  --storage-path my-backup-bucket \
  --aws-access-key KEY --aws-secret-key SECRET --s3-region us-east-1

Restore

dbbackup restore \
  --db-type postgresql \
  --host localhost --username postgres --password yourpass \
  --database mydb \
  --backup-file mydb_full_20240118_120000.dump.gz \
  --storage-path ./backups

List available backups

dbbackup list-backups --storage-type local --storage-path ./backups

Using a config file instead of flags

# config.yaml
database:
  db_type: postgresql
  host: localhost
  port: 5432
  username: postgres
  password: yourpass
  database: mydb

storage:
  storage_type: s3
  s3_bucket: my-backup-bucket
  s3_region: us-east-1
  aws_access_key: KEY
  aws_secret_key: SECRET

backup:
  backup_type: full
  compress: true
  compression_level: 6
  retention_days: 30
dbbackup --config config.yaml backup

Storage backends

Backend Config key Required credentials
Local filesystem local --storage-path
AWS S3 s3 --aws-access-key, --aws-secret-key, --s3-region
Google Cloud Storage gcp --gcp-credentials, --gcp-project
Azure Blob Storage azure --azure-account-name, --azure-account-key

Automation

Cron (daily at 2 AM):

0 2 * * * /usr/local/bin/dbbackup --config /etc/backup/config.yaml backup >> /var/log/db-backup.log 2>&1

Environment variables (no config file needed):

export DB_TYPE=postgresql
export DB_HOST=localhost
export DB_USER=postgres
export DB_PASSWORD=yourpass
export DB_NAME=mydb
export STORAGE_TYPE=local
export STORAGE_PATH=./backups

Project structure

Database-Backup-Utility-Tool/
├── db_backup_utility/
│   ├── cli.py                       # Click CLI — entry point
│   ├── core/
│   │   ├── config.py                # Config dataclasses + loaders (file, env, dict)
│   │   └── logger.py                # Coloured console + optional file logging
│   ├── databases/
│   │   ├── base.py                  # DatabaseConnector ABC
│   │   ├── mysql_connector.py
│   │   ├── postgresql_connector.py
│   │   ├── mongodb_connector.py
│   │   └── factory.py               # create_connector(config) factory
│   ├── storage/
│   │   ├── base.py                  # StorageHandler ABC
│   │   ├── local_storage.py
│   │   ├── s3_storage.py
│   │   ├── gcp_storage.py
│   │   ├── azure_storage.py
│   │   └── factory.py               # create_storage_handler(config) factory
│   ├── backup/manager.py            # BackupManager — orchestrates dump → compress → upload
│   ├── restore/manager.py           # RestoreManager — orchestrates download → decompress → restore
│   └── utils/
│       ├── compression.py           # gzip compress/decompress with chunked I/O
│       └── helpers.py               # filename generation, checksums, byte formatting
├── config.example.json
├── config.example.yaml
├── .env.example
├── requirements.txt
├── setup.py
└── EXAMPLES.md                      # 22 worked examples covering every backup/restore scenario

Security notes

Credentials are never written to disk or included in log output. PostgreSQL passwords are passed via environment variable (PGPASSWORD) to avoid exposure in process listings. Config files containing credentials should have restricted permissions (chmod 600). The .gitignore excludes all *.sql, *.dump, *.gz, and config files with passwords by default.


License

MIT — see LICENSE

About

A smart, automated database backup CLI for multiple DBs with secure storage, compression, and restore capabilities.

Topics

Resources

License

Stars

11 stars

Watchers

0 watching

Forks

Releases

No releases published

Sponsor this project

Contributors

Languages