Skip to main content

A CLI tool for backing up databases to cloud storage.

Project description

Database Backup Tool

A command-line tool for backing up MySQL databases to local storage or AWS S3.

Features

  • Back up all MySQL databases, excluding system databases.
  • Store backups in a local directory or an AWS S3 bucket.
  • Create a separate folder for each database.
  • Timestamped backups for easy identification.
  • Automatic cleanup of old backups based on a retention policy.
  • Configuration via a .env file.
  • Command-line interface for easy operation.

Requirements

  • Python 3.11+

  • mysql-connector-python

  • boto3

  • python-dotenv

  • click

  • MySQL client tools (provides mysqldump)

    On macOS (Homebrew):

    brew install mysql-client
    # Typical binary path: /opt/homebrew/opt/mysql-client/bin/mysqldump (Apple Silicon)
    

Installation

  1. Clone the repository:

    git clone https://github.com/your-username/database-backup.git
    
  2. Install the required libraries:

    pip install -r requirements.txt
    
  3. Create a .env file in the project root and add the following configuration:

    MYSQL_HOST=localhost
    MYSQL_PORT=3306
    MYSQL_USER=root
    MYSQL_PASSWORD=mysecretpassword
    BACKUP_DIR=./backups
    S3_BUCKET=mybucket
    S3_PATH=backups
    AWS_ACCESS_KEY_ID=XXXXXXX
    AWS_SECRET_ACCESS_KEY=YYYYYYY
    RETENTION_COUNT=5
    # Optional: set full path or command name for mysqldump
    MYSQLDUMP_PATH=/opt/homebrew/opt/mysql-client/bin/mysqldump
    BACKUP_DRIVER=local # local, s3
    EXCLUDED_DATABASES=db_1,db_2
    

Configuration

By default, the CLI loads config from:

  • macOS/Linux: ~/.config/database-backup/.env (or ${XDG_CONFIG_HOME}/database-backup/.env)

Override with --config or DATABASE_BACKUP_CONFIG env.

Example .env:

MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=password
BACKUP_DIR=/Users/<USER>/backups/databses
S3_BUCKET=mybucket
S3_PATH=backups
AWS_ACCESS_KEY_ID=XXXXXXX
AWS_SECRET_ACCESS_KEY=YYYYYYY
RETENTION_COUNT=5
MYSQLDUMP_PATH=/opt/homebrew/opt/mysql-client/bin/mysqldump
BACKUP_DRIVER=local # local, s3
EXCLUDED_DATABASES=db_1,db_2

CLI usage

After installation as a package, use the short command:

db-backup --local
# or
database-backup --s3

Options:

  • --compress/--no-compress (default: --compress): gzip the dump and keep .gz.
  • --mysqldump PATH: override mysqldump path.
  • --config FILE: override config file path.

You can still run the module directly:

python -m db_backup --local

Usage

Preferred: run as a module from the project root (this works reliably regardless of relative imports):

python -m db_backup --config .env --local

Or run the script directly (works after the import fallback fix):

python db_backup/main.py --config .env --local

You can override mysqldump path via CLI:

python -m db_backup --config .env --local --mysqldump /opt/homebrew/opt/mysql-client/bin/mysqldump

To store your backups in an S3 bucket:

python -m db_backup --config .env --s3

You can also override the retention count and backup directory using the command-line options:

python -m db_backup --config .env --retention 10 --local --backup-dir /path/to/backups

Architecture

The database backup tool is built using a Clean Architecture approach, which separates the code into four layers:

  • Domain: Contains the core business logic and entities of the application.
  • Data: Contains the data access layer, which is responsible for interacting with the database and storage.
  • App: Contains the application logic, which orchestrates the backup process.
  • Interface: Contains the user interface, which is responsible for handling user input and displaying output.

This separation of concerns makes the application more modular, testable, and maintainable.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or feedback.

License

This project is licensed under the MIT License.

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

database_backup-0.1.10.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

database_backup-0.1.10-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file database_backup-0.1.10.tar.gz.

File metadata

  • Download URL: database_backup-0.1.10.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for database_backup-0.1.10.tar.gz
Algorithm Hash digest
SHA256 50a7d2b4a836cc4857c20decb920a585942b776e3cecfd61bff61e7215acc651
MD5 ecdfab8a968bee1fef4312504b11020e
BLAKE2b-256 5ed37399ffac589c6ef872881271b3051ce3e4d00a0bb5cd4df4983046a9bb43

See more details on using hashes here.

Provenance

The following attestation bundles were made for database_backup-0.1.10.tar.gz:

Publisher: publish.yml on magicstack-llp/db-backup

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file database_backup-0.1.10-py3-none-any.whl.

File metadata

File hashes

Hashes for database_backup-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 71bf750dc2c209dc75049c7c41edc0b18a883d65e85cc0483bfad4ee2dcd2eb6
MD5 ea22c790edcc566159de8a34f807a9d8
BLAKE2b-256 ceb4e2a1925dc7a55bce4777b5ee9d3125fd0a032fc921eeba5a80562ad5129c

See more details on using hashes here.

Provenance

The following attestation bundles were made for database_backup-0.1.10-py3-none-any.whl:

Publisher: publish.yml on magicstack-llp/db-backup

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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