mirror of
https://github.com/fradelg/docker-mysql-cron-backup.git
synced 2024-11-01 08:31:45 +01:00
Merge pull request #48 from fradelg/no-root-user
clean and sort Dockerfile
This commit is contained in:
commit
27ce6e2def
4 changed files with 26 additions and 15 deletions
20
Dockerfile
20
Dockerfile
|
@ -3,7 +3,9 @@ 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}
|
||||
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
|
||||
|
@ -13,7 +15,14 @@ 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/*
|
||||
RUN apk add --update \
|
||||
tzdata \
|
||||
bash \
|
||||
mysql-client \
|
||||
gzip \
|
||||
openssl \
|
||||
mariadb-connector-c && \
|
||||
rm -rf /var/cache/apk/*
|
||||
|
||||
COPY --from=binary /go/bin/dockerize /usr/local/bin
|
||||
|
||||
|
@ -23,7 +32,12 @@ ENV CRON_TIME="0 3 * * sun" \
|
|||
TIMEOUT="10s"
|
||||
|
||||
COPY ["run.sh", "backup.sh", "restore.sh", "/"]
|
||||
RUN mkdir /backup && chmod u+x /backup.sh /restore.sh
|
||||
RUN mkdir /backup && \
|
||||
chmod 777 /backup && \
|
||||
chmod 755 /run.sh /backup.sh /restore.sh && \
|
||||
touch /mysql_backup.log && \
|
||||
chmod 666 /mysql_backup.log
|
||||
|
||||
VOLUME ["/backup"]
|
||||
|
||||
CMD dockerize -wait tcp://${MYSQL_HOST}:${MYSQL_PORT} -timeout ${TIMEOUT} /run.sh
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
[![Build Status](https://travis-ci.org/fradelg/docker-mysql-cron-backup.svg?branch=master)](https://travis-ci.org/fradelg/docker-mysql-cron-backup)
|
||||
|
||||
# mysql-cron-backup
|
||||
|
||||
This docker image runs mysqldump to backup your databases periodically using cron task manager. Backups are placed in `/backup` so you can mount your backup docker volume in this path.
|
||||
Run mysqldump to backup your databases periodically using the cron task manager in the container. Your backups are saved in `/backup`. You can mount any directory of your host or a docker volumes in /backup. Othwerwise, a docker volume is created in the default location.
|
||||
|
||||
## Usage:
|
||||
|
||||
|
@ -103,7 +101,7 @@ mysql-cron-backup:
|
|||
### Restore using a docker command
|
||||
|
||||
```bash
|
||||
docker container exec <your_mysql_backup_container_name> /restore.sh backup/<your_sql_backup_gz_file>
|
||||
docker container exec <your_mysql_backup_container_name> /restore.sh /backup/<your_sql_backup_gz_file>
|
||||
```
|
||||
|
||||
if no database name is specified, `restore.sh` will try to find the database name from the backup file.
|
|
@ -14,12 +14,12 @@ do
|
|||
echo "==> Dumping database: $db"
|
||||
FILENAME=/backup/$DATE.$db.sql
|
||||
LATEST=/backup/latest.$db.sql.gz
|
||||
if mysqldump --single-transaction -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" $db $MYSQLDUMP_OPTS > "$FILENAME"
|
||||
if mysqldump --single-transaction -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$db" $MYSQLDUMP_OPTS > "$FILENAME"
|
||||
then
|
||||
gzip "-$GZIP_LEVEL" -f "$FILENAME"
|
||||
echo "==> Creating symlink to latest backup: $(basename "$FILENAME".gz)"
|
||||
rm "$LATEST" 2> /dev/null
|
||||
cd /backup || exit && ln -s $(basename "$FILENAME".gz) $(basename "$LATEST") && cd -
|
||||
cd /backup || exit && ln -s "$(basename "$FILENAME".gz)" "$(basename "$LATEST")"
|
||||
DB_COUNTER=$(( DB_COUNTER + 1 ))
|
||||
else
|
||||
rm -rf "$FILENAME"
|
||||
|
|
11
run.sh
11
run.sh
|
@ -1,5 +1,4 @@
|
|||
#!/bin/bash
|
||||
touch /mysql_backup.log
|
||||
tail -F /mysql_backup.log &
|
||||
|
||||
if [ "${INIT_BACKUP}" -gt "0" ]; then
|
||||
|
@ -12,10 +11,10 @@ elif [ -n "${INIT_RESTORE_LATEST}" ]; then
|
|||
echo "waiting database container..."
|
||||
sleep 1
|
||||
done
|
||||
find /backup -maxdepth 1 -name '*.sql.gz' | tail -1 | xargs /restore.sh
|
||||
find /backup -maxdepth 1 -name '*.sql.gz' | tail -1 | xargs /restore.sh
|
||||
fi
|
||||
|
||||
echo "${CRON_TIME} /backup.sh >> /mysql_backup.log 2>&1" > /crontab.conf
|
||||
crontab /crontab.conf
|
||||
echo "=> Running cron task manager"
|
||||
exec crond -f
|
||||
echo "${CRON_TIME} /backup.sh >> /mysql_backup.log 2>&1" > /tmp/crontab.conf
|
||||
crontab /tmp/crontab.conf
|
||||
echo "=> Running cron task manager in foreground"
|
||||
exec crond -f -l 8 -L /mysql_backup.log
|
||||
|
|
Loading…
Reference in a new issue