-
Notifications
You must be signed in to change notification settings - Fork 3
/
mariadb-backup.sh
executable file
·205 lines (173 loc) · 4.74 KB
/
mariadb-backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
set -e
#
# Retrieve and check mode, which can either be "BACKUP" or "RESTORE".
# Based on the mode, different default options will be set.
#
file_env() {
local var="$1"
local fileVar="${var}__FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
if [[ "${DB_USER}" == "root" ]]; then
#
# If this is set to the default, then unset it in case we are going to get it from a secret
#
unset DB_USER
fi
file_env "DB_PASS"
file_env "DB_USER"
file_env "DB_HOST"
file_env "DB_NAME"
MODE=${MODE:-BACKUP}
TARBALL=${TARBALL:-}
DB_PORT=${DB_PORT:-3306}
DB_USER=${DB_USER:-root}
case "${MODE^^}" in
'BACKUP')
if [[ "${TARBALL^^}" != "" ]]
then
OPTIONS="${OPTIONS:--c} --outputdir=${TARBALL}"
else
OPTIONS=${OPTIONS:--c}
fi
;;
'RESTORE')
OPTIONS=${OPTIONS:--o}
;;
*)
echo 'ERROR: Please set MODE environment variable to "BACKUP" or "RESTORE"' >&2
exit 255
esac
#
# Retrieve backup settings and set some defaults.
# Then display the settings on standard out.
#
USER="mybackup"
echo "${MODE} SETTINGS"
echo "================"
echo
echo " User: ${USER}"
echo " UID: ${BACKUP_UID:=666}"
echo " GID: ${BACKUP_GID:=666}"
echo " Umask: ${UMASK:=0022}"
echo
echo " Base directory: ${BASE_DIR:=/backup}"
[[ "${MODE^^}" == "RESTORE" ]] && \
echo " Restore directory: ${RESTORE_DIR}"
echo
echo " Options: ${OPTIONS}"
echo
#
# Detect linked container settings based on Docker's environment variables.
# Display the container informations on standard out.
#
if [[ -z "${DB_HOST}" ]]
then
echo "ERROR: Couldn't find the SQL host." >&2
echo >&2
echo "Please set the DB_HOST environment variable" >&2
exit 1
fi
if [[ -z "${DB_PASS}" ]]
then
echo "ERROR: Couldn't find the password for the root user." >&2
echo >&2
echo "Please set the DB_PASS environment variable" >&2
exit 1
fi
echo "DATABASE SETTINGS"
echo "================="
echo
echo " Host: ${DB_HOST}"
echo " Port: ${DB_PORT}"
echo " User: ${DB_USER}"
if [[ -n "${DB_NAME}" ]]
then
echo " Database: ${DB_NAME}"
fi
echo
#
# Change UID / GID of backup user and settings umask.
#
[[ "$(id -u ${USER})" == "${BACKUP_UID}" ]] || usermod -o -u $BACKUP_UID ${USER}
[[ "$(id -g ${USER})" == "${BACKUP_GID}" ]] || groupmod -o -g $BACKUP_GID ${USER}
umask ${UMASK}
#
# Building common CLI options to use for mydumper and myloader.
#
#
CLI_OPTIONS="-v 3 -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USER} -p ${DB_PASS}"
if [[ -n "${DB_NAME}" ]]
then
CLI_OPTIONS+=" -B ${DB_NAME}"
fi
CLI_OPTIONS+=" ${OPTIONS}"
#
# When MODE is set to "BACKUP", then mydumper has to be used to backup the database.
#
echo "${MODE^^}"
echo "======="
echo
if [[ "${MODE^^}" == "BACKUP" ]]
then
printf "===> Creating base directory... "
mkdir -p ${BASE_DIR}
echo "DONE"
printf "===> Changing owner of base directory... "
chown ${USER}: ${BASE_DIR}
echo "DONE"
printf "===> Changing into base directory... "
cd ${BASE_DIR}
echo "DONE"
echo "===> Starting backup..."
if [[ "${TARBALL^^}" != "" ]]
then
exec su -pc "mydumper ${CLI_OPTIONS} && tar -czvf ${TARBALL}.tgz ${TARBALL} && rm -rf ${TARBALL}" ${USER}
else
exec su -pc "mydumper ${CLI_OPTIONS}" ${USER}
fi
#
# When MODE is set to "RESTORE", then myloader has to be used to restore the database.
#
elif [[ "${MODE^^}" == "RESTORE" ]]
then
printf "===> Changing into base directory... "
cd ${BASE_DIR}
echo "DONE"
if [[ "${TARBALL^^}" != "" ]]
then
RESTORE_DIR=${TARBALL}
rm -rf "${RESTORE_DIR}"
echo "===> Restoring database from ${RESTORE_DIR}..."
exec su -pc "tar -xvf ${TARBALL}.tgz ${RESTORE_DIR} && myloader --directory=${RESTORE_DIR} ${CLI_OPTIONS}" ${USER}
else
if [[ -z "${RESTORE_DIR}" ]]
then
printf "===> No RESTORE_DIR set, trying to find latest backup... "
RESTORE_DIR=$(find . -maxdepth 1)
if [[ -n "${RESTORE_DIR}" ]]
then
echo "DONE"
else
echo "FAILED"
echo "ERROR: Auto detection of latest backup directory failed!" >&2
exit 1
fi
fi
echo "===> Restoring database from ${RESTORE_DIR}..."
exec su -pc "myloader --directory=${RESTORE_DIR} ${CLI_OPTIONS}" ${USER}
fi
fi