Skip to content

Commit

Permalink
Merge pull request databacker#14 from deitch/optional-individual-db
Browse files Browse the repository at this point in the history
Provide option for selecting which databases to restore
  • Loading branch information
deitch authored May 21, 2017
2 parents 57dc887 + 7c8e12b commit 1d88b28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To run a backup, launch `mysql-backup` image as a container with the correct par
For example:

````bash
docker run -d --restart=always -e DB_DUMP_FREQ=60 -e DB_DUMP_BEGIN=2330 -e DB_DUMP_TARGET=/db --link my-db-container:db -v /local/file/path:/db deitch/mysql-backup
docker run -d --restart=always -e DB_DUMP_FREQ=60 -e DB_DUMP_BEGIN=2330 -e DB_DUMP_TARGET=/db --link my-db-container:db -v /local/file/path:/db deitch/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`.
Expand All @@ -30,6 +30,7 @@ __You should consider the [use of `--env-file=`](https://docs.docker.com/engine/

* `DB_USER`: username for the database
* `DB_PASS`: password for the database
* `DB_NAMES`: names of databases to dump; defaults to all databases in the database server
* `DB_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:
* Absolute: HHMM, e.g. `2330` or `0415`
Expand All @@ -48,7 +49,7 @@ __You should consider the [use of `--env-file=`](https://docs.docker.com/engine/
In order to perform the actual dump, `mysql-backup` needs to connect to the database container. You should link to the container by passing the `--link` option to the `mysql-backup` container. The linked container should **always** be aliased to `db`. E.g.:

````bash
docker run -d --restart=always -e DB_USER=user123 -e DB_PASS=pass123 -e DB_DUMP_FREQ=60 -e DB_DUMP_BEGIN=2330 -e DB_DUMP_TARGET=/db --link my-db-container:db -v /local/file/path:/db deitch/mysql-backup
docker run -d --restart=always -e DB_USER=user123 -e DB_PASS=pass123 -e DB_DUMP_FREQ=60 -e DB_DUMP_BEGIN=2330 -e DB_DUMP_TARGET=/db --link my-db-container:db -v /local/file/path:/db deitch/mysql-backup
````

### Dump Target
Expand All @@ -72,7 +73,7 @@ If you use a URL like `smb://host/share/path`, you can have it save to an SMB se

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.
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`

Expand All @@ -87,7 +88,7 @@ __You should consider the [use of `--env-file=`](https://docs.docker.com/engine/
* `DB_PASS`: password for the database
* `DB_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.
* To use the S3 driver `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_DEFAULT_REGION` will need to be defined.
* To use the S3 driver `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_DEFAULT_REGION` will need to be defined.


Examples:
Expand All @@ -103,7 +104,7 @@ This gituhub repo is the source for the mysql-backup image. The actual image is
There are 2 builds: 1 for version based on the git tag, and another for the particular version number.

## License
Released under the MIT License.
Released under the MIT License.
Copyright Avi Deitcher https://github.com/deitch

Thanks to the kind contributions and support of [TraderTools](http://www.tradertools.com).
12 changes: 8 additions & 4 deletions entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function uri_parser() {
if [[ "${full:0:8}" == "file:///" ]]; then
full="${full/file:\/\/\//file://localhost/}"
fi

# top level parsing
pattern='^(([a-z0-9]{2,5})://)?((([^:\/]+)(:([^@\/]*))?@)?([^:\/?]+)(:([0-9]+))?)(\/[^?]*)?(\?[^#]*)?(#.*)?$'
[[ "$full" =~ $pattern ]] || return 1;
Expand All @@ -73,7 +73,7 @@ function uri_parser() {
uri[share]=${BASH_REMATCH[1]}
uri[sharepath]=${BASH_REMATCH[2]}
fi

# does the user have a domain?
if [[ -n ${uri[user]} && ${uri[user]} =~ ^([^\;]+)\;(.+)$ ]]; then
uri[userdomain]=${BASH_REMATCH[1]}
Expand Down Expand Up @@ -146,9 +146,14 @@ else
# what is the name of our target?
now=$(date -u +"%Y%m%d%H%M%S")
TARGET=db_backup_${now}.gz
if [[ -n "$DB_NAMES" ]]; then
DB_LIST="--databases $DB_NAMES"
else
DB_LIST="-A"
fi

# make the dump
mysqldump -A -h $DBSERVER -u$DBUSER -p$DBPASS | gzip > ${TMPDIR}/${TARGET}
mysqldump -h $DBSERVER -u$DBUSER -p$DBPASS $DB_LIST | gzip > ${TMPDIR}/${TARGET}

# what kind of target do we have? Plain filesystem? smb?
case "${uri[schema]}" in
Expand Down Expand Up @@ -183,4 +188,3 @@ else
sleep $(($DB_DUMP_FREQ*60))
done
fi

0 comments on commit 1d88b28

Please sign in to comment.