Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 6c0ac43

Browse files
committed
Initial import
0 parents  commit 6c0ac43

File tree

5 files changed

+365
-0
lines changed

5 files changed

+365
-0
lines changed

.gitlab-ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
variables:
2+
DOCKER_DRIVER: overlay2
3+
4+
stages:
5+
- build
6+
7+
.tags-template: &tags-definition
8+
tags:
9+
- ix-ai
10+
11+
.build-template: &build-definition
12+
image:
13+
name: gcr.io/kaniko-project/executor:debug
14+
entrypoint: [""]
15+
stage: build
16+
script:
17+
- echo "{\"auths\":{\"${CI_REGISTRY}\":{\"username\":\"${CI_REGISTRY_USER}\",\"password\":\"${CI_REGISTRY_PASSWORD}\"}}}" > /kaniko/.docker/config.json
18+
- /kaniko/executor --context ${CI_PROJECT_DIR} --dockerfile ${CI_PROJECT_DIR}/Dockerfile --destination ${IMAGE_TAG}
19+
20+
build:test:
21+
except:
22+
- master
23+
- latest
24+
before_script:
25+
- export IMAGE_TAG="${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}"
26+
<<: *tags-definition
27+
<<: *build-definition
28+
29+
build:release:
30+
only:
31+
- master
32+
before_script:
33+
- export IMAGE_TAG="${CI_REGISTRY_IMAGE}:latest"
34+
<<: *tags-definition
35+
<<: *build-definition

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM debian:buster
2+
LABEL MAINTAINER="docker@ix.ai"
3+
ENV DEBIAN_FRONTEND=noninteractive TERM=linux
4+
5+
RUN groupadd -g 666 mybackup && \
6+
useradd -u 666 -g 666 -d /backup -c "MariaDB Backup User" mybackup && \
7+
apt-get update && \
8+
apt-get -y dist-upgrade && \
9+
apt-get install -y mydumper && \
10+
apt-get -y --purge autoremove && \
11+
apt-get clean && \
12+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
13+
find /var/log -type f | while read f; do echo -ne '' > $f; done;
14+
15+
COPY init.sh /init.sh
16+
RUN chmod 750 /init.sh
17+
18+
VOLUME ["/backup"]
19+
WORKDIR /backup
20+
21+
CMD ["/init.sh"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Namshi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
About
2+
=====
3+
4+
The mysql-backup Docker image will provide you a container to backup and restore a [MySQL](https://hub.docker.com/_/mysql/) or [MariaDB](https://hub.docker.com/_/mariadb/) database container.
5+
6+
The backup is made with [mydumper](http://centminmod.com/mydumper.html), a fast MySQL backup utility.
7+
8+
Usage
9+
=====
10+
11+
To backup a [MySQL](https://hub.docker.com/_/mysql/) or [MariaDB](https://hub.docker.com/_/mariadb/) container you simply have to run a container from this Docker image and link a MySQL or MariaDB container to it.
12+
13+
The container will automatically detect the linked database container and tries to backup the database based on the environment variables of the database container:
14+
15+
* `<CONTAINER>_ENV_MYSQL_DATABASE`
16+
* `<CONTAINER>_ENV_MYSQL_ROOT_PASSWORD`
17+
18+
Please note the backup will be written to `/backup` by default, so you might want to mount that directory from your host.
19+
20+
Example Docker CLI client
21+
-------------------------
22+
23+
To __create a backup__ from a MySQL container via `docker` CLI client:
24+
25+
```bash
26+
docker run --name my-backup --link my-mysql -v /var/mysql_backups:/backup registry.gitlab.com/ix.ai/mariadb-backup
27+
```
28+
29+
The container will stop automatically as soon as the backup has finished.
30+
To create more backups in the future simply start your container again:
31+
32+
```bash
33+
docker start my-backup
34+
```
35+
36+
To __restore a backup__ into a MySQL container via `docker` CLI client:
37+
38+
```bash
39+
docker run --name my-restore --link my-mysql -v /var/mysql_backups:/backup registry.gitlab.com/ix.ai/mariadb-backup
40+
```
41+
42+
Example Docker Compose
43+
----------------------
44+
45+
Here's an example of a [Docker Compose](https://docs.docker.com/compose/) file, e.g. `docker-compose.yml`:
46+
47+
```yaml
48+
backup:
49+
image: registry.gitlab.com/ix.ai/mariadb-backup
50+
volumes:
51+
- ./data/backup:/backup
52+
links:
53+
- my-mysql
54+
restart: "no"
55+
```
56+
57+
Configuration
58+
=============
59+
60+
Mode
61+
----
62+
63+
By default the container backups the database.
64+
However, you can change the mode of the container by setting the following environment variable:
65+
66+
* `MODE`: Sets the mode of the backup container while [`BACKUP`|`RESTORE`]
67+
68+
Base directory
69+
--------------
70+
71+
By default the base directory `/backup` is used.
72+
However, you can overwrite that by setting the following environment variable:
73+
74+
* `BASE_DIR`: Path of the base directory (aka working directory)
75+
76+
Restory directory
77+
-----------------
78+
79+
By default the container will automatically restore the latest backup found in `BASE_DIR`.
80+
However, you can manually set the name of a backup directory underneath `BASE_DIR`:
81+
82+
* `RESTORE_DIR`: Name of a backup directory to restore
83+
84+
_This option is only required when the container runs in in `RESTORE` mode._
85+
86+
UID and GID
87+
-----------
88+
89+
By default the backup will be written with UID and GID `666`.
90+
However, you can overwrite that by setting the following environment variables:
91+
92+
* `BACKUP_UID`: UID of the backup
93+
* `BACKUP_GID`: GID of the backup
94+
95+
umask
96+
-----
97+
98+
By default a `umask` of `0022` will be used.
99+
However, you can overwrite that by setting the following environment variable:
100+
101+
* `UMASK`: Umask which should be used to write the backup files
102+
103+
mydumper / myloader CLI options
104+
-------------------------------
105+
106+
By default `mydumper` is invoked with the `-c` (compress backup) and `myloader` with the `-o` (overwrite tables) CLI option.
107+
However, you can modify the CLI options by setting the following environment variable:
108+
109+
* `OPTIONS`: Options passed to `mydumper` (when `MODE` is `BACKUP`) or `myloader` (when `MODE` is `RESTORE`)
110+
111+
Credits
112+
=======
113+
114+
Special thanks to [confirm/docker-mysql-backup](https://github.com/confirm/docker-mysql-backup), which this project uses heavily.

init.sh

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
#
5+
# Retreive and check mode, which can either be "BACKUP" or "RESTORE".
6+
# Based on the mode, different default options will be set.
7+
#
8+
9+
MODE=${MODE:-BACKUP}
10+
TARBALL=${TARBALL:-}
11+
12+
case "${MODE^^}" in
13+
'BACKUP')
14+
if [[ "${TARBALL^^}" != "" ]]
15+
then
16+
OPTIONS="--outputdir=${TARBALL}"
17+
else
18+
OPTIONS=${OPTIONS:--c}
19+
fi
20+
;;
21+
'RESTORE')
22+
OPTIONS=${OPTIONS:--o}
23+
;;
24+
*)
25+
echo 'ERROR: Please set MODE environment variable to "BACKUP" or "RESTORE"' >&2
26+
exit 255
27+
esac
28+
29+
#
30+
# Retreive backup settings and set some defaults.
31+
# Then display the settings on standard out.
32+
#
33+
34+
USER="mybackup"
35+
36+
echo "${MODE} SETTINGS"
37+
echo "================"
38+
echo
39+
echo " User: ${USER}"
40+
echo " UID: ${BACKUP_UID:=666}"
41+
echo " GID: ${BACKUP_GID:=666}"
42+
echo " Umask: ${UMASK:=0022}"
43+
echo
44+
echo " Base directory: i ${BASE_DIR:=/backup}"
45+
[[ "${MODE^^}" == "RESTORE" ]] && \
46+
echo " Restore directory: ${RESTORE_DIR}"
47+
echo
48+
echo " Options: ${OPTIONS}"
49+
echo
50+
51+
#
52+
# Detect linked container settings based on Docker's environment variables.
53+
# Display the container informations on standard out.
54+
#
55+
56+
CONTAINER=$(export | sed -nr "/ENV_MYSQL_ROOT_PASSWORD/{s/^.+ -x (.+)_ENV.+/\1/p;q}")
57+
58+
if [[ -z "${CONTAINER}" ]]
59+
then
60+
echo "ERROR: Couldn't find linked MySQL container." >&2
61+
echo >&2
62+
echo "Please link a MySQL or MariaDB container to the backup container and try again" >&2
63+
exit 1
64+
fi
65+
66+
DB_PORT=$(export | sed -nr "/-x ${CONTAINER}_PORT_[[:digit:]]+_TCP_PORT/{s/^.+ -x (.+)=.+/\1/p}")
67+
DB_ADDR="${CONTAINER}_PORT_${!DB_PORT}_TCP_ADDR"
68+
DB_NAME="${CONTAINER}_ENV_MYSQL_DATABASE"
69+
DB_PASS="${CONTAINER}_ENV_MYSQL_ROOT_PASSWORD"
70+
71+
echo "CONTAINER SETTINGS"
72+
echo "=================="
73+
echo
74+
echo " Container: ${CONTAINER}"
75+
echo
76+
echo " Address: ${!DB_ADDR}"
77+
echo " Port: ${!DB_PORT}"
78+
echo
79+
80+
if [[ -n "${!DB_NAME}" ]]
81+
then
82+
echo " Database: ${!DB_NAME}"
83+
echo
84+
fi
85+
86+
#
87+
# Change UID / GID of backup user and settings umask.
88+
#
89+
90+
[[ "$(id -u ${USER})" == "${BACKUP_UID}" ]] || usermod -o -u $BACKUP_UID ${USER}
91+
[[ "$(id -g ${USER})" == "${BACKUP_GID}" ]] || groupmod -o -g $BACKUP_GID ${USER}
92+
93+
umask ${UMASK}
94+
95+
#
96+
# Building common CLI options to use for mydumper and myloader.
97+
#
98+
#
99+
100+
CLI_OPTIONS="-v 3 -h ${!DB_ADDR} -P ${!DB_PORT} -u root -p ${!DB_PASS}"
101+
102+
if [[ -n "${!DB_NAME}" ]]
103+
then
104+
CLI_OPTIONS+=" -B ${!DB_NAME}"
105+
fi
106+
107+
CLI_OPTIONS+=" ${OPTIONS}"
108+
109+
#
110+
# When MODE is set to "BACKUP", then mydumper has to be used to backup the database.
111+
#
112+
113+
echo "${MODE^^}"
114+
echo "======="
115+
echo
116+
117+
if [[ "${MODE^^}" == "BACKUP" ]]
118+
then
119+
120+
printf "===> Creating base directory... "
121+
mkdir -p ${BASE_DIR}
122+
echo "DONE"
123+
124+
printf "===> Changing owner of base directory... "
125+
chown ${USER}: ${BASE_DIR}
126+
echo "DONE"
127+
128+
printf "===> Changing into base directory... "
129+
cd ${BASE_DIR}
130+
echo "DONE"
131+
132+
echo "===> Starting backup..."
133+
if [[ "${TARBALL^^}" != "" ]]
134+
then
135+
exec su -pc "mydumper ${CLI_OPTIONS} && tar -czvf ${TARBALL}.tgz ${TARBALL} && rm -rf ${TARBALL}" ${USER}
136+
else
137+
exec su -pc "mydumper ${CLI_OPTIONS}" ${USER}
138+
fi
139+
140+
#
141+
# When MODE is set to "RESTORE", then myloader has to be used to restore the database.
142+
#
143+
144+
elif [[ "${MODE^^}" == "RESTORE" ]]
145+
then
146+
147+
printf "===> Changing into base directory... "
148+
cd ${BASE_DIR}
149+
echo "DONE"
150+
151+
if [[ "${TARBALL^^}" != "" ]]
152+
then
153+
RESTORE_DIR=${TARBALL}
154+
rm -rf ${RESTORE_DIR}
155+
echo "===> Restoring database from ${RESTORE_DIR}..."
156+
exec su -pc "tar -xvf ${TARBALL}.tgz ${RESTORE_DIR} && myloader --directory=${RESTORE_DIR} ${CLI_OPTIONS}" ${USER}
157+
else
158+
if [[ -z "${RESTORE_DIR}" ]]
159+
then
160+
printf "===> No RESTORE_DIR set, trying to find latest backup... "
161+
RESTORE_DIR=$(ls -t | head -1)
162+
if [[ -n "${RESTORE_DIR}" ]]
163+
then
164+
echo "DONE"
165+
else
166+
echo "FAILED"
167+
echo "ERROR: Auto detection of latest backup directory failed!" >&2
168+
exit 1
169+
fi
170+
fi
171+
echo "===> Restoring database from ${RESTORE_DIR}..."
172+
exec su -pc "myloader --directory=${RESTORE_DIR} ${CLI_OPTIONS}" ${USER}
173+
fi
174+
fi

0 commit comments

Comments
 (0)