mirror of
https://github.com/fradelg/docker-mysql-cron-backup.git
synced 2025-06-28 07:36:40 +00:00
Compare commits
112 commits
Author | SHA1 | Date | |
---|---|---|---|
|
4bacd0dc7c | ||
|
1d3c439b65 | ||
|
8a15e59c1f | ||
|
cff595e634 | ||
|
dc253cd1ea | ||
|
d5d98e5a7c | ||
|
823343381d | ||
|
aebb29ab64 | ||
|
8a403d725b | ||
|
dac06953c3 | ||
|
a715b4946c | ||
|
07213e8135 | ||
|
353430ff83 | ||
|
9f3f7bcdfa | ||
|
23cf857487 | ||
|
da8028abe5 | ||
|
9e325e3ef7 | ||
|
dbece920d6 | ||
|
7fbc2fb79e | ||
|
6fb2c053e7 | ||
|
efe5430629 | ||
|
0ac27b1753 | ||
|
d20b5ea8a9 | ||
|
f01e97f92d | ||
|
9c84748595 | ||
|
66ac1288be | ||
|
a84b82077f | ||
|
f1a3a6b01f | ||
|
f8addec08f | ||
|
057495229e | ||
|
549f30ef6f | ||
|
e52061d4fa | ||
|
4be928f6d0 | ||
|
c217d30c39 | ||
|
554424bbc5 | ||
|
6b19e33340 | ||
|
895f17a082 | ||
|
70b282dfec | ||
|
bcf82ae4e6 | ||
|
7dd3aa517b | ||
|
b7c09ddc1c | ||
|
69f8608858 | ||
|
d772d22dd4 | ||
|
b9d28576ea | ||
|
e1a26c194c | ||
|
494c1ed20a | ||
|
982600303d | ||
|
41c9f729ea | ||
|
9edf0dedaf | ||
|
19c7f7395d | ||
|
25729c7bc3 | ||
|
96ca5bb74a | ||
|
37334bf3c3 | ||
|
d6217926f2 | ||
|
1f5f594e14 | ||
|
47977b7846 | ||
|
b0ae6e075d | ||
|
90ad75a24e | ||
|
8d2e012667 | ||
|
8eb4379305 | ||
|
9bd732897e | ||
|
81f5e3dc69 | ||
|
57a90b2880 | ||
|
a2675a3c06 | ||
|
6ef15a2672 | ||
|
374b26531b | ||
|
0dba471e17 | ||
|
6b6656c540 | ||
|
ee27961c60 | ||
|
f8307fd40f | ||
|
74aa80e2b3 | ||
|
39d39915e4 | ||
|
400bb8c95e | ||
|
218bc5778c | ||
|
3011a8770a | ||
|
e975214636 | ||
|
f02d4d92e1 | ||
|
3d32ab2a13 | ||
|
2de64e836a | ||
|
d1f887f83c | ||
|
7bc94755f2 | ||
|
4abf8c5d9d | ||
|
37a8400103 | ||
|
d07cf4ffb7 | ||
|
9b542daf42 | ||
|
bde69eaf1a | ||
|
e4a6fb2593 | ||
|
6b19217285 | ||
|
d172e583b0 | ||
|
2b43049ff9 | ||
|
35ff2141ea | ||
|
6f512df392 | ||
|
aca23ddb93 | ||
|
795dab6dc4 | ||
|
e8396b9fda | ||
|
0bf1a8d7ab | ||
|
040b3e4e77 | ||
|
0721c4cf8f | ||
|
7e9b1fdbcf | ||
|
4d4d4dbd6c | ||
|
f1ba971fa0 | ||
|
4b64ac852f | ||
|
546533416e | ||
|
1ed7bc3e12 | ||
|
f30f90ad61 | ||
|
f73f393f19 | ||
|
ca54b7769e | ||
|
6368925724 | ||
|
331adf57b0 | ||
|
d16bf5443b | ||
|
eb42488f00 | ||
|
db6014ab1a |
12 changed files with 266 additions and 89 deletions
33
.github/workflows/build.yml
vendored
Normal file
33
.github/workflows/build.yml
vendored
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
|
||||||
|
name: build docker image
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "**"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
steps:
|
||||||
|
- name: Checkout the code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
# https://github.com/docker/setup-qemu-action
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v3
|
||||||
|
# https://github.com/docker/setup-buildx-action
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
- name: Get latest release version number
|
||||||
|
id: docker-tag
|
||||||
|
uses: yuya-takeyama/docker-tag-from-github-ref-action@v1
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
username: fradelg
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
- name: Build multiarch image
|
||||||
|
run: |
|
||||||
|
docker buildx build --push \
|
||||||
|
--tag fradelg/mysql-cron-backup:${{ steps.docker-tag.outputs.tag }} \
|
||||||
|
--platform linux/amd64,linux/arm/v7,linux/arm64 .
|
40
.github/workflows/image.yml
vendored
40
.github/workflows/image.yml
vendored
|
@ -1,40 +0,0 @@
|
||||||
name: build docker image
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- "**"
|
|
||||||
tags:
|
|
||||||
- "**"
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-20.04
|
|
||||||
steps:
|
|
||||||
- name: Checkout the code
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: Test Bash scripts
|
|
||||||
run: sudo apt-get -qq update && sudo apt-get install -y devscripts shellcheck && make test
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-20.04
|
|
||||||
needs: test
|
|
||||||
steps:
|
|
||||||
- name: Checkout the code
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
# https://github.com/docker/setup-qemu-action
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v1
|
|
||||||
# https://github.com/docker/setup-buildx-action
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v1
|
|
||||||
- name: Get latest release version number
|
|
||||||
id: docker-tag
|
|
||||||
uses: yuya-takeyama/docker-tag-from-github-ref-action@v1
|
|
||||||
- name: Login to Docker Hub
|
|
||||||
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
|
|
||||||
- name: Build multiarch image
|
|
||||||
run: |
|
|
||||||
docker buildx build --push \
|
|
||||||
--tag fradelg/mysql-cron-backup:${{ steps.docker-tag.outputs.tag }} \
|
|
||||||
--platform linux/amd64,linux/arm/v7,linux/arm64 .
|
|
26
.github/workflows/test.yml
vendored
Normal file
26
.github/workflows/test.yml
vendored
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
name: build docker image
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- "**"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
steps:
|
||||||
|
- name: Checkout the code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- 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
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
data
|
32
Dockerfile
32
Dockerfile
|
@ -1,27 +1,28 @@
|
||||||
FROM golang:1.15.8-alpine3.12 AS binary
|
FROM golang:1.20.4-alpine3.18 AS binary
|
||||||
RUN apk -U add openssl git
|
RUN apk -U add openssl git
|
||||||
|
|
||||||
ARG DOCKERIZE_VERSION=v0.6.1
|
ARG DOCKERIZE_VERSION=v0.7.0
|
||||||
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
|
||||||
RUN go get github.com/robfig/glock
|
ENV GO111MODULE=on
|
||||||
RUN glock sync -n < GLOCKFILE
|
RUN go mod tidy
|
||||||
RUN go install
|
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o /go/bin/dockerize .
|
||||||
|
|
||||||
FROM alpine:3.15.0
|
FROM alpine:3.20.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 \
|
||||||
mysql-client \
|
gzip \
|
||||||
gzip \
|
openssl \
|
||||||
openssl \
|
mysql-client=~10.11 \
|
||||||
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
|
||||||
|
@ -32,13 +33,16 @@ ENV CRON_TIME="0 3 * * sun" \
|
||||||
TIMEOUT="10s" \
|
TIMEOUT="10s" \
|
||||||
MYSQLDUMP_OPTS="--quick"
|
MYSQLDUMP_OPTS="--quick"
|
||||||
|
|
||||||
COPY ["run.sh", "backup.sh", "restore.sh", "/"]
|
COPY ["run.sh", "backup.sh", "restore.sh", "/delete.sh", "/"]
|
||||||
RUN mkdir /backup && \
|
RUN mkdir /backup && \
|
||||||
chmod 777 /backup && \
|
chmod 777 /backup && \
|
||||||
chmod 755 /run.sh /backup.sh /restore.sh && \
|
chmod 755 /run.sh /backup.sh /restore.sh /delete.sh && \
|
||||||
touch /mysql_backup.log && \
|
touch /mysql_backup.log && \
|
||||||
chmod 666 /mysql_backup.log
|
chmod 666 /mysql_backup.log
|
||||||
|
|
||||||
VOLUME ["/backup"]
|
VOLUME ["/backup"]
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=2s --retries=1800 \
|
||||||
|
CMD stat /HEALTHY.status || exit 1
|
||||||
|
|
||||||
CMD dockerize -wait tcp://${MYSQL_HOST}:${MYSQL_PORT} -timeout ${TIMEOUT} /run.sh
|
CMD dockerize -wait tcp://${MYSQL_HOST}:${MYSQL_PORT} -timeout ${TIMEOUT} /run.sh
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -6,7 +6,7 @@ test:
|
||||||
# Checking for syntax errors
|
# Checking for syntax errors
|
||||||
set -e; for SCRIPT in *.sh; \
|
set -e; for SCRIPT in *.sh; \
|
||||||
do \
|
do \
|
||||||
sh -n $$SCRIPT; \
|
bash -n $$SCRIPT; \
|
||||||
done
|
done
|
||||||
|
|
||||||
# Checking for bashisms (currently not failing, but only listing)
|
# Checking for bashisms (currently not failing, but only listing)
|
||||||
|
|
108
README.md
108
README.md
|
@ -13,23 +13,36 @@ docker container run -d \
|
||||||
fradelg/mysql-cron-backup
|
fradelg/mysql-cron-backup
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Healthcheck
|
||||||
|
|
||||||
|
|
||||||
|
Healthcheck is provided as a basic init control.
|
||||||
|
Container is **Healthy** after the database init phase, that is after `INIT_BACKUP` or `INIT_RESTORE_LATEST` happends without check if there is an error, **Starting** otherwise. Not other checks are actually provided.
|
||||||
|
|
||||||
## Variables
|
## Variables
|
||||||
|
|
||||||
|
|
||||||
- `MYSQL_HOST`: The host/ip of your mysql database.
|
- `MYSQL_HOST`: The host/ip of your mysql database.
|
||||||
|
- `MYSQL_HOST_FILE`: The file in container where to find the host of your mysql database (cf. docker secrets). You should use either MYSQL_HOST_FILE or MYSQL_HOST (see examples below).
|
||||||
- `MYSQL_PORT`: The port number of your mysql database.
|
- `MYSQL_PORT`: The port number of your mysql database.
|
||||||
- `MYSQL_USER`: The username of your mysql database.
|
- `MYSQL_USER`: The username of your mysql database.
|
||||||
|
- `MYSQL_USER_FILE`: The file in container where to find the user of your mysql database (cf. docker secrets). You should use either MYSQL_USER_FILE or MYSQL_USER (see examples below).
|
||||||
- `MYSQL_PASS`: The password of your mysql database.
|
- `MYSQL_PASS`: The password of your mysql database.
|
||||||
- `MYSQL_PASS_FILE`: The file in container where to find the password of your mysql database (cf. docker secrets). You should use either MYSQL_PASS_FILE or MYSQL_PASS (see examples below).
|
- `MYSQL_PASS_FILE`: The file in container where to find the password of your mysql database (cf. docker secrets). You should use either MYSQL_PASS_FILE or MYSQL_PASS (see examples below).
|
||||||
- `MYSQL_DATABASE`: The database name to dump. Default: `--all-databases`.
|
- `MYSQL_DATABASE`: The database name to dump. Default: `--all-databases`.
|
||||||
|
- `MYSQL_DATABASE_FILE`: The file in container where to find the database name(s) in your mysql database (cf. docker secrets). In that file, there can be several database names: one per line. You should use either MYSQL_DATABASE or MYSQL_DATABASE_FILE (see examples below).
|
||||||
- `MYSQLDUMP_OPTS`: Command line arguments to pass to mysqldump (see [mysqldump documentation](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html)).
|
- `MYSQLDUMP_OPTS`: Command line arguments to pass to mysqldump (see [mysqldump documentation](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html)).
|
||||||
|
- `MYSQL_SSL_OPTS`: Command line arguments to use [SSL](https://dev.mysql.com/doc/refman/5.6/en/using-encrypted-connections.html).
|
||||||
- `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.
|
- `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.
|
- `MAX_BACKUPS`: The number of backups to keep. When reaching the limit, the old backup will be discarded. No limit by default.
|
||||||
- `INIT_BACKUP`: If set, create a backup when the container starts.
|
- `INIT_BACKUP`: If set, create a backup when the container starts.
|
||||||
- `INIT_RESTORE_LATEST`: If set, restores latest backup.
|
- `INIT_RESTORE_LATEST`: If set, restores latest backup.
|
||||||
|
- `EXIT_BACKUP`: If set, create a backup when the container stops.
|
||||||
- `TIMEOUT`: Wait a given number of seconds for the database to be ready and make the first backup, `10s` by default. After that time, the initial attempt for backup gives up and only the Cron job will try to make a backup.
|
- `TIMEOUT`: Wait a given number of seconds for the database to be ready and make the first backup, `10s` by default. After that time, the initial attempt for backup gives up and only the Cron job will try to make a backup.
|
||||||
- `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:
|
||||||
|
|
||||||
|
@ -68,6 +81,8 @@ 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:
|
||||||
|
@ -78,17 +93,23 @@ volumes:
|
||||||
|
|
||||||
The database root password passed to docker container by using [docker secrets](https://docs.docker.com/engine/swarm/).
|
The database root password passed to docker container by using [docker secrets](https://docs.docker.com/engine/swarm/).
|
||||||
|
|
||||||
In example below, docker is in classic 'docker engine mode' (iow. not swarm mode) and secret source is a local file on host filesystem.
|
In example below, docker is in classic 'docker engine mode' (iow. not swarm mode) and secret sources are local files on host filesystem.
|
||||||
|
|
||||||
Alternatively, secret can be stored in docker secrets engine (iow. not in host filesystem).
|
Alternatively, secrets can be stored in docker secrets engine (iow. not in host filesystem).
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
version: "3.7"
|
version: "3.7"
|
||||||
|
|
||||||
secrets:
|
secrets:
|
||||||
|
# Place your secret file somewhere on your host filesystem, with your password inside
|
||||||
mysql_root_password:
|
mysql_root_password:
|
||||||
# Place your secret file somewhere on your host filesystem, with your password inside
|
|
||||||
file: ./secrets/mysql_root_password
|
file: ./secrets/mysql_root_password
|
||||||
|
mysql_user:
|
||||||
|
file: ./secrets/mysql_user
|
||||||
|
mysql_password:
|
||||||
|
file: ./secrets/mysql_password
|
||||||
|
mysql_database:
|
||||||
|
file: ./secrets/mysql_database
|
||||||
|
|
||||||
services:
|
services:
|
||||||
mariadb:
|
mariadb:
|
||||||
|
@ -100,10 +121,15 @@ services:
|
||||||
- data:/var/lib/mysql
|
- data:/var/lib/mysql
|
||||||
- ${VOLUME_PATH}/backup:/backup
|
- ${VOLUME_PATH}/backup:/backup
|
||||||
environment:
|
environment:
|
||||||
- MYSQL_DATABASE=${DATABASE_NAME}
|
|
||||||
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql_root_password
|
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql_root_password
|
||||||
|
- MYSQL_USER_FILE=/run/secrets/mysql_user
|
||||||
|
- MYSQL_PASSWORD_FILE=/run/secrets/mysql_password
|
||||||
|
- MYSQL_DATABASE_FILE=/run/secrets/mysql_database
|
||||||
secrets:
|
secrets:
|
||||||
- mysql_root_password
|
- mysql_root_password
|
||||||
|
- mysql_user
|
||||||
|
- mysql_password
|
||||||
|
- mysql_database
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
backup:
|
backup:
|
||||||
|
@ -115,13 +141,18 @@ services:
|
||||||
- ${VOLUME_PATH}/backup:/backup
|
- ${VOLUME_PATH}/backup:/backup
|
||||||
environment:
|
environment:
|
||||||
- MYSQL_HOST=my_mariadb
|
- MYSQL_HOST=my_mariadb
|
||||||
- MYSQL_USER=root
|
# Alternatively to MYSQL_USER_FILE, we can use MYSQL_USER=root to use root user instead
|
||||||
- MYSQL_PASS_FILE=/run/secrets/mysql_root_password
|
- MYSQL_USER_FILE=/run/secrets/mysql_user
|
||||||
|
# Alternatively, we can use /run/secrets/mysql_root_password when using root user
|
||||||
|
- MYSQL_PASS_FILE=/run/secrets/mysql_password
|
||||||
|
- MYSQL_DATABASE_FILE=/run/secrets/mysql_database
|
||||||
- MAX_BACKUPS=10
|
- MAX_BACKUPS=10
|
||||||
- INIT_BACKUP=1
|
- INIT_BACKUP=1
|
||||||
- CRON_TIME=0 0 * * *
|
- CRON_TIME=0 0 * * *
|
||||||
secrets:
|
secrets:
|
||||||
- mysql_root_password
|
- mysql_user
|
||||||
|
- mysql_password
|
||||||
|
- mysql_database
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
|
@ -163,4 +194,65 @@ mysql-cron-backup:
|
||||||
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.
|
if no database name is specified, `restore.sh` will try to find the database name from the backup file.
|
||||||
|
|
||||||
|
### Automatic backup and restore on container starts and stops
|
||||||
|
|
||||||
|
Set `INIT_RESTORE_LATEST` to automatic restore the last backup on startup.
|
||||||
|
Set `EXIT_BACKUP` to automatic create a last backup on shutdown.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
mysql-cron-backup:
|
||||||
|
image: fradelg/mysql-cron-backup
|
||||||
|
depends_on:
|
||||||
|
- mariadb
|
||||||
|
volumes:
|
||||||
|
- ${VOLUME_PATH}/backup:/backup
|
||||||
|
environment:
|
||||||
|
- MYSQL_HOST=my_mariadb
|
||||||
|
- MYSQL_USER=${MYSQL_USER}
|
||||||
|
- MYSQL_PASS=${MYSQL_PASSWORD}
|
||||||
|
- MAX_BACKUPS=15
|
||||||
|
- INIT_RESTORE_LATEST=1
|
||||||
|
- EXIT_BACKUP=1
|
||||||
|
# Every day at 03:00
|
||||||
|
- CRON_TIME=0 3 * * *
|
||||||
|
# Make it small
|
||||||
|
- GZIP_LEVEL=9
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
data:
|
||||||
|
```
|
||||||
|
|
||||||
|
Docker database image could expose a directory you could add files as init sql script.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
mysql:
|
||||||
|
image: mysql
|
||||||
|
expose:
|
||||||
|
- 3306
|
||||||
|
volumes:
|
||||||
|
- data:/var/lib/mysql
|
||||||
|
# If there is not scheme, restore using the init script (if exists)
|
||||||
|
- ./init-script.sql:/docker-entrypoint-initdb.d/database.sql.gz
|
||||||
|
environment:
|
||||||
|
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
|
||||||
|
- MYSQL_DATABASE=${DATABASE_NAME}
|
||||||
|
restart: unless-stopped
|
||||||
|
```
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
mariadb:
|
||||||
|
image: mariadb
|
||||||
|
expose:
|
||||||
|
- 3306
|
||||||
|
volumes:
|
||||||
|
- data:/var/lib/mysql
|
||||||
|
# If there is not scheme, restore using the init script (if exists)
|
||||||
|
- ./init-script.sql:/docker-entrypoint-initdb.d/database.sql.gz
|
||||||
|
environment:
|
||||||
|
- MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
|
||||||
|
- MYSQL_DATABASE=${DATABASE_NAME}
|
||||||
|
restart: unless-stopped
|
||||||
|
```
|
37
backup.sh
37
backup.sh
|
@ -1,15 +1,23 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Get hostname: try read from file, else get from env
|
||||||
|
[ -z "${MYSQL_HOST_FILE}" ] || { MYSQL_HOST=$(head -1 "${MYSQL_HOST_FILE}"); }
|
||||||
|
[ -z "${MYSQL_HOST}" ] && { echo "=> MYSQL_HOST cannot be empty" && exit 1; }
|
||||||
|
# Get username: try read from file, else get from env
|
||||||
|
[ -z "${MYSQL_USER_FILE}" ] || { MYSQL_USER=$(head -1 "${MYSQL_USER_FILE}"); }
|
||||||
[ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; }
|
[ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; }
|
||||||
# If provided, take password from file
|
# Get password: try read from file, else get from env, else get from MYSQL_PASSWORD env
|
||||||
[ -z "${MYSQL_PASS_FILE}" ] || { MYSQL_PASS=$(head -1 "${MYSQL_PASS_FILE}"); }
|
[ -z "${MYSQL_PASS_FILE}" ] || { MYSQL_PASS=$(head -1 "${MYSQL_PASS_FILE}"); }
|
||||||
# Alternatively, take it from env var
|
|
||||||
[ -z "${MYSQL_PASS:=$MYSQL_PASSWORD}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
|
[ -z "${MYSQL_PASS:=$MYSQL_PASSWORD}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
|
||||||
|
# Get database name(s): try read from file, else get from env
|
||||||
|
# Note: when from file, there can be one database name per line in that file
|
||||||
|
[ -z "${MYSQL_DATABASE_FILE}" ] || { MYSQL_DATABASE=$(cat "${MYSQL_DATABASE_FILE}"); }
|
||||||
|
# Get level from env, else use 6
|
||||||
[ -z "${GZIP_LEVEL}" ] && { GZIP_LEVEL=6; }
|
[ -z "${GZIP_LEVEL}" ] && { GZIP_LEVEL=6; }
|
||||||
|
|
||||||
DATE=$(date +%Y%m%d%H%M)
|
DATE=$(date +%Y%m%d%H%M)
|
||||||
echo "=> Backup started at $(date "+%Y-%m-%d %H:%M:%S")"
|
echo "=> Backup started at $(date "+%Y-%m-%d %H:%M:%S")"
|
||||||
DATABASES=${MYSQL_DATABASE:-${MYSQL_DB:-$(mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)}}
|
DATABASES=${MYSQL_DATABASE:-${MYSQL_DB:-$(mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" $MYSQL_SSL_OPTS -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)}}
|
||||||
for db in ${DATABASES}
|
for db in ${DATABASES}
|
||||||
do
|
do
|
||||||
if [[ "$db" != "information_schema" ]] \
|
if [[ "$db" != "information_schema" ]] \
|
||||||
|
@ -21,13 +29,18 @@ 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
|
||||||
if mysqldump --single-transaction "$MYSQLDUMP_OPTS" -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$db" > "$FILENAME"
|
BASIC_OPTS="--single-transaction"
|
||||||
|
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" -f "$FILENAME"
|
gzip "-$GZIP_LEVEL" -n -f "$FILENAME"
|
||||||
EXT=.gz
|
EXT=.gz
|
||||||
FILENAME=$FILENAME$EXT
|
FILENAME=$FILENAME$EXT
|
||||||
LATEST=$LATEST$EXT
|
LATEST=$LATEST$EXT
|
||||||
|
@ -36,15 +49,15 @@ 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
|
||||||
while [ "$(find /backup -maxdepth 1 -name "*.$db.sql$EXT" -type f | wc -l)" -gt "$MAX_BACKUPS" ]
|
# Execute the delete script, delete older backup or other custom delete script
|
||||||
do
|
/delete.sh "$db" $EXT
|
||||||
TARGET=$(find /backup -maxdepth 1 -name "*.$db.sql$EXT" -type f | sort | head -n 1)
|
|
||||||
echo "==> Max number of ($MAX_BACKUPS) backups reached. Deleting ${TARGET} ..."
|
|
||||||
rm -rf "${TARGET}"
|
|
||||||
echo "==> Backup ${TARGET} deleted"
|
|
||||||
done
|
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
rm -rf "$FILENAME"
|
rm -rf "$FILENAME"
|
||||||
|
|
14
delete.sh
Executable file
14
delete.sh
Executable file
|
@ -0,0 +1,14 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
db=$1
|
||||||
|
EXT=$2
|
||||||
|
|
||||||
|
# This file could be customized to create custom delete strategy
|
||||||
|
|
||||||
|
while [ "$(find /backup -maxdepth 1 -name "*.$db.sql$EXT" -type f | wc -l)" -gt "$MAX_BACKUPS" ]
|
||||||
|
do
|
||||||
|
TARGET=$(find /backup -maxdepth 1 -name "*.$db.sql$EXT" -type f | sort | head -n 1)
|
||||||
|
echo "==> Max number of ($MAX_BACKUPS) backups reached. Deleting ${TARGET} ..."
|
||||||
|
rm -rf "${TARGET}"
|
||||||
|
echo "==> Backup ${TARGET} deleted"
|
||||||
|
done
|
|
@ -1,8 +1,9 @@
|
||||||
version: "2"
|
|
||||||
services:
|
services:
|
||||||
mariadb:
|
mariadb:
|
||||||
image: mariadb:10
|
image: mariadb:10.11
|
||||||
container_name: my_mariadb
|
container_name: my_mariadb
|
||||||
|
security_opt:
|
||||||
|
- seccomp:unconfined
|
||||||
expose:
|
expose:
|
||||||
- 3306
|
- 3306
|
||||||
volumes:
|
volumes:
|
||||||
|
@ -11,13 +12,19 @@ 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:
|
||||||
|
@ -28,6 +35,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:
|
||||||
|
|
15
restore.sh
15
restore.sh
|
@ -1,20 +1,25 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Get hostname: try read from file, else get from env
|
||||||
|
[ -z "${MYSQL_HOST_FILE}" ] || { MYSQL_HOST=$(head -1 "${MYSQL_HOST_FILE}"); }
|
||||||
|
[ -z "${MYSQL_HOST}" ] && { echo "=> MYSQL_HOST cannot be empty" && exit 1; }
|
||||||
|
# Get username: try read from file, else get from env
|
||||||
|
[ -z "${MYSQL_USER_FILE}" ] || { MYSQL_USER=$(head -1 "${MYSQL_USER_FILE}"); }
|
||||||
[ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; }
|
[ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; }
|
||||||
# If provided, take password from file
|
# Get password: try read from file, else get from env, else get from MYSQL_PASSWORD env
|
||||||
[ -z "${MYSQL_PASS_FILE}" ] || { MYSQL_PASS=$(head -1 "${MYSQL_PASS_FILE}"); }
|
[ -z "${MYSQL_PASS_FILE}" ] || { MYSQL_PASS=$(head -1 "${MYSQL_PASS_FILE}"); }
|
||||||
# Alternatively, take it from env var
|
[ -z "${MYSQL_PASS:=$MYSQL_PASSWORD}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
|
||||||
[ -z "${MYSQL_PASS}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
|
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
if [ -z "${USE_PLAIN_SQL}" ]
|
if [ -z "${USE_PLAIN_SQL}" ]
|
||||||
then
|
then
|
||||||
SQL=$(gunzip -c "$1")
|
SQL=$(gunzip -c "$1")
|
||||||
else
|
else
|
||||||
SQL=$(cat "$1")
|
SQL=$(cat "$1")
|
||||||
|
@ -30,7 +35,7 @@ fi
|
||||||
|
|
||||||
echo "=> Restore database $DB_NAME from $1"
|
echo "=> Restore database $DB_NAME from $1"
|
||||||
|
|
||||||
if echo "$SQL" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$DB_NAME"
|
if echo "$SQL" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" $MYSQL_SSL_OPTS "$DB_NAME"
|
||||||
then
|
then
|
||||||
echo "=> Restore succeeded"
|
echo "=> Restore succeeded"
|
||||||
else
|
else
|
||||||
|
|
28
run.sh
28
run.sh
|
@ -1,7 +1,7 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
tail -F /mysql_backup.log &
|
tail -F /mysql_backup.log &
|
||||||
|
|
||||||
if [ "${INIT_BACKUP}" -gt "0" ]; then
|
if [ "${INIT_BACKUP:-0}" -gt "0" ]; then
|
||||||
echo "=> Create a backup on the startup"
|
echo "=> Create a backup on the startup"
|
||||||
/backup.sh
|
/backup.sh
|
||||||
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
|
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
|
||||||
|
@ -11,10 +11,32 @@ elif [ -n "${INIT_RESTORE_LATEST}" ]; then
|
||||||
echo "waiting database container..."
|
echo "waiting database container..."
|
||||||
sleep 1
|
sleep 1
|
||||||
done
|
done
|
||||||
find /backup -maxdepth 1 -name '*.sql.gz' | tail -1 | xargs /restore.sh
|
# Needed to exclude the 'latest.<database>.sql.gz' file, consider only filenames starting with number
|
||||||
|
# Only data-tagged backups, eg. '202212250457.database.sql.gz', must be trapped by the regex
|
||||||
|
find /backup -maxdepth 1 -name '[0-9]*.*.sql.gz' | sort | tail -1 | xargs /restore.sh
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
function final_backup {
|
||||||
|
echo "=> Captured trap for final backup"
|
||||||
|
echo "=> Requested last backup at $(date "+%Y-%m-%d %H:%M:%S")"
|
||||||
|
exec /backup.sh
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ -n "${EXIT_BACKUP}" ]; then
|
||||||
|
echo "=> Listening on container shutdown gracefully to make last backup before close"
|
||||||
|
trap final_backup SIGHUP SIGINT SIGTERM
|
||||||
|
fi
|
||||||
|
|
||||||
|
touch /HEALTHY.status
|
||||||
|
|
||||||
echo "${CRON_TIME} /backup.sh >> /mysql_backup.log 2>&1" > /tmp/crontab.conf
|
echo "${CRON_TIME} /backup.sh >> /mysql_backup.log 2>&1" > /tmp/crontab.conf
|
||||||
crontab /tmp/crontab.conf
|
crontab /tmp/crontab.conf
|
||||||
echo "=> Running cron task manager in foreground"
|
echo "=> Running cron task manager in foreground"
|
||||||
exec crond -f -l 8 -L /mysql_backup.log
|
crond -f -l 8 -L /mysql_backup.log &
|
||||||
|
|
||||||
|
echo "Listening on crond, and wait..."
|
||||||
|
|
||||||
|
tail -f /dev/null & wait $!
|
||||||
|
|
||||||
|
echo "Script is shutted down."
|
Loading…
Add table
Add a link
Reference in a new issue