Skip to content
Go back

Automatic backup of docker Mysql or MariaDB container

By SumGuy 5 min read
Automatic backup of docker Mysql or MariaDB container

Why do I need MySQL / MariaDB backups?

Regular backups of your MySQL databases are essential for several reasons, whether you are hosting as a hobby project at home or for a business with an SLA.

Overall, regular backups of your MySQL databases are crucial for protecting your data, ensuring business continuity, and complying with regulations. It is recommended to establish a backup strategy that includes regular backups and storing backups in multiple locations along with just as important: restoring said backups to ensure the restore works.

Install docker

Install docker on Ubuntu/DebianInstall docker on Ubuntu/Debian

Or install docker rootless

How to install Docker rootlessHow to install Docker rootless

Edit docker-compose.yml

Edit your docker-compose.yml and add the section from line 18 – Line 39.

version: '3'
services:
mycoolapp_db:
image: mariadb:10
container_name: mycoolapp_db
volumes:
- ./db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: mycoolapp_r_p
MYSQL_DATABASE: mycoolapp
MYSQL_USER: mycoolapp
MYSQL_PASSWORD: mycoolapp
restart: unless-stopped
networks:
- net
mycoolapp_db_backup:
image: fradelg/mysql-cron-backup
container_name: mycoolapp_db_backup
depends_on:
- mycoolapp_db
volumes:
- ./db_backup:/backup
environment:
- MYSQL_HOST=mycoolapp_db
- MYSQL_DATABASE=mycoolapp
- MYSQL_USER=mycoolapp
- MYSQL_PASS=mycoolapp
- MAX_BACKUPS=2
- INIT_BACKUP=0
# Every day at 03:00
- CRON_TIME=42 3 * * *
- GZIP_LEVEL=9
- TZ=America/New_York
- MYSQLDUMP_OPTS=--skip-lock-tables --single-transaction --quick
restart: unless-stopped
networks:
- net
networks:
net:

ENV variables you can use:

The ones that matter most here (imo) are the following:

INIT_BACKUP: I don’t bother with this because I want backups done on a schedule. INIT_RESTORE_LATEST: this restores the last backup made on launch, this is dangerous, be careful setting this. EXIT_BACKUP: I also don’t have this as again I need timed backups, but this can be very useful to make backups on the shutdown of a container. MAX_BACKUPS: very important, this is basically how many days of backups you will have. I keep it low because I have these backups moved to remote storage where they are kept for up to 6 months. CRON_TIME: this follows standard cron notation which you can change using a resource like https://crontab.guru .

you can get the latest info from the projects homepage : https://github.com/fradelg/docker-mysql-cron-backup


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Install & use Doxygen via Docker
Next Post
WordPress on PHP-FPM & Caddy in Docker

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts