mirror of
https://github.com/fradelg/docker-mysql-cron-backup.git
synced 2024-11-01 08:31:45 +01:00
build dockerize and fix restore.sh db name for issue #38
This commit is contained in:
parent
150f905567
commit
2405077482
3 changed files with 31 additions and 13 deletions
20
Dockerfile
20
Dockerfile
|
@ -1,15 +1,21 @@
|
|||
FROM golang:1.15.8-alpine3.12 AS binary
|
||||
RUN apk -U add openssl git
|
||||
|
||||
ARG DOCKERIZE_VERSION=v0.6.1
|
||||
WORKDIR /go/src/github.com/jwilder
|
||||
RUN git clone https://github.com/jwilder/dockerize.git && cd dockerize && git checkout ${DOCKERIZE_VERSION}
|
||||
|
||||
WORKDIR /go/src/github.com/jwilder/dockerize
|
||||
RUN go get github.com/robfig/glock
|
||||
RUN glock sync -n < GLOCKFILE
|
||||
RUN go install
|
||||
|
||||
FROM alpine:3.12
|
||||
LABEL maintainer "Fco. Javier Delgado del Hoyo <frandelhoyo@gmail.com>"
|
||||
|
||||
RUN apk add --update tzdata bash mysql-client gzip openssl mariadb-connector-c && rm -rf /var/cache/apk/*
|
||||
|
||||
ARG OS=alpine-linux
|
||||
ARG ARCH=amd64
|
||||
ARG DOCKERIZE_VERSION=v0.6.1
|
||||
|
||||
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-$OS-$ARCH-$DOCKERIZE_VERSION.tar.gz \
|
||||
&& tar -C /usr/local/bin -xzvf dockerize-$OS-$ARCH-$DOCKERIZE_VERSION.tar.gz \
|
||||
&& rm dockerize-$OS-$ARCH-$DOCKERIZE_VERSION.tar.gz
|
||||
COPY --from=binary /go/bin/dockerize /usr/local/bin
|
||||
|
||||
ENV CRON_TIME="0 3 * * sun" \
|
||||
MYSQL_HOST="mysql" \
|
||||
|
|
21
README.md
21
README.md
|
@ -21,7 +21,7 @@ docker container run -d \
|
|||
- `MYSQL_PORT`: The port number of your mysql database.
|
||||
- `MYSQL_USER`: The username of your mysql database.
|
||||
- `MYSQL_PASS`: The password of your mysql database.
|
||||
- `MYSQL_DB`: The database name to dump. Default: `--all-databases`.
|
||||
- `MYSQL_DATABASE`: The database name to dump. Default: `--all-databases`.
|
||||
- `MYSQLDUMP_OPTS`: Command line arguments to pass to mysqldump. Example: `--single-transaction`.
|
||||
- `CRON_TIME`: The interval of cron job to run mysqldump. `0 3 * * sun` by default, which is every Sunday at 03:00. It uses UTC timezone.
|
||||
- `MAX_BACKUPS`: The number of backups to keep. When reaching the limit, the old backup will be discarded. No limit by default.
|
||||
|
@ -77,11 +77,22 @@ volumes:
|
|||
See the list of backups in your running docker container, just write in your favorite terminal:
|
||||
|
||||
```bash
|
||||
docker container exec backup ls /backup
|
||||
docker container exec <your_mysql_backuo_container_name> ls /backup
|
||||
```
|
||||
|
||||
To restore a database from a certain backup, simply run:
|
||||
To restore a database from a certain backup you have to specify the database name in the variable MYSQL_DATABASE:
|
||||
|
||||
```bash
|
||||
docker container exec backup /restore.sh /backup/201708060500.my_db.sql.gz
|
||||
```YAML
|
||||
mysql-cron-backup:
|
||||
image: fradelg/mysql-cron-backup
|
||||
command: "/restore.sh /backup/201708060500.${DATABASE_NAME}.sql.gz"
|
||||
depends_on:
|
||||
- mariadb
|
||||
volumes:
|
||||
- ${VOLUME_PATH}/backup:/backup
|
||||
environment:
|
||||
- MYSQL_HOST=my_mariadb
|
||||
- MYSQL_USER=root
|
||||
- MYSQL_PASS=${MARIADB_ROOT_PASSWORD}
|
||||
- MYSQL_DATABASE=${DATABASE_NAME}
|
||||
```
|
||||
|
|
|
@ -9,7 +9,8 @@ fi
|
|||
|
||||
echo "=> Restore database from $1"
|
||||
set -o pipefail
|
||||
if gunzip --stdout "$1" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS"
|
||||
DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}}
|
||||
if gunzip --stdout "$1" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$DB_NAME"
|
||||
then
|
||||
echo "=> Restore succeeded"
|
||||
else
|
||||
|
|
Loading…
Reference in a new issue