How to backup MySQL/ MariaDB databases with Docker

Need to backup your MySQL database ? Learn how to do it. We have created a container that automatically saves at the time you want.

How to backup MySQL/ MariaDB databases with Docker

If you are deploying your applications using docker you may have wondered how to manage the backup of production databases. Indeed, traditionally, without docker, you would have made a small script based on mysql_dump in a cron on the host machine. Simple and efficient.

Unfortunately, under Docker, it is recommended not to install anything outside of the containers. So no cron or mysql_dump.

The first idea that comes is to still install these tools on the host and make port 3306 accessible from the host. Unsatisfactory and complicated solution to implement in the event of a Swarm cluster for example and with non-neutral security impacts.

Simplify the backup of your container

This is why we have developed another method to save another docker container using a docker container in a simple and efficient way, whatever the architecture of your cluster and without installing anything on the host.

The solution is to deploy a simple container which will contain the cron and MySQLldump tools and which will back up the server you want.

It could not be easier:

Add the backup image to your docker compose and provides it with some information:

db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: XXX
  volumes:
    - /data/volumes/mysql-db:/var/lib/mysql

backup:
  image: kiwibackup/mysqlbackup
  environment:
    - DBHOST=db
    - DBPASS=XXX
    - DBLOGIN=root
    - CRONH=1
    - CRONM=19
  volumes:
    - /data/backup/db:/backup