Compare commits

...

78 commits

Author SHA1 Message Date
Fco. Javier Delgado del Hoyo
4bacd0dc7c
Merge pull request from fradelg/bugfix/skip-dump-date
Fixed database name on dumps
2024-12-09 12:35:48 +01:00
Fco. Javier Delgado del Hoyo
1d3c439b65 split test and build jobs 2024-12-09 12:32:31 +01:00
Fco. Javier Delgado del Hoyo
8a15e59c1f skip only timestamps when needed 2024-12-09 12:23:08 +01:00
Fco. Javier Delgado del Hoyo
cff595e634 disable skip comments only when needed 2024-12-08 13:56:43 +01:00
Fco. Javier Delgado del Hoyo
dc253cd1ea stick to client 10.11 version 2024-12-08 12:52:35 +01:00
Fco. Javier Delgado del Hoyo
d5d98e5a7c
Merge pull request from fradelg/dependabot-docker-master-alpine-3.21.0
Bump alpine from 3.20.3 to 3.21.0
2024-12-08 11:46:50 +01:00
Tobias Janke
823343381d
[Feature] Remove duplicate database dumps to save storage space ()
* Replace gzip with deterministic bzip2 compression

* Exclude comments from SQL dump

* Add option to remove duplicates using fdupes

* Revert change to bzip2 as gzip can be deterministic using no-name parameter
2024-12-08 11:12:39 +01:00
dependabot[bot]
aebb29ab64
Bump alpine from 3.20.3 to 3.21.0
Bumps alpine from 3.20.3 to 3.21.0.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-12-06 04:39:42 +00:00
Fco. Javier Delgado del Hoyo
8a403d725b
Merge pull request from fradelg/fix/build
Fixed issues in CI testing
2024-09-10 19:54:30 +02:00
Fco. Javier Delgado del Hoyo
dac06953c3
do not mask docker username 2024-09-10 19:52:19 +02:00
Fco. Javier Delgado del Hoyo
a715b4946c
add health check 2024-09-10 19:47:30 +02:00
Fco. Javier Delgado del Hoyo
07213e8135
use service name for dns 2024-09-10 19:37:59 +02:00
Fco. Javier Delgado del Hoyo
353430ff83
move to docker v2 2024-09-10 19:25:10 +02:00
Fco. Javier Delgado del Hoyo
9f3f7bcdfa
Merge pull request from fradelg/dependabot-docker-master-alpine-3.20.3
Bump alpine from 3.20.2 to 3.20.3
2024-09-10 19:15:40 +02:00
dependabot[bot]
23cf857487
Bump alpine from 3.20.2 to 3.20.3
Bumps alpine from 3.20.2 to 3.20.3.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-09-09 04:30:04 +00:00
Fco. Javier Delgado del Hoyo
da8028abe5
Merge pull request from fradelg/dependabot-docker-master-alpine-3.20.2
Bump alpine from 3.20.1 to 3.20.2
2024-07-26 18:11:00 +02:00
dependabot[bot]
9e325e3ef7
Bump alpine from 3.20.1 to 3.20.2
Bumps alpine from 3.20.1 to 3.20.2.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-07-23 04:19:30 +00:00
Fco. Javier Delgado del Hoyo
dbece920d6
Merge pull request from fradelg/dependabot-docker-master-alpine-3.20.1
Bump alpine from 3.20.0 to 3.20.1
2024-06-21 18:13:06 +02:00
dependabot[bot]
7fbc2fb79e
Bump alpine from 3.20.0 to 3.20.1
Bumps alpine from 3.20.0 to 3.20.1.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-06-21 04:44:44 +00:00
Fco. Javier Delgado del Hoyo
6fb2c053e7
Merge pull request from fradelg/dependabot-docker-master-alpine-3.20.0
Bump alpine from 3.19.1 to 3.20.0
2024-05-23 19:55:57 +02:00
dependabot[bot]
efe5430629
Bump alpine from 3.19.1 to 3.20.0
Bumps alpine from 3.19.1 to 3.20.0.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-05-23 04:30:24 +00:00
Fco. Javier Delgado del Hoyo
0ac27b1753
Merge pull request from fradelg/dependabot-docker-master-alpine-3.19.1
Bump alpine from 3.19.0 to 3.19.1
2024-01-29 19:56:13 +01:00
dependabot[bot]
d20b5ea8a9
Bump alpine from 3.19.0 to 3.19.1
Bumps alpine from 3.19.0 to 3.19.1.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-01-29 04:23:47 +00:00
Fco. Javier Delgado del Hoyo
f01e97f92d
Merge pull request from fradelg/dependabot-docker-master-alpine-3.19.0
Bump alpine from 3.18.5 to 3.19.0
2023-12-09 19:12:29 +01:00
dependabot[bot]
9c84748595
Bump alpine from 3.18.5 to 3.19.0
Bumps alpine from 3.18.5 to 3.19.0.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-12-08 04:20:17 +00:00
Fco. Javier Delgado del Hoyo
66ac1288be
Merge pull request from fradelg/dependabot-docker-master-alpine-3.18.5
Bump alpine from 3.18.4 to 3.18.5
2023-12-01 19:10:18 +01:00
dependabot[bot]
a84b82077f
Bump alpine from 3.18.4 to 3.18.5
Bumps alpine from 3.18.4 to 3.18.5.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-12-01 04:59:07 +00:00
Fco. Javier Delgado del Hoyo
f1a3a6b01f
Merge pull request from nikgli/master
Fixed access denied the PROCESS privilege(s) is required for MySQL > 8.0.21
2023-11-02 10:49:55 +01:00
nikgli
f8addec08f Fixed access denied (at least one of) the PROCESS privilege(s) is required error 2023-11-02 14:28:58 +08:00
Fco. Javier Delgado del Hoyo
057495229e
Merge pull request from fradelg/dependabot-docker-master-alpine-3.18.4
Bump alpine from 3.18.3 to 3.18.4
2023-09-30 11:34:19 +02:00
dependabot[bot]
549f30ef6f
Bump alpine from 3.18.3 to 3.18.4
Bumps alpine from 3.18.3 to 3.18.4.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-29 04:49:58 +00:00
Fco. Javier Delgado del Hoyo
e52061d4fa
Test docker image before pushing to docker hub ()
* update dockerize docker instructions

