FORK of https://github.com/deitch/mysql-backup to enable backup on real mysql server
482
FORK of https://github.com/deitch/mysql-backup to enable backup on real mysql server
mysql-backup is a simple way to do MySQL database backups and restores when the database is running in a container.
It has the following features:
To run a backup, launch mysql-backup image as a container with the correct parameters. Everything is controlled by environment variables passed to the container.
For example:
docker run -d --restart=always -e DB_DUMP_FREQ=60 -e DB_DUMP_BEGIN=2330 -e DB_DUMP_TARGET=/db -v /local/file/path:/db teknologist/mysql-backup
The above will run a dump every 60 minutes, beginning at the next 2330 local time, from the database accessible in the container my-db-container.
The following are the environment variables for a backup:
You should consider the use of --env-file= to keep your secrets out of your shell history
DB_SERVER: MySQL Host to backupDB_USER: username for the databaseDB_PASS: password for the databaseDB_NAMES: names of databases to dump; defaults to all databases in the database serverDB_DUMP_FREQ: How often to do a dump, in minutes. Defaults to 1440 minutes, or once per day.DB_DUMP_BEGIN: What time to do the first dump. Defaults to immediate. Must be in one of two formats:
2330 or 0415+0 (immediate), +10 (in 10 minutes), or +90 in an hour and a halfDB_DUMP_DEBUG: If set to true, print copious shell script messages to the container log. Otherwise only basic messages are printed.DB_DUMP_TARGET: Where to put the dump file, should be a directory. Supports three formats:
DB_DUMP_TARGET starts with a / character, will dump to a local path, which should be volume-mounted.DB_DUMP_TARGET is a URL of the format smb://hostname/share/path/ then it will connect via SMB.DB_DUMP_TARGET is a URL of the format s3://bucketname/path then it will connect via awscli.AWS_ACCESS_KEY_ID: AWS Key IDAWS_SECRET_ACCESS_KEY: AWS Secret Access KeyAWS_DEFAULT_REGION: Region in which the bucket residesSMB_USER: SMB username. May also be specified in DB_DUMP_TARGET with an smb:// url. If both specified, this variable overrides the value in the URL.SMB_PASS: SMB password. May also be specified in DB_DUMP_TARGET with an smb:// url. If both specified, this variable overrides the value in the URL.In order to perform the actual dump, mysql-backup needs to connect to the database container. You should define DB_SERVER to point to the IP/hostname of the actual mysql server
docker run -d --restart=always -e DB_SERVER=192.168.1.22 -e DB_USER=user123 -e DB_PASS=pass123 -e DB_DUMP_FREQ=60 -e DB_DUMP_BEGIN=2330 -e DB_DUMP_TARGET=/db -v /local/file/path:/db teknologist/mysql-backup
The dump target is where you want the backup files to be saved. The backup file always is a gzipped file the following format:
db_backup_YYYYMMDDHHmm.sql.gz
Where:
The time used is UTC time at the moment the dump begins.
The dump target is the location where the dump should be placed, defaults to /backup in the container. Of course, having the backup in the container does not help very much, so we very strongly recommend you volume mount it outside somewhere. See the above example.
If you use a URL like smb://host/share/path, you can have it save to an SMB server. If you need loging credentials, use smb://user:pass@host/share/path.
Note that for smb, if the username includes a domain, e.g. your user is mydom\myuser, then you should use the samb convention of replacing the '' with a ';'. In other words smb://mydom;myuser:pass@host/share/path
If you use a URL like s3://bucket/path, you can have it save to an S3 bucket.
Note that for s3, you'll need to specify your AWS credentials and default AWS region via AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION
Any executable script with .sh extension in /scripts.d/post-backup/ directory in the container will be executed after the backup dump process has finished, but before uploading the backup file to its ultimate target. This is useful if you need to include some files along with the database dump, for example, to backup a WordPress install.
To use them you need to add a host volume that points to the post-backup scripts in the docker host. Start the container like this:
docker run -d --restart=always -e DB_SERVER=192.168.1.22 -e DB_USER=user123 -e DB_PASS=pass123 -e DB_DUMP_FREQ=60 -e DB_DUMP_BEGIN=2330 -e DB_DUMP_TARGET=/db -v /local/file/path:/db -v /path/to/post-backup/scripts:/scripts.d/post-backup teknologist/mysql-backup
Or, if you prefer compose:
version: '2.1'
services:
backup:
image: teknologist/mysql-backup
restart: always
volumes:
- /local/file/path:/db
- /path/to/post-backup/scripts:/scripts.d/post-backup
env:
- DB_SERVER=192.168.1.22
- DB_DUMP_TARGET=/db
- DB_USER=user123
- DB_PASS=pass123
- DB_DUMP_FREQ=60
- DB_DUMP_BEGIN=2330
mysql_db:
image: mysql
....
The scripts are executed in the entrypoint script, which means it has access to all exported environment variables. The following are available, but we are happy to export more as required (just open an issue or better yet, a pull request):
DUMPFILE: full path in the container to the output fileNOW: date of the backup, as included in DUMPFILE and given by date -u +"%Y%m%d%H%M%S"In addition, all of the environment variables set for the container will be available to the script.
For example, the following script will rename the backup file after the dump is done:
#!/bin/bash
# Rename backup file.
new_name=${now}.gz
echo "Renaming backup file from ${TARGET} to ${new_name}"
if [ -e ${TMPDIR}/${TARGET} ];
then
mv ${TMPDIR}/${TARGET} ${TMPDIR}/${new_name}
TARGET=${new_name}
else
echo "ERROR: Backup file ${TMPDIR}/${TARGET} does not exist!"
fi
You can think of this as a sort of basic plugin system. Look at the source of the entrypoint script for other variables that can be used.
If you wish to run a restore to an existing database, you can use mysql-backup to do a restore.
You need only the following environment variables:
You should consider the use of --env-file= to keep your secrets out of your shell history
DB_SERVER: MySQL Host to restore toDB_USER: username for the databaseDB_PASS: password for the databaseDB_RESTORE_TARGET: path to the actual restore file, which should be a gzip of an sql dump file. The target can be an absolute path, which should be volume mounted, an smb or S3 URL, similar to the target.DB_DUMP_DEBUG: if true, dump copious outputs to the container logs while restoring.AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION will need to be defined.Examples:
docker run -e DB_SERVER=192.168.1.22 -e DB_USER=user123 -e DB_PASS=pass123 -e DB_RESTORE_TARGET=/backup/db_backup_201509271627.sql.gz -v /local/path:/backup teknologist/mysql-backupdocker run -e DB_SERVER=192.168.1.22 -e DB_USER=user123 -e DB_PASS=pass123 -e DB_RESTORE_TARGET=smb://smbserver/share1/backup/db_backup_201509271627.sql.gz teknologist/mysql-backupdocker run -e DB_SERVER=192.168.1.22 -e AWS_ACCESS_KEY_ID=awskeyid -e AWS_SECRET_ACCESS_KEY=secret -e AWS_DEFAULT_REGION=eu-central-1 -e DB_USER=user123 -e DB_PASS=pass123 -e DB_RESTORE_TARGET=s3://bucket/path/db_backup_201509271627.sql.gz teknologist/mysql-backupThis gituhub repo is the source for the mysql-backup image. The actual image is stored on the docker hub at teknologist/mysql-backup, and is triggered with each commit to the source by automated build via Webhooks.
There are 2 builds: 1 for version based on the git tag, and another for the particular version number.
Released under the MIT License. Copyright Avi Deitcher https://github.com/deitch
Thanks to the kind contributions and support of TraderTools.
Content type
Image
Digest
Size
46 MB
Last updated
about 9 years ago
docker pull teknologist/mysql-backup