Backup MySQL periodically, based on idea of prodrigestivill/docker-postgres-backup-local.
Supports the following Docker architectures: linux/amd64, linux/arm64, linux/arm/v7.
Bind the mysql data directory to the container, set the envs and bind the backup directory.
Docker:
docker run -v ${MYSQL_BACKUP_OUT}:/data -e MYSQL_BACKUP_KEEP_DAYS=7 -e MYSQL_BACKUP_KEEP_WEEKS=4 -e MYSQL_BACKUP_KEEP_MONTHS=6 -e "DB_USER=${DB_USER}" -e "DB_PASSWORD=${DB_PASSWORD}" -e DB_HOST=${DB_HOST} -e DB_PORT=${DB_PORT} psychomantys/mysql-backup
Docker Compose:
version: '3.8'
services:
app-db:
image: mariadb
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_TCP_PORT: ${DB_PORT}
volumes:
- app_db_data:/var/lib/mysql
app-backup-db:
image: psychomantys/mysql-backup:latest
environment:
MYSQL_BACKUP_KEEP_DAYS: 7
MYSQL_BACKUP_KEEP_WEEKS: 4
MYSQL_BACKUP_KEEP_MONTHS: 12
MYSQL_BACKUP_SCHEDULE: "00 03 * * * "
DB_NAMES: ${DB_NAME}
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
volumes:
- type: bind
source: /home/app/backup/mariadb/
target: /data
volumes:
app_db_data:
| env variable | Description |
|---|---|
| DB_NAMES | Space separated list of tables. If empty, backup all database. Default is empty. |
| DB_USER | User to bind and make the backup |
| DB_PASSWORD | Password for bind user |
| DB_HOST | Host of mysql server |
| DB_PORT | Port of running mysql server. Default to 3306 |
| MYSQL_BACKUP_DIR | Directory to save the backup at. Defaults to /data. |
| MYSQL_BACKUP_SUFFIX | Filename suffix to save the backup. Defaults to .ldif.xz. |
| MYSQL_BACKUP_KEEP_DAYS | Number of daily backups to keep before removal. Defaults to 7. |
| MYSQL_BACKUP_KEEP_WEEKS | Number of weekly backups to keep before removal. Defaults to 4. |
| MYSQL_BACKUP_KEEP_MONTHS | Number of monthly backups to keep before removal. Defaults to 6. |
| MYSQL_BACKUP_SCHEDULE | Cron like string to set daily execution of backup script. Defaults to 00 03 * * *. |
By default, the backup is make daily at 03:00. But, you can force a backup running the script /backup_mysql.sh.
To force a backup now with a container created on first section, you can make:
docker exec -it "${CONTAINER}" /backup_mysql.sh
You can change the MYSQL_BACKUP_SCHEDULE environment variable in -e MYSQL_BACKUP_SCHEDULE= to alter the default frequency. Default is 00 03 * * *.
More information about the scheduling can be found here.
Folders daily, weekly and monthly are created and populated using hard links to save disk space.
Some examples to restore/apply the backups.
Configure the container like in compose example. Replace the ${BACKUP_FILENAME} and ${CONTAINER}, from the following command:
docker exec -ti "${CONTAINER}" db_restore "${BACKUP_FILENAME}"
For example:
docker exec -ti mysql-backup_app-backup-db_1 db_restore "/data/daily/opa-2020-10-31-02:20:00.sql.xz"
Content type
Image
Digest
sha256:7db97c4e1…
Size
81.3 MB
Last updated
almost 4 years ago
docker pull psychomantys/mysql-backup