Docker MySQL Backup

How to backing up a MySQL Docker container

Home / Posts / Docker MySQL Backup

We moved an internal tool to docker (docker-compose), this includes the required MySQL Database.
I looked for a easy solution todo a backup through mysqldump and found something great 🙂
This is the relevant docker-compose part:

1
2
3
mysql:
   networks:
     - back_net

If this docker-compose is running, the mysql container get the following name ‘ourtool_mysql_1’
The network gets the compose name added:

1
2
3
docker network ls
NETWORK ID          NAME                          DRIVER              SCOPE
4d42f74cfe7e        ourtool_pub_net    macvlan             local

We want our backups in the host folder /mnt/Backups/ourtool/sql (This is a nfs mounted folder).
Now we just put this information together:

1
2
3
4
5
docker run -it \
--net=ourtool_pub_net \
-v /mnt/Backups/ourtool/sql:/var/backup \
--rm mysql \
sh -c "exec mysqldump -h ourtool_mysql_1 -uroot -pYOURPASSWORD YOURDATABASE > /var/backup/mysql.dump"

That’s it, it runs, after that the temporary container gets destroyed and you can find the data on the host itself (’/mnt/Backups/ourtool/sql’ in my case)

We now just run this as a cronjob

1
2
3
4
5
6
7
8
9
cat /etc/cron.daily/backup_ourtool
#!/bin/sh

FILENAME="ourtool-`date -I`.sql"
docker run -it \
--net=ourtool_pub_net \
-v /mnt/Backups/ourtool/sql:/var/backup \
--rm mysql \
sh -c "exec mysqldump -h ourtool_mysql_1 -uroot -pYOURPASSWORD YOURDATABASE > /var/backup/$FILENAME"
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy