Sign inSign up

brainbitsgmbh/mysql-backup

By brainbitsgmbh

•Updated over 2 years ago

Image with mysql backup cli

Image
0

10K+

brainbitsgmbh/mysql-backup repository overview

⁠MySQL Backup

This image allows a simple backup strategy for mysql and mariadb databases. Backups are created via mysqldump and gziped, and can be restored with this image by just changing a single environment variable.

⁠Backup strategy

This image follows a simple strategy for keeping or removing backuped dumps:

  • It checks whether there already is a file in the output directory that was created on the current day.
    • If no, a new backup will be created and treated (and named) as a "daily" backup.
    • If yes, a new backup will be created and treated (and named) as an "hourly" backup.
  • Depending on whether it created a "daily" or "hourly" backup, it might remove certain older backup files:
    • Daily: It will remove all daily backups (files starting with the name daily-*) that are older than a specified amount of days.
    • Hourly: It will remove all hourly backups (files starting with the name hourly-*) and only keeps a specified amount of files.

This strategy ensures, that there are always a certain amount of backup files available from the last couple of days, but also that there are at least a few recent backups available too, without storing too many backups.

⁠Variables

The following environment variables can be used to configure the script:

VariableDefaultDescription
MODEbackupTell the script whether it should create a backup or restore one. Allowed values: backup restore
DATABASE_DSNThe DSN of the database you want to backup or restore in the format mysql://user:password@host:port/database_name
DATABASE_DSN_FILEAlternative to DATABASE_DSN if you prefer to pass in a path to a file that contains the DSN instead of passing it directly via environment variable.
BACKUP_DIRECTORY/backupThe directory (within the docker container) where the backup files are stored. This should usually be configured by changing the volume mount instead of this variable.
MAX_DAILY_BACKUP_AGE7Only for mode backup. The number of days to keep daily backups.
MAX_HOURLY_BACKUP_COUNT3Only for mode backup. The number backup files to keep for hourly backups.
BACKUP_RESTORE_NAMElatestOnly for mode restore. The filename of the backup to restore. The filename must be relative to BACKUP_DIRECTORY

⁠Examples

Example creating and restoring a backup using docker run:

# Create a backup. It will automatically create a link called `latest` to that backup.
docker run -it --rm -e "MODE=backup" -e "DATABASE_DSN=mysql://MyDbUser:[email protected]/my-database" -v "$(pwd)/backup:/backup" brainbitsgmbh/mysql-backup:latest

# Restore the `latest` backup.
docker run -it --rm -e "MODE=restore" -e "DATABASE_DSN=mysql://MyDbUser:[email protected]/my-database" -v "$(pwd)/backup:/backup" brainbitsgmbh/mysql-backup:latest

⁠Troubleshooting

⁠The script cannot connect to my database

Make sure that you enter a valid DSN that can also be reached from withing the docker container the script is running in. If your DSN includes localhost it will likely not work, because the container cannot reach the database on your local machine via localhost. In that case you can try host.docker.internal instead, as seen in the example above.

Tag summary

Content type

Image

Digest

sha256:60fb22c58…

Size

117.9 MB

Last updated

over 2 years ago

docker pull brainbitsgmbh/mysql-backup