Skip to main content

No project description provided

Project description

docker-dbdump - database backup for docker containers

This python script creates backups of databases which run in Docker containers. Please create an issue if you run into problems.

How does it work?

  1. Iterate over all running Docker containers
  2. Check if it's a database container (MySQL, MariaDB, PostgreSQL, PostgriSQL)
  3. If so, run mysqldump or the equivalent
  4. Use -v/--verbose, it's helpful

Features

  • Backups all database containers, a few specified ones or all containers with some exceptions
  • Creates rsyncable zip files (can be disabled with --skip-gzip)
  • Creates a state file (useful for monitoring) with --update-state-file
  • Always returns with exit code 0 unless you specify --fail

Getting started

Please check the docker-compose.yml in the repo. It spawns some db containers and creates a backup. Use docker-dbdump -h for more information.

vim docker-compose.yml
sudo docker-compose up

Installation

using pip:

pip install docker-dbdump

Using docker:

docker pull ghcr.io/kmille/docker-dbdump:latest

Warning: When using the docker image, you have to mount the Docker socket (-v /var/run/docker.sock:/var/run/docker.sock).

Using docker-compose:

services: 
  docker-dbdump:
    image: ghcr.io/kmille/docker-dbdump:latest
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /backups:/backups
    command: "docker-dbdump --all --verbose"

Use docker-compose -f /opt/container-backups/docker-compose.yml up in cronjobs.

dev environment

sudo docker-compose up # run some DB container
sudo poetry install

kmille@linbox:docker-dbdump sudo poetry run docker-dbdump -h
usage: [-h] [-v] [--backup-dir BACKUP_DIR] [-l [LIST]] [-a] [-b BACKUP [BACKUP ...]] [-i IGNORE_CONTAINER [IGNORE_CONTAINER ...]] [--skip-gzip] [-s] [--fail] [--version]

options:
  -h, --help            show this help message and exit
  -v, --verbose         print verbose output. Warning! This logs database passwords!
  --backup-dir BACKUP_DIR
                        output directory for backups
  -l, --list [LIST]     show running docker containers. Add argument to grep
  -a, --all             backup all running db containers
  -b, --backup BACKUP [BACKUP ...]
                        only backup specific containers
  -i, --ignore-container IGNORE_CONTAINER [IGNORE_CONTAINER ...]
                        backup all running db containers except the ones specified (can be used multiple times)
  --skip-gzip           Do not create create rsyncable gzip dump
  -s, --update-state-file
                        update state file (/var/log/docker-dbdump.done) with current date if everything succeeds
  --fail                if --fail is specified, the script will return with exit code 1 if an error occurs. If not specified, the exit code is always 0
  --version             print version and exit

Note: If you use --ignore-container option multiple times, please use the format -i container1 container2.

Example output

kmille@linbox:docker-dbdump sudo poetry run docker-dbdump -l
docker-dbdump-maria-db-1       mariadb:11.2.2                 /home/kmille/projects/docker-dbdump
docker-dbdump-postgres-db-1    postgres:latest                /home/kmille/projects/docker-dbdump
docker-dbdump-postgis-db-1     postgis/postgis:latest         /home/kmille/projects/docker-dbdump
docker-dbdump-mysql-db-1       mysql:latest                   /home/kmille/projects/docker-dbdump

kmille@linbox:docker-dbdump sudo poetry run docker-dbdump -l maria
docker-dbdump-maria-db-1       mariadb:11.2.2                 /home/kmille/projects/docker-dbdump