* use go with alpine

* login to docker hub using action too

* test docker image before building

* extract command from the entrypoint

* change volume path

* user docker compose run and cmd instead entrypoint
2023-09-17 11:01:33 +02:00
Fco. Javier Delgado del Hoyo
4be928f6d0
Upgrade dockerize to 0.7.0 ()
* update dockerize docker instructions

* login to docker hub using action
2023-09-15 19:56:06 +02:00
Fco. Javier Delgado del Hoyo
c217d30c39
Merge pull request from fradelg/dependabot-github_actions-docker-setup-qemu-action-3
Bump docker/setup-qemu-action from 2 to 3
2023-09-15 19:11:01 +02:00
dependabot[bot]
554424bbc5
Bump docker/setup-qemu-action from 2 to 3
Bumps [docker/setup-qemu-action](https://github.com/docker/setup-qemu-action) from 2 to 3.
- [Release notes](https://github.com/docker/setup-qemu-action/releases)
- [Commits](https://github.com/docker/setup-qemu-action/compare/v2...v3)

---
updated-dependencies:
- dependency-name: docker/setup-qemu-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-15 17:07:34 +00:00
Fco. Javier Delgado del Hoyo
6b19e33340
Merge pull request from fradelg/dependabot-github_actions-docker-setup-buildx-action-3
Bump docker/setup-buildx-action from 2 to 3
2023-09-15 19:07:01 +02:00
dependabot[bot]
895f17a082
Bump docker/setup-buildx-action from 2 to 3
Bumps [docker/setup-buildx-action](https://github.com/docker/setup-buildx-action) from 2 to 3.
- [Release notes](https://github.com/docker/setup-buildx-action/releases)
- [Commits](https://github.com/docker/setup-buildx-action/compare/v2...v3)

---
updated-dependencies:
- dependency-name: docker/setup-buildx-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-13 05:03:02 +00:00
Fco. Javier Delgado del Hoyo
70b282dfec
Merge pull request from fradelg/dependabot-github_actions-actions-checkout-4
Bump actions/checkout from 3 to 4
2023-09-05 19:30:26 +02:00
dependabot[bot]
bcf82ae4e6
Bump actions/checkout from 3 to 4
Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v3...v4)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-05 04:59:56 +00:00
Fco. Javier Delgado del Hoyo
7dd3aa517b
Merge pull request from fradelg/dependabot-docker-master-alpine-3.18.3
Bump alpine from 3.18.2 to 3.18.3
2023-08-12 10:42:07 +02:00
dependabot[bot]
b7c09ddc1c
Bump alpine from 3.18.2 to 3.18.3
Bumps alpine from 3.18.2 to 3.18.3.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-08-08 04:55:47 +00:00
Fco. Javier Delgado del Hoyo
69f8608858
Merge pull request from fradelg/dependabot-docker-master-alpine-3.18.2
Bump alpine from 3.18.0 to 3.18.2
2023-06-16 20:15:29 +02:00
dependabot[bot]
d772d22dd4
Bump alpine from 3.18.0 to 3.18.2
Bumps alpine from 3.18.0 to 3.18.2.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-06-16 05:00:59 +00:00
Fco. Javier Delgado del Hoyo
b9d28576ea
Merge pull request from fradelg/dependabot-docker-master-alpine-3.18.0
Bump alpine from 3.17.3 to 3.18.0
2023-05-11 20:59:39 +02:00
dependabot[bot]
e1a26c194c
Bump alpine from 3.17.3 to 3.18.0
Bumps alpine from 3.17.3 to 3.18.0.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-05-10 05:01:16 +00:00
Fco. Javier Delgado del Hoyo
494c1ed20a
Merge pull request from fradelg/dependabot-docker-master-alpine-3.17.3
Bump alpine from 3.17.2 to 3.17.3
2023-04-01 11:08:30 +02:00
dependabot[bot]
982600303d
Bump alpine from 3.17.2 to 3.17.3
Bumps alpine from 3.17.2 to 3.17.3.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-03-30 05:01:41 +00:00
Fco. Javier Delgado del Hoyo
41c9f729ea
Merge pull request from fradelg/dependabot-docker-master-alpine-3.17.2
Bump alpine from 3.17.1 to 3.17.2
2023-02-13 19:31:03 +01:00
dependabot[bot]
9edf0dedaf
Bump alpine from 3.17.1 to 3.17.2
Bumps alpine from 3.17.1 to 3.17.2.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-02-13 05:06:41 +00:00
Fco. Javier Delgado del Hoyo
19c7f7395d
Merge pull request from g-nardiello/feature-healthcheck
[Feature] Add basic healthcheck
2023-01-20 19:00:55 +01:00
Giuseppe Nardiello
25729c7bc3 Corrected HEALTHY.status typo 2023-01-20 12:04:42 +01:00
Giuseppe Nardiello
96ca5bb74a Corrected HEALTHY.status typo 2023-01-20 12:04:19 +01:00
Fco. Javier Delgado del Hoyo
37334bf3c3
Merge pull request from fradelg/bugfix/ci
Bugfix/ci
2023-01-19 20:22:16 +01:00
Fco. Javier Delgado del Hoyo
d6217926f2 fix minor issues 2023-01-19 20:14:48 +01:00
Fco. Javier Delgado del Hoyo
1f5f594e14 add parenthesis to function syntax 2023-01-19 20:01:21 +01:00
Fco. Javier Delgado del Hoyo
47977b7846 ignore data dir 2023-01-19 19:57:20 +01:00
Fco. Javier Delgado del Hoyo
b0ae6e075d update to ubuntu 22.04 2023-01-19 19:57:03 +01:00
Fco. Javier Delgado del Hoyo
90ad75a24e
Merge pull request from g-nardiello/feature-exit-backup
[Feature] Make backup on container shutdown
2023-01-19 19:32:42 +01:00
Fco. Javier Delgado del Hoyo
8d2e012667
Merge pull request from g-nardiello/patch-restore-latest-sort
[Bugfix] RESTORE_LATEST script sorted by backup date
2023-01-19 19:29:10 +01:00
Giuseppe Nardiello
8eb4379305 Changed find regex to explain date-format 2023-01-17 22:57:09 +01:00
Giuseppe Nardiello
9bd732897e Better wait for crond 2023-01-17 22:23:34 +01:00
Giuseppe Nardiello
81f5e3dc69 Corrected *Healthy* typo 2023-01-10 23:55:38 +01:00
Fco. Javier Delgado del Hoyo
57a90b2880
Merge pull request from fradelg/dependabot-docker-master-alpine-3.17.1
Bump alpine from 3.17.0 to 3.17.1
2023-01-10 19:50:32 +01:00
dependabot[bot]
a2675a3c06
Bump alpine from 3.17.0 to 3.17.1
Bumps alpine from 3.17.0 to 3.17.1.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-01-10 04:06:46 +00:00
Fco. Javier Delgado del Hoyo
6ef15a2672
Merge pull request from g-nardiello/feature-custom-delete
[Feature] Get out delete script, to allow custom logics
2023-01-09 20:50:08 +01:00
Fco. Javier Delgado del Hoyo
374b26531b
Merge pull request from g-nardiello/patch-syntax-if
[Bugfix] run.sh - Missing default value for INIT_BACKUP
2023-01-09 20:24:17 +01:00
g.nardiello
0dba471e17 Merge branch 'feature-custom-delete' of https://github.com/g-nardiello/docker-mysql-cron-backup into feature-custom-delete 2023-01-06 09:38:01 +01:00
g.nardiello
6b6656c540 Delete strategy moved to new file, as could custom 2023-01-06 09:35:11 +01:00
g.nardiello
ee27961c60 Delete strategy moved to new file, as could custom 2023-01-06 09:29:31 +01:00
g.nardiello
f8307fd40f Improved docs for Healthcheck 2023-01-06 08:43:40 +01:00
g.nardiello
74aa80e2b3 Added EXIT_BACKUP feature with gracefully shutdown 2023-01-05 11:59:16 +01:00
g.nardiello
39d39915e4 Added file-based HEALTHCHECK 2023-01-05 11:55:04 +01:00
g.nardiello
400bb8c95e Added sort algorithm to RESTORE_LATEST script 2023-01-05 11:47:54 +01:00
g.nardiello
218bc5778c Fixed if on line 4 to accept default 0 value 2023-01-05 11:44:39 +01:00
Fco. Javier Delgado del Hoyo
3011a8770a
Merge pull request from fradelg/dependabot-docker-master-alpine-3.17.0
Bump alpine from 3.16.3 to 3.17.0
2022-11-24 21:58:35 +01:00
dependabot[bot]
e975214636
Bump alpine from 3.16.3 to 3.17.0
Bumps alpine from 3.16.3 to 3.17.0.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-11-23 04:08:05 +00:00
Fco. Javier Delgado del Hoyo
f02d4d92e1
Merge pull request from fradelg/dependabot-docker-master-alpine-3.16.3
Bump alpine from 3.16.2 to 3.16.3
2022-11-14 20:25:02 +01:00
dependabot[bot]
3d32ab2a13
Bump alpine from 3.16.2 to 3.16.3
Bumps alpine from 3.16.2 to 3.16.3.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-11-14 04:06:23 +00:00
12 changed files with 218 additions and 74 deletions

33
.github/workflows/build.yml vendored Normal file
View 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 .

View file

@ -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@v3
- 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@v3
# https://github.com/docker/setup-qemu-action
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- 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 --username "${{ 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
View 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
View file

@ -0,0 +1 @@
data

View file

@ -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
ARG DOCKERIZE_VERSION=v0.6.1
ARG DOCKERIZE_VERSION=v0.7.0
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
ENV GO111MODULE=on
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o /go/bin/dockerize .
FROM alpine:3.16.2
FROM alpine:3.20.3
LABEL maintainer "Fco. Javier Delgado del Hoyo <frandelhoyo@gmail.com>"
RUN apk add --update \
tzdata \
bash \
mysql-client \
gzip \
openssl \
mariadb-connector-c && \
tzdata \
bash \
gzip \
openssl \
mysql-client=~10.11 \
mariadb-connector-c \
fdupes && \
rm -rf /var/cache/apk/*
COPY --from=binary /go/bin/dockerize /usr/local/bin
@ -32,13 +33,16 @@ ENV CRON_TIME="0 3 * * sun" \
TIMEOUT="10s" \
MYSQLDUMP_OPTS="--quick"
COPY ["run.sh", "backup.sh", "restore.sh", "/"]
COPY ["run.sh", "backup.sh", "restore.sh", "/delete.sh", "/"]
RUN mkdir /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 && \
chmod 666 /mysql_backup.log
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

View file

@ -6,7 +6,7 @@ test:
# Checking for syntax errors
set -e; for SCRIPT in *.sh; \
do \
sh -n $$SCRIPT; \
bash -n $$SCRIPT; \
done
# Checking for bashisms (currently not failing, but only listing)

View file

@ -13,6 +13,12 @@ docker container run -d \
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
@ -31,10 +37,12 @@ docker container run -d \
- `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_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.
- `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.
- `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:
@ -73,6 +81,8 @@ services:
- CRON_TIME=0 3 * * *
# Make it small
- GZIP_LEVEL=9
# As of MySQL 8.0.21 this is needed
- MYSQLDUMP_OPTS=--no-tablespaces
restart: unless-stopped
volumes:
@ -184,4 +194,65 @@ mysql-cron-backup:
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
```

View file

@ -29,13 +29,18 @@ do
echo "==> Dumping database: $db"
FILENAME=/backup/$DATE.$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" $MYSQL_SSL_OPTS "$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
EXT=
if [ -z "${USE_PLAIN_SQL}" ]
then
echo "==> Compressing $db with LEVEL $GZIP_LEVEL"
gzip "-$GZIP_LEVEL" -f "$FILENAME"
gzip "-$GZIP_LEVEL" -n -f "$FILENAME"
EXT=.gz
FILENAME=$FILENAME$EXT
LATEST=$LATEST$EXT
@ -44,15 +49,15 @@ do
echo "==> Creating symlink to latest backup: $BASENAME"
rm "$LATEST" 2> /dev/null
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" ]
then
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
# Execute the delete script, delete older backup or other custom delete script
/delete.sh "$db" $EXT
fi
else
rm -rf "$FILENAME"

14
delete.sh Executable file
View 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

View file

@ -1,8 +1,9 @@
version: "2"
services:
mariadb:
image: mariadb:10
image: mariadb:10.11
container_name: my_mariadb
security_opt:
- seccomp:unconfined
expose:
- 3306
volumes:
@ -11,13 +12,19 @@ services:
environment:
- MYSQL_DATABASE=${DATABASE_NAME}
- MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
- MYSQL_ALLOW_EMPTY_ROOT_PASSWORD=yes
restart: unless-stopped
healthcheck:
test: [ "CMD", "healthcheck.sh", "--su-mysql", "--connect" ]
timeout: 5s
retries: 10
backup:
build: .
image: fradelg/mysql-cron-backup
depends_on:
- mariadb
mariadb:
condition: service_healthy
volumes:
- ${VOLUME_PATH}/backup:/backup
environment:
@ -28,6 +35,6 @@ services:
- INIT_BACKUP=1
- CRON_TIME=0 0 * * *
restart: unless-stopped
volumes:
data:
volumes:
data:

View file

@ -13,6 +13,7 @@
if [ "$#" -ne 1 ]
then
echo "You must pass the path of the backup file to restore"
exit 1
fi
set -o pipefail

28
run.sh
View file

@ -1,7 +1,7 @@
#!/bin/bash
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"
/backup.sh
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
@ -11,10 +11,32 @@ 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
# 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
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
crontab /tmp/crontab.conf
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."