A comprehensive CLI tool for database backup and restore operations supporting MySQL, PostgreSQL, and MongoDB with multiple storage backends.
- 🗄️ Multiple Database Support: MySQL, PostgreSQL, and MongoDB
- 📦 Backup Types: Full, incremental, and differential backups
- 🗜️ Automatic Compression: Gzip compression with configurable levels
- ☁️ Cloud Storage: Support for AWS S3, Google Cloud Storage, and Azure Blob Storage
- 💾 Local Storage: Traditional local filesystem storage
- 🔄 Restore Operations: Full database and selective table/collection restore
- 📊 Database Information: View database size and table/collection counts
- 🔒 Secure: Password-protected connections and encrypted cloud storage
- 📝 Comprehensive Logging: Betterstack-compatible structured logging
- 🌐 Cross-Platform: Compatible with Windows, Linux, and macOS
- ⚡ Large Database Support: Efficient handling of large databases with chunked operations
- Python 3.8 or higher
- Database command-line tools (depending on your database):
- MySQL:
mysqlandmysqldump - PostgreSQL:
psql,pg_dump, andpg_restore - MongoDB:
mongodumpandmongorestore
- MySQL:
- Clone the repository:
git clone https://github.com/kisugez/Database-Backup-Utility.git
cd Database-Backup-Utility- Install dependencies:
pip install -r requirements.txt- Install the package:
pip install -e .pip install -r requirements.txtBefore performing any backup or restore operations, test your database connection:
dbbackup test-connection \
--db-type mysql \
--host localhost \
--port 3306 \
--username root \
--password your_password \
--database mydbdbbackup backup \
--db-type mysql \
--host localhost \
--port 3306 \
--username root \
--password your_password \
--database mydb \
--storage-path ./backupsdbbackup backup \
--db-type postgresql \
--host localhost \
--port 5432 \
--username postgres \
--password your_password \
--database mydb \
--storage-type s3 \
--storage-path my-backup-bucket \
--aws-access-key YOUR_ACCESS_KEY \
--aws-secret-key YOUR_SECRET_KEY \
--s3-region us-east-1dbbackup backup \
--db-type mongodb \
--host localhost \
--port 27017 \
--username admin \
--password your_password \
--database mydb \
--table my_collection \
--storage-path ./backupsdbbackup restore \
--db-type mysql \
--host localhost \
--port 3306 \
--username root \
--password your_password \
--database mydb \
--backup-file mydb_full_20240118_120000.sql.gz \
--storage-path ./backupsdbbackup restore \
--db-type mysql \
--host localhost \
--port 3306 \
--username root \
--password your_password \
--database mydb \
--backup-file users_full_20240118_120000.sql.gz \
--storage-path ./backups \
--table usersdbbackup restore \
--db-type postgresql \
--host localhost \
--port 5432 \
--username postgres \
--password your_password \
--database mydb \
--backup-file /path/to/backup.dump.gz \
--local-file# List local backups
dbbackup list-backups \
--storage-type local \
--storage-path ./backups
# List S3 backups with prefix filter
dbbackup list-backups \
--storage-type s3 \
--storage-path my-backup-bucket \
--prefix mydb_ \
--aws-access-key YOUR_ACCESS_KEY \
--aws-secret-key YOUR_SECRET_KEYInstead of passing all options via command line, you can use a configuration file:
Create a config.json file:
{
"database": {
"db_type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "your_password",
"database": "mydb"
},
"storage": {
"storage_type": "local",
"local_path": "./backups"
},
"backup": {
"backup_type": "full",
"compress": true,
"compression_level": 6,
"retention_days": 30
},
"log_file": "backup.log",
"betterstack_token": null
}Create a config.yaml file:
database:
db_type: postgresql
host: localhost
port: 5432
username: postgres
password: your_password
database: mydb
storage:
storage_type: s3
s3_bucket: my-backup-bucket
s3_region: us-east-1
aws_access_key: YOUR_ACCESS_KEY
aws_secret_key: YOUR_SECRET_KEY
backup:
backup_type: full
compress: true
compression_level: 9
retention_days: 60
log_file: backup.logdbbackup --config config.json backup
dbbackup --config config.yaml restore --backup-file mydb_full_20240118_120000.dump.gzYou can also configure the tool using environment variables:
# Database configuration
export DB_TYPE=mysql
export DB_HOST=localhost
export DB_PORT=3306
export DB_USER=root
export DB_PASSWORD=your_password
export DB_NAME=mydb
# Storage configuration
export STORAGE_TYPE=local
export STORAGE_PATH=./backups
# For AWS S3
export STORAGE_TYPE=s3
export S3_BUCKET=my-backup-bucket
export S3_REGION=us-east-1
export AWS_ACCESS_KEY=YOUR_ACCESS_KEY
export AWS_SECRET_KEY=YOUR_SECRET_KEY
# Backup configuration
export BACKUP_TYPE=full
export BACKUP_COMPRESS=true
export BACKUP_COMPRESSION_LEVEL=6
# Logging
export LOG_FILE=backup.log
export BETTERSTACK_TOKEN=your_betterstack_tokendbbackup backup \
--db-type mysql \
--host localhost \
--username root \
--password your_password \
--database mydb \
--storage-type s3 \
--storage-path my-bucket-name \
--aws-access-key YOUR_ACCESS_KEY \
--aws-secret-key YOUR_SECRET_KEY \
--s3-region us-east-1dbbackup backup \
--db-type postgresql \
--host localhost \
--username postgres \
--password your_password \
--database mydb \
--storage-type gcp \
--storage-path my-bucket-name \
--gcp-project my-project-id \
--gcp-credentials /path/to/credentials.jsondbbackup backup \
--db-type mongodb \
--host localhost \
--username admin \
--password your_password \
--database mydb \
--storage-type azure \
--storage-path my-container-name \
--azure-account-name mystorageaccount \
--azure-account-key YOUR_ACCOUNT_KEYBy default, logs are output to the console with colored formatting:
dbbackup --log-level DEBUG backup [options]Save logs to a file:
dbbackup --log-file backup.log backup [options]For centralized logging with Betterstack (Logtail):
- Install the Betterstack library:
pip install logtail-python- Set your Betterstack token:
export BETTERSTACK_TOKEN=your_source_tokenOr in config file:
{
"betterstack_token": "your_source_token"
}--config, -c: Path to configuration file (JSON or YAML)--log-level: Logging level (DEBUG, INFO, WARNING, ERROR)--log-file: Path to log file
Perform a database backup.
Options:
--db-type: Database type (mysql, postgresql, mongodb) [required]--host: Database host (default: localhost)--port: Database port--username, -u: Database username [required]--password, -p: Database password [required]--database, -d: Database name [required]--backup-type: Backup type (full, incremental, differential) (default: full)--storage-type: Storage type (local, s3, gcp, azure) (default: local)--storage-path: Storage path or bucket/container name--compress/--no-compress: Enable/disable compression (default: enabled)--table: Specific table/collection to backup
Cloud Storage Options:
- AWS S3:
--aws-access-key,--aws-secret-key,--s3-region - GCP:
--gcp-credentials,--gcp-project - Azure:
--azure-account-name,--azure-account-key
Restore a database from backup.
Options:
--db-type: Database type (mysql, postgresql, mongodb) [required]--host: Database host (default: localhost)--port: Database port--username, -u: Database username [required]--password, -p: Database password [required]--database, -d: Database name [required]--backup-file: Backup file name or path [required]--storage-type: Storage type (local, s3, gcp, azure) (default: local)--storage-path: Storage path or bucket/container name--local-file: Backup file is a local file (not in storage)--table: Specific table/collection to restore
Cloud Storage Options: (same as backup command)
Test database connection.
Options:
--db-type: Database type [required]--host: Database host (default: localhost)--port: Database port--username, -u: Database username [required]--password, -p: Database password [required]--database, -d: Database name [required]
List available backups in storage.
Options:
--storage-type: Storage type (default: local)--storage-path: Storage path or bucket/container name [required]--prefix: Filter backups by prefix
Cloud Storage Options: (same as backup command)
Ensure the required command-line tools are installed and in your PATH:
MySQL:
# Ubuntu/Debian
sudo apt-get install mysql-client
# macOS
brew install mysql-client
# Windows
# Download from https://dev.mysql.com/downloads/mysql/PostgreSQL:
# Ubuntu/Debian
sudo apt-get install postgresql-client
# macOS
brew install postgresql
# Windows
# Download from https://www.postgresql.org/download/windows/MongoDB:
# Ubuntu/Debian
sudo apt-get install mongodb-database-tools
# macOS
brew install mongodb-database-tools
# Windows
# Download from https://www.mongodb.com/try/download/database-tools- Verify database is running
- Check host and port settings
- Verify username and password
- Check firewall settings
- Use
test-connectioncommand to diagnose
Ensure the database user has appropriate permissions:
MySQL:
GRANT SELECT, LOCK TABLES, SHOW VIEW, TRIGGER ON mydb.* TO 'user'@'host';PostgreSQL:
GRANT CONNECT ON DATABASE mydb TO user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO user;MongoDB:
db.grantRolesToUser("user", ["backup", "restore"])This project was created on the copilot/create-cli-database-tool branch. To commit and push your changes manually to GitHub:
git status# Stage all changes
git add .
# Or stage specific files
git add db_backup_utility/ README.md requirements.txt setup.pygit commit -m "Your commit message describing the changes"# Push to the current branch
git push origin copilot/create-cli-database-tool
# Or if you've set up tracking
git pushIf you want to merge this branch into main:
- Go to https://github.com/kisugez/Database-Backup-Utility
- Click "Pull requests" tab
- Click "New pull request"
- Select
copilot/create-cli-database-toolas the source branch - Select
mainas the target branch - Review changes and create the pull request
- Merge when ready
git checkout main
git pull origin mainDatabase-Backup-Utility/
├── db_backup_utility/ # Main package
│ ├── __init__.py
│ ├── cli.py # CLI interface
│ ├── core/ # Core functionality
│ │ ├── config.py # Configuration management
│ │ └── logger.py # Logging setup
│ ├── databases/ # Database connectors
│ │ ├── base.py # Base connector interface
│ │ ├── mysql_connector.py
│ │ ├── postgresql_connector.py
│ │ ├── mongodb_connector.py
│ │ └── factory.py # Connector factory
│ ├── storage/ # Storage handlers
│ │ ├── base.py # Base storage interface
│ │ ├── local_storage.py
│ │ ├── s3_storage.py
│ │ ├── gcp_storage.py
│ │ ├── azure_storage.py
│ │ └── factory.py # Storage factory
│ ├── backup/ # Backup operations
│ │ └── manager.py # Backup manager
│ ├── restore/ # Restore operations
│ │ └── manager.py # Restore manager
│ └── utils/ # Utilities
│ ├── compression.py # Compression utilities
│ └── helpers.py # Helper functions
├── requirements.txt # Python dependencies
├── setup.py # Package setup
├── .gitignore # Git ignore rules
└── README.md # This file
MIT License - See LICENSE file for details
Contributions are welcome! Please feel free to submit a Pull Request.
For issues and questions, please open an issue on GitHub: https://github.com/kisugez/Database-Backup-Utility/issues