1- About
2- =====
1+ # mariadb-backup
32
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.
3+ [ ![ Pipeline Status] ( https://gitlab.com/ix.ai/mariadb-backup/badges/master/pipeline.svg )] ( https://gitlab.com/ix.ai/mariadb-backup/ )
4+ [ ![ Docker Stars] ( https://img.shields.io/docker/stars/ixdotai/mariadb-backup.svg )] ( https://hub.docker.com/r/ixdotai/mariadb-backup/ )
5+ [ ![ Docker Pulls] ( https://img.shields.io/docker/pulls/ixdotai/mariadb-backup.svg )] ( https://hub.docker.com/r/ixdotai/mariadb-backup/ )
6+ [ ![ Gitlab Project] ( https://img.shields.io/badge/GitLab-Project-554488.svg )] ( https://gitlab.com/ix.ai/mariadb-backup/ )
7+
8+ The mariadb-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.
59
610The backup is made with [ mydumper] ( http://centminmod.com/mydumper.html ) , a fast MySQL backup utility.
711
8- Usage
9- =====
12+ ## Usage
1013
1114To 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.
1215
13- Environment variables
14- =====================
15-
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
16+ ## Environment variables
17+ | ** Variable** | ** Default** | ** Mandatory** | ** Description** |
18+ | :--------------| :-----------:| :-------------:| :-----------------------------------------------------|
19+ | ` DB_HOST ` | - | * yes* | The host to connect to |
20+ | ` DB_PASS ` | - | * yes* | The password for the SQL server |
21+ | ` DB_NAME ` | - | * no* | If specified, only this database will be backed up |
22+ | ` DB_PORT ` | ` 3306 ` | * no* | The port of the SQL server |
23+ | ` DB_USER ` | ` root ` | * no* | The user to connect to the SQL server |
24+ | ` MODE ` | ` BACKUP ` | * no* | One of ` BACKUP ` or ` RESTORE ` |
25+ | ` BASE_DIR ` | ` /backup ` | * no* | Path of the base directory (aka working directory) |
26+ | ` RESTORE_DIR ` | - | * no* | Name of a backup directory to restore |
27+ | ` BACKUP_UID ` | ` 666 ` | * no* | UID of the backup |
28+ | ` BACKUP_GID ` | ` 666 ` | * no* | GID of the backup |
29+ | ` UMASK ` | ` 0022 ` | * no* | Umask which should be used to write the backup files |
30+ | ` OPTIONS ` | ` -c ` / ` -o ` | * no* | Options passed to ` mydumper ` / ` myloader ` |
2131
2232Please note the backup will be written to ` /backup ` by default, so you might want to mount that directory from your host.
2333
24- Example Docker CLI client
25- -------------------------
34+ ## Example Docker CLI client
2635
2736To __ create a backup__ from a MySQL container via ` docker ` CLI client:
2837
2938``` bash
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
39+ docker run --name my-backup -e DB_HOST=mariadb -e DB_PASS=amazing_pass -v /var/mysql_backups:/backup ixdotai/ mariadb-backup:latest
3140```
3241
3342The container will stop automatically as soon as the backup has finished.
@@ -40,30 +49,47 @@ docker start my-backup
4049To __ restore a backup__ into a MySQL container via ` docker ` CLI client:
4150
4251``` bash
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
52+ docker run --name my-restore -e DB_HOST=mariadb -e DB_PASS=amazing_pass -v /var/mysql_backups:/backup ixdotai/ mariadb-backup:latest
4453```
4554
46- Configuration
47- =============
55+ ## Script example
56+ The following example uses the image [ ] ( ) for MariaDB.
57+ To back up multiple databases, all running in docker, all labeled with ` mariadb-backup ` :
58+ ``` bash
59+ #! /usr/bin/env bash
60+ /bin/mkdir -p /mariadb-backup
61+
62+ /usr/bin/docker pull ixdotai/mariadb-backup:latest
63+
64+ for CONTAINER in $( /usr/bin/docker ps -f label=mariadb-backup --format=' {{.Names}}' ) ; do
65+ DB_PASS=$( /usr/bin/docker inspect ${CONTAINER} | /usr/bin/jq -r ' .[0]|.Config.Env[]|select(test("^MARIADB_ROOT_PASSWORD.*"))' | /bin/sed -n ' s/^MARIADB_ROOT_PASSWORD=\(.*\)/\1/p' )
66+ DB_NAME=$( /usr/bin/docker inspect ${CONTAINER} | /usr/bin/jq -r ' .[0]|.Config.Env[]|select(test("^MARIADB_DATABASE.*"))' | /bin/sed -n ' s/^MARIADB_DATABASE=\(.*\)/\1/p' )
67+ DB_NET=$( /usr/bin/docker inspect ${CONTAINER} | /usr/bin/jq -r ' .[0]|.NetworkSettings.Networks|to_entries[]|.key' )
68+ if [[ -n " ${DB_PASS} " ]]; then
69+ /usr/bin/docker run --rm --name ${CONTAINER} -backup -e DB_PASS=${DB_PASS} -e DB_HOST=${CONTAINER} -e DB_NAME=${DB_NAME} --network ${DB_NET} -v /mariadb-backup:/backup ixdotai/mariadb-backup:latest
70+ fi
71+ done
72+
73+ ```
74+
75+ ## Configuration
76+
4877
49- Mode
50- ----
78+ ### Mode
5179
5280By default the container backups the database.
5381However, you can change the mode of the container by setting the following environment variable:
5482
5583* ` MODE ` : Sets the mode of the backup container while [ ` BACKUP ` |` RESTORE ` ]
5684
57- Base directory
58- --------------
85+ ### Base directory
5986
6087By default the base directory ` /backup ` is used.
6188However, you can overwrite that by setting the following environment variable:
6289
6390* ` BASE_DIR ` : Path of the base directory (aka working directory)
6491
65- Restory directory
66- -----------------
92+ ### Restore directory
6793
6894By default the container will automatically restore the latest backup found in ` BASE_DIR ` .
6995However, you can manually set the name of a backup directory underneath ` BASE_DIR ` :
@@ -72,32 +98,28 @@ However, you can manually set the name of a backup directory underneath `BASE_DI
7298
7399_ This option is only required when the container runs in in ` RESTORE ` mode._
74100
75- UID and GID
76- -----------
101+ ### UID and GID
77102
78103By default the backup will be written with UID and GID ` 666 ` .
79104However, you can overwrite that by setting the following environment variables:
80105
81106* ` BACKUP_UID ` : UID of the backup
82107* ` BACKUP_GID ` : GID of the backup
83108
84- umask
85- -----
109+ ### umask
86110
87111By default a ` umask ` of ` 0022 ` will be used.
88112However, you can overwrite that by setting the following environment variable:
89113
90114* ` UMASK ` : Umask which should be used to write the backup files
91115
92- mydumper / myloader CLI options
93- -------------------------------
116+ ### mydumper / myloader CLI options
94117
95118By default ` mydumper ` is invoked with the ` -c ` (compress backup) and ` myloader ` with the ` -o ` (overwrite tables) CLI option.
96119However, you can modify the CLI options by setting the following environment variable:
97120
98121* ` OPTIONS ` : Options passed to ` mydumper ` (when ` MODE ` is ` BACKUP ` ) or ` myloader ` (when ` MODE ` is ` RESTORE ` )
99122
100- Credits
101- =======
123+ ## Credits
102124
103125Special thanks to [ confirm/docker-mysql-backup] ( https://github.com/confirm/docker-mysql-backup ) , which this project uses heavily.
0 commit comments