kmille@linbox:docker-dbdump sudo poetry run docker-dbdump --all --verbose --update-state-file
[2025-02-23 09:48:43,156 DEBUG] Dumping backups to /backups
[2025-02-23 09:48:43,172 INFO] Starting to backup container docker-dbdump-postgis-db-1 (postgres)
[2025-02-23 09:48:43,172 DEBUG] Running dump 'docker exec b94ef9366188a5ede0187f92b48f733e9bdbb88be28e9453acccb45343ea2a7d pg_dumpall --username 'test-user' | gzip --rsyncable > /backups/_home_kmille_projects_docker-dbdump_docker-dbdump-postgis-db-1_test-user_postgres.sql.gz'
[2025-02-23 09:48:44,056 DEBUG] Sucessfully dumped backup
[2025-02-23 09:48:44,056 DEBUG] Checking backup with '['zgrep', '-q', '--', 'PostgreSQL database cluster dump', '/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-postgis-db-1_test-user_postgres.sql.gz']'
[2025-02-23 09:48:44,067 DEBUG] Created backup looks good
[2025-02-23 09:48:44,067 INFO] Done backuping container docker-dbdump-postgis-db-1 (postgres)
[2025-02-23 09:48:44,070 INFO] Starting to backup container docker-dbdump-postgres-db-1 (postgres)
[2025-02-23 09:48:44,070 DEBUG] Running dump 'docker exec 3c6c7c220d34182d846127ea09fae1d2bd88b3ccd341a471eca8787522d782ed pg_dumpall --username 'postgres' | gzip --rsyncable > /backups/_home_kmille_projects_docker-dbdump_docker-dbdump-postgres-db-1_postgres_postgres.sql.gz'
[2025-02-23 09:48:44,399 DEBUG] Sucessfully dumped backup
[2025-02-23 09:48:44,399 DEBUG] Checking backup with '['zgrep', '-q', '--', 'PostgreSQL database cluster dump', '/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-postgres-db-1_postgres_postgres.sql.gz']'
[2025-02-23 09:48:44,409 DEBUG] Created backup looks good
[2025-02-23 09:48:44,410 INFO] Done backuping container docker-dbdump-postgres-db-1 (postgres)
[2025-02-23 09:48:44,412 INFO] Starting to backup container docker-dbdump-maria-db-1 (mariadb)
[2025-02-23 09:48:44,412 DEBUG] Running dump 'docker exec 91dafc7f3a3a7fdeec5bd3993df5bb96fac2a6e9b5334c8e3b4d6dcac5a39d58 mariadb-dump -u 'root' -p'mariadb-root-password' --single-transaction --skip-lock-tables --all-databases | gzip --rsyncable > /backups/_home_kmille_projects_docker-dbdump_docker-dbdump-maria-db-1_root_mariadb.sql.gz'
[2025-02-23 09:48:44,906 DEBUG] Sucessfully dumped backup
[2025-02-23 09:48:44,907 DEBUG] Checking backup with '['zgrep', '-q', '-i', '--', '^-- mariadb dump', '/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-maria-db-1_root_mariadb.sql.gz']'
[2025-02-23 09:48:44,919 DEBUG] Checking backup with '['zgrep', '-q', '--', '^-- Host: localhost', '/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-maria-db-1_root_mariadb.sql.gz']'
[2025-02-23 09:48:44,930 DEBUG] Created backup looks good
[2025-02-23 09:48:44,931 INFO] Done backuping container docker-dbdump-maria-db-1 (mariadb)
[2025-02-23 09:48:44,933 INFO] Starting to backup container docker-dbdump-mysql-db-1 (mysql)
[2025-02-23 09:48:44,933 DEBUG] Running dump 'docker exec 6482f3ed4c47ebaae9289614adcacdef92ee66f4581c5d1cc2e726d66ac0ae1c mysqldump -u 'root' -p'mysql-root-password' --single-transaction --skip-lock-tables --all-databases | gzip --rsyncable > /backups/_home_kmille_projects_docker-dbdump_docker-dbdump-mysql-db-1_root_mysql.sql.gz'
[2025-02-23 09:48:45,546 DEBUG] Sucessfully dumped backup
[2025-02-23 09:48:45,546 DEBUG] Checking backup with '['zgrep', '-q', '-i', '--', '^-- mysql dump', '/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-mysql-db-1_root_mysql.sql.gz']'
[2025-02-23 09:48:45,558 DEBUG] Checking backup with '['zgrep', '-q', '--', '^-- Host: localhost', '/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-mysql-db-1_root_mysql.sql.gz']'
[2025-02-23 09:48:45,570 DEBUG] Created backup looks good
[2025-02-23 09:48:45,570 INFO] Done backuping container docker-dbdump-mysql-db-1 (mysql)
[2025-02-23 09:48:45,571 INFO] Updated state file /var/log/docker-dbdump.done
[2025-02-23 09:48:45,571 INFO] Everything worked fine. Exiting with exit code 0.

kmille@linbox:docker-dbdump cat /var/log/docker-dbdump.done
2025-02-23 09:48:45.570701
kmille@linbox:docker-dbdump ls -l /var/log/docker-dbdump.done
-rw-r--r-- 1 root root 27 Feb 23 09:48 /var/log/docker-dbdump.done

root@linbox: file /backups/_home_kmille_projects_docker-dbdump_docker-dbdump-*
/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-maria-db-1_root_mariadb.sql.gz:         gzip compressed data, from Unix, original size modulo 2^32 3210314
/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-mysql-db-1_root_mysql.sql.gz:           gzip compressed data, from Unix, original size modulo 2^32 3763792
/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-postgis-db-1_test-user_postgres.sql.gz: gzip compressed data, from Unix, original size modulo 2^32 11915
/backups/_home_kmille_projects_docker-dbdump_docker-dbdump-postgres-db-1_postgres_postgres.sql.gz: gzip compressed data, from Unix, original size modulo 2^32 1789

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

docker_dbdump-0.3.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

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

docker_dbdump-0.3.0-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file docker_dbdump-0.3.0.tar.gz.

File metadata

  • Download URL: docker_dbdump-0.3.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.5 Linux/6.14.11-hardened1-1-hardened

File hashes

Hashes for docker_dbdump-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f7665178f4bb83f895a1bdbae04f7071d831c3b683bcd66c79807d234f73f16c
MD5 0a2dc20246e0590d69353af67743fbe8
BLAKE2b-256 2706a8475753b46e01f1567fdbd29f250f7c4d415e2fb8a96a6b3bd93745830e

See more details on using hashes here.

File details

Details for the file docker_dbdump-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: docker_dbdump-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.5 Linux/6.14.11-hardened1-1-hardened

File hashes

Hashes for docker_dbdump-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40080186140c632b498f64035c48ea40f53b7f8ef2df8c7b2deff0a577f18f5b
MD5 3fe2e603a489e43abcfe7f3db7ce62c9
BLAKE2b-256 2508c62b5690bcf9f96c96fbe8249fe17c42367c8b81504ab1869dfe2f6829e4

See more details on using hashes here.

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