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.9.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.9-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: database_backup-0.1.9.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.9.tar.gz
Algorithm Hash digest
SHA256 f3c914b35d94903d6988a3710369a66044335429304b3993d48fe97640baede8
MD5 9e94157c9406ef12bd98163d06c59c09
BLAKE2b-256 89617110c45ffb46852722e0ed5a0eb5c788a3f39acd6c857fec4556575cf4eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for database_backup-0.1.9.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.9-py3-none-any.whl.

File metadata

File hashes

Hashes for database_backup-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 cc39592ef012a1a17577d85e21cfec281e79022bec639d76b73f2f166c959c97
MD5 2c423259c2c00dccf2d49f67c6c23324
BLAKE2b-256 a19a07a37bfa165391ea5b5441aa53efb050fece0ad1c709038a2b6a5b531e34

See more details on using hashes here.

Provenance

The following attestation bundles were made for database_backup-0.1.9-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