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

Commit 553a15d

Browse files
committed
Merge branch '2-allow-read' into 'master'
Resolve #2 "Allow read" See merge request ix.ai/mariadb-backup!10
2 parents 4d986be + fcf7c73 commit 553a15d

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ To backup a [MySQL](https://hub.docker.com/_/mysql/) or [MariaDB](https://hub.do
1515

1616
## Environment variables
1717

18-
| **Variable** | **Default** | **Mandatory** | **Description** |
19-
|:--------------|:-----------:|:-------------:|:-----------------------------------------------------|
20-
| `DB_HOST` | - | *yes* | The host to connect to |
21-
| `DB_PASS` | - | *yes* | The password for the SQL server |
22-
| `DB_NAME` | - | *no* | If specified, only this database will be backed up |
23-
| `DB_PORT` | `3306` | *no* | The port of the SQL server |
24-
| `DB_USER` | `root` | *no* | The user to connect to the SQL server |
25-
| `MODE` | `BACKUP` | *no* | One of `BACKUP` or `RESTORE` |
26-
| `BASE_DIR` | `/backup` | *no* | Path of the base directory (aka working directory) |
27-
| `RESTORE_DIR` | - | *no* | Name of a backup directory to restore |
28-
| `BACKUP_UID` | `666` | *no* | UID of the backup |
29-
| `BACKUP_GID` | `666` | *no* | GID of the backup |
30-
| `UMASK` | `0022` | *no* | Umask which should be used to write the backup files |
31-
| `OPTIONS` | `-c` / `-o` | *no* | Options passed to `mydumper` / `myloader` |
18+
| **Variable** | **FILE Support** | **Default** | **Mandatory** | **Description** |
19+
|:--------------|:----------------:|:-----------:|:-------------:|:-----------------------------------------------------|
20+
| `DB_HOST` | `DB_HOST__FILE` | - | *yes* | The host to connect to |
21+
| `DB_PASS` | `DB_PASS__FILE` | - | *yes* | The password for the SQL server |
22+
| `DB_NAME` | `DB_NAME__FILE` | - | *no* | If specified, only this database will be backed up |
23+
| `DB_PORT` | N/A | `3306` | *no* | The port of the SQL server |
24+
| `DB_USER` | `DB_USER__FILE` | `root` | *no* | The user to connect to the SQL server |
25+
| `MODE` | N/A | `BACKUP` | *no* | One of `BACKUP` or `RESTORE` |
26+
| `BASE_DIR` | N/A | `/backup` | *no* | Path of the base directory (aka working directory) |
27+
| `RESTORE_DIR` | N/A | - | *no* | Name of a backup directory to restore |
28+
| `BACKUP_UID` | N/A | `666` | *no* | UID of the backup |
29+
| `BACKUP_GID` | N/A | `666` | *no* | GID of the backup |
30+
| `UMASK` | N/A | `0022` | *no* | Umask which should be used to write the backup files |
31+
| `OPTIONS` | N/A | `-c` / `-o` | *no* | Options passed to `mydumper` / `myloader` |
32+
33+
The following environment variables support setting via `*__FILE`, where
3234

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

mariadb-backup.sh

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,37 @@ set -e
66
# Based on the mode, different default options will be set.
77
#
88

9+
file_env() {
10+
local var="$1"
11+
local fileVar="${var}__FILE"
12+
local def="${2:-}"
13+
14+
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
15+
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
16+
exit 1
17+
fi
18+
local val="$def"
19+
if [ "${!var:-}" ]; then
20+
val="${!var}"
21+
elif [ "${!fileVar:-}" ]; then
22+
val="$(< "${!fileVar}")"
23+
fi
24+
export "$var"="$val"
25+
unset "$fileVar"
26+
}
27+
28+
if [[ "${DB_USER}" == "root" ]]; then
29+
#
30+
# If this is set to the default, then unset it in case we are going to get it from a secret
31+
#
32+
unset DB_USER
33+
fi
34+
35+
file_env "DB_PASS"
36+
file_env "DB_USER"
37+
file_env "DB_HOST"
38+
file_env "DB_NAME"
39+
940
MODE=${MODE:-BACKUP}
1041
TARBALL=${TARBALL:-}
1142
DB_PORT=${DB_PORT:-3306}
@@ -43,7 +74,7 @@ echo " UID: ${BACKUP_UID:=666}"
4374
echo " GID: ${BACKUP_GID:=666}"
4475
echo " Umask: ${UMASK:=0022}"
4576
echo
46-
echo " Base directory: i ${BASE_DIR:=/backup}"
77+
echo " Base directory: ${BASE_DIR:=/backup}"
4778
[[ "${MODE^^}" == "RESTORE" ]] && \
4879
echo " Restore directory: ${RESTORE_DIR}"
4980
echo

0 commit comments

Comments
 (0)