mirror of
https://github.com/fradelg/docker-mysql-cron-backup.git
synced 2025-08-15 05:49:12 +00:00
Compare commits
No commits in common. "master" and "1.13.1" have entirely different histories.
7 changed files with 39 additions and 80 deletions
|
@ -1,33 +1,40 @@
|
||||||
|
|
||||||
name: build docker image
|
name: build docker image
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
push:
|
push:
|
||||||
|
branches:
|
||||||
|
- "**"
|
||||||
tags:
|
tags:
|
||||||
- "**"
|
- "**"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
test:
|
||||||
runs-on: ubuntu-22.04
|
runs-on: ubuntu-22.04
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout the code
|
- name: Checkout the code
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v3
|
||||||
|
- name: Test Bash scripts
|
||||||
|
run: sudo apt-get -qq update && sudo apt-get install -y devscripts shellcheck && make test
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
needs: test
|
||||||
|
steps:
|
||||||
|
- name: Checkout the code
|
||||||
|
uses: actions/checkout@v3
|
||||||
# https://github.com/docker/setup-qemu-action
|
# https://github.com/docker/setup-qemu-action
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@v2
|
||||||
# https://github.com/docker/setup-buildx-action
|
# https://github.com/docker/setup-buildx-action
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v2
|
||||||
- name: Get latest release version number
|
- name: Get latest release version number
|
||||||
id: docker-tag
|
id: docker-tag
|
||||||
uses: yuya-takeyama/docker-tag-from-github-ref-action@v1
|
uses: yuya-takeyama/docker-tag-from-github-ref-action@v1
|
||||||
- name: Login to Docker Hub
|
- name: Login to Docker Hub
|
||||||
uses: docker/login-action@v3
|
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login --username "${{ secrets.DOCKER_USERNAME }}" --password-stdin
|
||||||
with:
|
|
||||||
username: fradelg
|
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
|
||||||
- name: Build multiarch image
|
- name: Build multiarch image
|
||||||
run: |
|
run: |
|
||||||
docker buildx build --push \
|
docker buildx build --push \
|
||||||
--tag fradelg/mysql-cron-backup:${{ steps.docker-tag.outputs.tag }} \
|
--tag fradelg/mysql-cron-backup:${{ steps.docker-tag.outputs.tag }} \
|
||||||
--platform linux/amd64,linux/arm/v7,linux/arm64 .
|
--platform linux/amd64,linux/arm/v7,linux/arm64 .
|
26
.github/workflows/test.yml
vendored
26
.github/workflows/test.yml
vendored
|
@ -1,26 +0,0 @@
|
||||||
name: build docker image
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- "**"
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-22.04
|
|
||||||
steps:
|
|
||||||
- name: Checkout the code
|
|
||||||
uses: actions/checkout@v5
|
|
||||||
- name: Test Bash scripts
|
|
||||||
run: sudo apt-get -qq update && sudo apt-get install -y devscripts shellcheck && make test
|
|
||||||
- name: Test image
|
|
||||||
env:
|
|
||||||
VOLUME_PATH: /tmp/mariadb
|
|
||||||
DATABASE_NAME: foo
|
|
||||||
MARIADB_ROOT_PASSWORD: abcd
|
|
||||||
run: |
|
|
||||||
docker compose up -d mariadb
|
|
||||||
docker compose run backup /backup.sh
|
|
||||||
docker compose run backup /restore.sh /backup/latest.foo.sql.gz
|
|
||||||
docker compose stop
|
|
29
Dockerfile
29
Dockerfile
|
@ -1,28 +1,27 @@
|
||||||
FROM golang:1.20.4-alpine3.18 AS binary
|
FROM golang:1.15.8-alpine3.12 AS binary
|
||||||
RUN apk -U add openssl git
|
RUN apk -U add openssl git
|
||||||
|
|
||||||
ARG DOCKERIZE_VERSION=v0.7.0
|
ARG DOCKERIZE_VERSION=v0.6.1
|
||||||
WORKDIR /go/src/github.com/jwilder
|
WORKDIR /go/src/github.com/jwilder
|
||||||
RUN git clone https://github.com/jwilder/dockerize.git && \
|
RUN git clone https://github.com/jwilder/dockerize.git && \
|
||||||
cd dockerize && \
|
cd dockerize && \
|
||||||
git checkout ${DOCKERIZE_VERSION}
|
git checkout ${DOCKERIZE_VERSION}
|
||||||
|
|
||||||
WORKDIR /go/src/github.com/jwilder/dockerize
|
WORKDIR /go/src/github.com/jwilder/dockerize
|
||||||
ENV GO111MODULE=on
|
RUN go get github.com/robfig/glock
|
||||||
RUN go mod tidy
|
RUN glock sync -n < GLOCKFILE
|
||||||
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o /go/bin/dockerize .
|
RUN go install
|
||||||
|
|
||||||
FROM alpine:3.20.3
|
FROM alpine:3.18.3
|
||||||
LABEL maintainer "Fco. Javier Delgado del Hoyo <frandelhoyo@gmail.com>"
|
LABEL maintainer "Fco. Javier Delgado del Hoyo <frandelhoyo@gmail.com>"
|
||||||
|
|
||||||
RUN apk add --update \
|
RUN apk add --update \
|
||||||
tzdata \
|
tzdata \
|
||||||
bash \
|
bash \
|
||||||
gzip \
|
mysql-client \
|
||||||
openssl \
|
gzip \
|
||||||
mysql-client=~10.11 \
|
openssl \
|
||||||
mariadb-connector-c \
|
mariadb-connector-c && \
|
||||||
fdupes && \
|
|
||||||
rm -rf /var/cache/apk/*
|
rm -rf /var/cache/apk/*
|
||||||
|
|
||||||
COPY --from=binary /go/bin/dockerize /usr/local/bin
|
COPY --from=binary /go/bin/dockerize /usr/local/bin
|
||||||
|
@ -43,6 +42,6 @@ RUN mkdir /backup && \
|
||||||
VOLUME ["/backup"]
|
VOLUME ["/backup"]
|
||||||
|
|
||||||
HEALTHCHECK --interval=2s --retries=1800 \
|
HEALTHCHECK --interval=2s --retries=1800 \
|
||||||
CMD stat /HEALTHY.status || exit 1
|
CMD stat /HEALTHY.status || exit 1
|
||||||
|
|
||||||
CMD dockerize -wait tcp://${MYSQL_HOST}:${MYSQL_PORT} -timeout ${TIMEOUT} /run.sh
|
ENTRYPOINT dockerize -wait tcp://${MYSQL_HOST}:${MYSQL_PORT} -timeout ${TIMEOUT} /run.sh
|
|
@ -42,7 +42,6 @@ Container is **Healthy** after the database init phase, that is after `INIT_BACK
|
||||||
- `GZIP_LEVEL`: Specify the level of gzip compression from 1 (quickest, least compressed) to 9 (slowest, most compressed), default is 6.
|
- `GZIP_LEVEL`: Specify the level of gzip compression from 1 (quickest, least compressed) to 9 (slowest, most compressed), default is 6.
|
||||||
- `USE_PLAIN_SQL`: If set, back up and restore plain SQL files without gzip.
|
- `USE_PLAIN_SQL`: If set, back up and restore plain SQL files without gzip.
|
||||||
- `TZ`: Specify TIMEZONE in Container. E.g. "Europe/Berlin". Default is UTC.
|
- `TZ`: Specify TIMEZONE in Container. E.g. "Europe/Berlin". Default is UTC.
|
||||||
- `REMOVE_DUPLICATES`: Use [fdupes](https://github.com/adrianlopezroche/fdupes) to remove duplicate database dumps
|
|
||||||
|
|
||||||
If you want to make this image the perfect companion of your MySQL container, use [docker-compose](https://docs.docker.com/compose/). You can add more services that will be able to connect to the MySQL image using the name `my_mariadb`, note that you only expose the port `3306` internally to the servers and not to the host:
|
If you want to make this image the perfect companion of your MySQL container, use [docker-compose](https://docs.docker.com/compose/). You can add more services that will be able to connect to the MySQL image using the name `my_mariadb`, note that you only expose the port `3306` internally to the servers and not to the host:
|
||||||
|
|
||||||
|
@ -81,8 +80,6 @@ services:
|
||||||
- CRON_TIME=0 3 * * *
|
- CRON_TIME=0 3 * * *
|
||||||
# Make it small
|
# Make it small
|
||||||
- GZIP_LEVEL=9
|
- GZIP_LEVEL=9
|
||||||
# As of MySQL 8.0.21 this is needed
|
|
||||||
- MYSQLDUMP_OPTS=--no-tablespaces
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
|
|
14
backup.sh
14
backup.sh
|
@ -29,18 +29,13 @@ do
|
||||||
echo "==> Dumping database: $db"
|
echo "==> Dumping database: $db"
|
||||||
FILENAME=/backup/$DATE.$db.sql
|
FILENAME=/backup/$DATE.$db.sql
|
||||||
LATEST=/backup/latest.$db.sql
|
LATEST=/backup/latest.$db.sql
|
||||||
BASIC_OPTS="--single-transaction"
|
if mysqldump --single-transaction $MYSQLDUMP_OPTS -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" $MYSQL_SSL_OPTS "$db" > "$FILENAME"
|
||||||
if [ -n "$REMOVE_DUPLICATES" ]
|
|
||||||
then
|
|
||||||
BASIC_OPTS="$BASIC_OPTS --skip-dump-date"
|
|
||||||
fi
|
|
||||||
if mysqldump $BASIC_OPTS $MYSQLDUMP_OPTS -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" $MYSQL_SSL_OPTS "$db" > "$FILENAME"
|
|
||||||
then
|
then
|
||||||
EXT=
|
EXT=
|
||||||
if [ -z "${USE_PLAIN_SQL}" ]
|
if [ -z "${USE_PLAIN_SQL}" ]
|
||||||
then
|
then
|
||||||
echo "==> Compressing $db with LEVEL $GZIP_LEVEL"
|
echo "==> Compressing $db with LEVEL $GZIP_LEVEL"
|
||||||
gzip "-$GZIP_LEVEL" -n -f "$FILENAME"
|
gzip "-$GZIP_LEVEL" -f "$FILENAME"
|
||||||
EXT=.gz
|
EXT=.gz
|
||||||
FILENAME=$FILENAME$EXT
|
FILENAME=$FILENAME$EXT
|
||||||
LATEST=$LATEST$EXT
|
LATEST=$LATEST$EXT
|
||||||
|
@ -49,11 +44,6 @@ do
|
||||||
echo "==> Creating symlink to latest backup: $BASENAME"
|
echo "==> Creating symlink to latest backup: $BASENAME"
|
||||||
rm "$LATEST" 2> /dev/null
|
rm "$LATEST" 2> /dev/null
|
||||||
cd /backup || exit && ln -s "$BASENAME" "$(basename "$LATEST")"
|
cd /backup || exit && ln -s "$BASENAME" "$(basename "$LATEST")"
|
||||||
if [ -n "$REMOVE_DUPLICATES" ]
|
|
||||||
then
|
|
||||||
echo "==> Removing duplicate database dumps"
|
|
||||||
fdupes -idN /backup/
|
|
||||||
fi
|
|
||||||
if [ -n "$MAX_BACKUPS" ]
|
if [ -n "$MAX_BACKUPS" ]
|
||||||
then
|
then
|
||||||
# Execute the delete script, delete older backup or other custom delete script
|
# Execute the delete script, delete older backup or other custom delete script
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
|
version: "2"
|
||||||
services:
|
services:
|
||||||
mariadb:
|
mariadb:
|
||||||
image: mariadb:10.11
|
image: mariadb:10
|
||||||
container_name: my_mariadb
|
container_name: my_mariadb
|
||||||
security_opt:
|
|
||||||
- seccomp:unconfined
|
|
||||||
expose:
|
expose:
|
||||||
- 3306
|
- 3306
|
||||||
volumes:
|
volumes:
|
||||||
|
@ -12,19 +11,13 @@ services:
|
||||||
environment:
|
environment:
|
||||||
- MYSQL_DATABASE=${DATABASE_NAME}
|
- MYSQL_DATABASE=${DATABASE_NAME}
|
||||||
- MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
|
- MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
|
||||||
- MYSQL_ALLOW_EMPTY_ROOT_PASSWORD=yes
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
healthcheck:
|
|
||||||
test: [ "CMD", "healthcheck.sh", "--su-mysql", "--connect" ]
|
|
||||||
timeout: 5s
|
|
||||||
retries: 10
|
|
||||||
|
|
||||||
backup:
|
backup:
|
||||||
build: .
|
build: .
|
||||||
image: fradelg/mysql-cron-backup
|
image: fradelg/mysql-cron-backup
|
||||||
depends_on:
|
depends_on:
|
||||||
mariadb:
|
- mariadb
|
||||||
condition: service_healthy
|
|
||||||
volumes:
|
volumes:
|
||||||
- ${VOLUME_PATH}/backup:/backup
|
- ${VOLUME_PATH}/backup:/backup
|
||||||
environment:
|
environment:
|
||||||
|
@ -35,6 +28,6 @@ services:
|
||||||
- INIT_BACKUP=1
|
- INIT_BACKUP=1
|
||||||
- CRON_TIME=0 0 * * *
|
- CRON_TIME=0 0 * * *
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
data:
|
data:
|
|
@ -13,7 +13,6 @@
|
||||||
if [ "$#" -ne 1 ]
|
if [ "$#" -ne 1 ]
|
||||||
then
|
then
|
||||||
echo "You must pass the path of the backup file to restore"
|
echo "You must pass the path of the backup file to restore"
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue