Easy database backups with docker-compose

I implemented a backup tool for my new docker compose and traefik stack to help save and restore my databases and avoid loss of data.

Easy database backups with docker-compose
Saving data to 3 different locations, of which at least one offline, is a golden rule of data security. Photo by Markus Spiske / Unsplash

After I nuked my mastodon instance in December, something that took me 3 months to recover, I decided to implement a backup solution for this worst case scenarios.

Idea

I wanted a simple, automatic, and open source solution to do databases backups. Ideally, it should be able to connect to multiple databases and backup them. It should also be possible to easily restore backups when bad things happens.

Ideally it should have a neat UI allowing me to easily insert handle this tasks.

Lastly, it should also be docker based for easy installation.

It sounds like an incredible software, like? Maybe too nice to be true?

In fact, it is, I couldn't find an open source software with all these requirements.

Software selected

After reducing the requirements to the most important ones, I selected this OSS project called docker-db-backup from Dave Conroy.

It is a docker container which you can attach to only a single database, unfortunately, and it backs it up. It allows for pleasant configurations and simple restore options. Furthermore, it can also upload backups to s3 if needed.

Developer machines once they start to use docker containers. Photo by Ian Taylor / Unsplash

Installation

To install it, I simply have to add it to the docker compose where my database container is already present.

In the case of my ghost installation for this blog I used this config.

version: '3.8'

services:

  db_backup:
    container_name: db_backup
    image: tiredofit/db-backup
    depends_on:
      - mysql
    volumes:
      - ../backups/danielpetrica_com/ghost_db/:/backup
      #- ./post-script.sh:/assets/custom-scripts/post-script.sh
    environment:
      # - DEBUG_MODE=TRUE
      - DB_TYPE=mysql
      - DB_HOST=ghost_mysql
      - DB_NAME=${DATABASE__CONNECTION__DATABASE}
      - DB_USER=root
      - DB_PASS=${DATABASE__CONNECTION__PASSWORD}
      - DB_DUMP_FREQ=720          # backup 12 hours 12 * 60
      # - DB_DUMP_BEGIN=0000      # backup starts immediately
      - DB_CLEANUP_TIME=72000     # clean backups that are older than 72000 minutes
      - CHECKSUM=SHA1
      - COMPRESSION=GZ
      - SPLIT_DB=true
      - CONTAINER_ENABLE_MONITORING=FALSE
    restart: always
    
networks:
  default:
    external: true
    name: traefik

The other services are published in the article about my blog migration: From Dokku to Traefik, migration of a blog

If you need different configs or databases, feel free to check them here https://github.com/tiredofit/docker-db-backup#configuration

Finally, in my config, I back files in a directory relative to the parent folder.

This is currently not final. I choose to do this to enable the integration of another backup tool down the line, which will save them offsite on my unraid machine and, ideally, on Microsoft OneDrive. OneDrive because of its huge storage quota of 6 TB on the family plan.

I hope you found this short article a little helpful. I'll extend it to include more examples and configs once I go through them. If you have any advice or question, please use the comment section.

Mastodon Mastodon