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

Commit d923476

Browse files
committed
Merge branch 'fix-db-detection' into 'master'
Fix db detection Also changes variables and drops the auto-detection See merge request ix.ai/mariadb-backup!1
2 parents 6c0ac43 + 5545764 commit d923476

File tree

3 files changed

+34
-45
lines changed

3 files changed

+34
-45
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM debian:buster
22
LABEL MAINTAINER="docker@ix.ai"
3-
ENV DEBIAN_FRONTEND=noninteractive TERM=linux
3+
ENV DEBIAN_FRONTEND=noninteractive TERM=linux DB_PORT=3306 DB_USER=root
44

55
RUN groupadd -g 666 mybackup && \
66
useradd -u 666 -g 666 -d /backup -c "MariaDB Backup User" mybackup && \

README.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ The backup is made with [mydumper](http://centminmod.com/mydumper.html), a fast
88
Usage
99
=====
1010

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.
11+
To backup a [MySQL](https://hub.docker.com/_/mysql/) or [MariaDB](https://hub.docker.com/_/mariadb/) database, you simply specify the credentials and the host. You can optionally specify the database as well.
1212

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:
13+
Environment variables
14+
=====================
1415

15-
* `<CONTAINER>_ENV_MYSQL_DATABASE`
16-
* `<CONTAINER>_ENV_MYSQL_ROOT_PASSWORD`
16+
* `DB_HOST` (mandatory, no default): The host to connect to
17+
* `DB_PASS` (mandatory, no default): The password to use
18+
* `DB_NAME` (optional, no default): If specified, only this database will be backed up
19+
* `DB_PORT` (optional, default `3306`): The password for the SQL server
20+
* `DB_USER` (optional, default `root`): The user to connect to the SQL server
1721

1822
Please note the backup will be written to `/backup` by default, so you might want to mount that directory from your host.
1923

@@ -23,7 +27,7 @@ Example Docker CLI client
2327
To __create a backup__ from a MySQL container via `docker` CLI client:
2428

2529
```bash
26-
docker run --name my-backup --link my-mysql -v /var/mysql_backups:/backup registry.gitlab.com/ix.ai/mariadb-backup
30+
docker run --name my-backup -e DB_HOST=mariadb -e DB_PASS=amazing_pass -v /var/mysql_backups:/backup registry.gitlab.com/ix.ai/mariadb-backup
2731
```
2832

2933
The container will stop automatically as soon as the backup has finished.
@@ -36,22 +40,7 @@ docker start my-backup
3640
To __restore a backup__ into a MySQL container via `docker` CLI client:
3741

3842
```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"
43+
docker run --name my-restore -e DB_HOST=mariadb -e DB_PASS=amazing_pass -v /var/mysql_backups:/backup registry.gitlab.com/ix.ai/mariadb-backup
5544
```
5645

5746
Configuration

init.sh

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ set -e
88

99
MODE=${MODE:-BACKUP}
1010
TARBALL=${TARBALL:-}
11+
DB_PORT=${DB_PORT:-3306}
12+
DB_USER=${DB_USER:-root}
1113

1214
case "${MODE^^}" in
1315
'BACKUP')
@@ -53,35 +55,33 @@ echo
5355
# Display the container informations on standard out.
5456
#
5557

56-
CONTAINER=$(export | sed -nr "/ENV_MYSQL_ROOT_PASSWORD/{s/^.+ -x (.+)_ENV.+/\1/p;q}")
57-
58-
if [[ -z "${CONTAINER}" ]]
58+
if [[ -z "${DB_HOST}" ]]
5959
then
60-
echo "ERROR: Couldn't find linked MySQL container." >&2
60+
echo "ERROR: Couldn't find the SQL host." >&2
6161
echo >&2
62-
echo "Please link a MySQL or MariaDB container to the backup container and try again" >&2
62+
echo "Please set the DB_HOST environment variable" >&2
6363
exit 1
6464
fi
6565

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"
66+
if [[ -z "${DB_PASS}" ]]
67+
then
68+
echo "ERROR: Couldn't find the password for the root user." >&2
69+
echo >&2
70+
echo "Please set the DB_PASS environment variable" >&2
71+
exit 1
72+
fi
7073

71-
echo "CONTAINER SETTINGS"
72-
echo "=================="
74+
echo "DATABASE SETTINGS"
75+
echo "================="
7376
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}" ]]
77+
echo " Host: ${DB_HOST}"
78+
echo " Port: ${DB_PORT}"
79+
echo " User: ${DB_USER}"
80+
if [[ -n "${DB_NAME}" ]]
8181
then
82-
echo " Database: ${!DB_NAME}"
83-
echo
82+
echo " Database: ${DB_NAME}"
8483
fi
84+
echo
8585

8686
#
8787
# Change UID / GID of backup user and settings umask.
@@ -97,11 +97,11 @@ umask ${UMASK}
9797
#
9898
#
9999

100-
CLI_OPTIONS="-v 3 -h ${!DB_ADDR} -P ${!DB_PORT} -u root -p ${!DB_PASS}"
100+
CLI_OPTIONS="-v 3 -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USER} -p ${DB_PASS}"
101101

102-
if [[ -n "${!DB_NAME}" ]]
102+
if [[ -n "${DB_NAME}" ]]
103103
then
104-
CLI_OPTIONS+=" -B ${!DB_NAME}"
104+
CLI_OPTIONS+=" -B ${DB_NAME}"
105105
fi
106106

107107
CLI_OPTIONS+=" ${OPTIONS}"

0 commit comments

Comments
 (0)