Backup a Docker MySQL database with CRON

Simple bash script and cron example to backup a MySQL database from within a Docker container

docker
MySQL
backup
cron

Published 4 years ago

Edit crontab -e

* * * * * cd /path/to/script && bash ./backup.sh >> /path/to/log/backup.log

backup.sh:

#!/bin/bash BACKUP_DIR="/home/backups" BACKUP_FILE_NAME="$(date +"%d-%m-%y-%H%M%S.sql.gz")" docker exec <CONTAINER_NAME> bash -c 'exec mysqldump --databases "$MYSQL_DATABASE" -h<DOCKER_MYSQL_SERVICE_NAME> -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"' > gzip > "$BACKUP_DIR"/"$BACKUP_FILE_NAME";