A CLI tool for automated database backup and restore across MySQL, PostgreSQL, and MongoDB — with local and cloud storage backends.
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.
Strategy pattern for database connectors — DatabaseConnector 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.
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 |
dbbackup test-connection \
--db-type postgresql \
--host localhost --port 5432 \
--username postgres --password yourpass \
--database mydbdbbackup backup \
--db-type mysql \
--host localhost --username root --password yourpass \
--database mydb \
--storage-path ./backupsdbbackup 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-1dbbackup restore \
--db-type postgresql \
--host localhost --username postgres --password yourpass \
--database mydb \
--backup-file mydb_full_20240118_120000.dump.gz \
--storage-path ./backupsdbbackup list-backups --storage-type local --storage-path ./backups# 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: 30dbbackup --config config.yaml backup| 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 |
Cron (daily at 2 AM):
0 2 * * * /usr/local/bin/dbbackup --config /etc/backup/config.yaml backup >> /var/log/db-backup.log 2>&1Environment 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=./backupsDatabase-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
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.
MIT — see LICENSE