Docker and MySQL / MariaDB database backups

MySQL backup databaseIf 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 MySQL docker 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

Links

Don't hesitate to come up with new ideas and improvements.

Can’t we make it even simpler ?

The downside of this method is that you must not forget to add the backup container to your docker-compose.

An idea for improvement would be to have a container connected to docker APIs and which monitors all containers started on the environment and automatically saves them. This is possible thanks to the docker APIs and it would facilitate the deployment of new stacks without asking any questions.

To be continued …