From 2405077482f6e7d8ff7cebd5cbf3a184b598b641 Mon Sep 17 00:00:00 2001 From: "Fco. Javier Delgado del Hoyo" Date: Sun, 7 Mar 2021 12:57:19 +0100 Subject: [PATCH] build dockerize and fix restore.sh db name for issue #38 --- Dockerfile | 20 +++++++++++++------- README.md | 21 ++++++++++++++++----- restore.sh | 3 ++- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4d7369c..1d0f4ae 100644 --- a/Dockerfile +++ b/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 " 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" \ diff --git a/README.md b/README.md index ed8cb86..cdecc77 100644 --- a/README.md +++ b/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 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} ``` diff --git a/restore.sh b/restore.sh index 20561f2..e2d8796 100755 --- a/restore.sh +++ b/restore.sh @@ -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