-
Notifications
You must be signed in to change notification settings - Fork 24
/
init.sh
executable file
·174 lines (144 loc) · 4.21 KB
/
init.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
#!/bin/bash
set -e
#
# Retreive and check mode, which can either be "BACKUP" or "RESTORE".
# Based on the mode, different default options will be set.
#
MODE=${MODE:-BACKUP}
TARBALL=${TARBALL:-}
case "${MODE^^}" in
'BACKUP')
if [[ "${TARBALL^^}" != "" ]]
then
OPTIONS="--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
#
# Retreive 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: i ${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.
#
CONTAINER=$(export | sed -nr "/ENV_MYSQL_ROOT_PASSWORD/{s/^.+ -x (.+)_ENV.+/\1/p;q}")
if [[ -z "${CONTAINER}" ]]
then
echo "ERROR: Couldn't find linked MySQL container." >&2
echo >&2
echo "Please link a MySQL or MariaDB container to the backup container and try again" >&2
exit 1
fi
DB_PORT=$(export | sed -nr "/-x ${CONTAINER}_PORT_[[:digit:]]+_TCP_PORT/{s/^.+ -x (.+)=.+/\1/p}")
DB_ADDR="${CONTAINER}_PORT_${!DB_PORT}_TCP_ADDR"
DB_NAME="${CONTAINER}_ENV_MYSQL_DATABASE"
DB_PASS="${CONTAINER}_ENV_MYSQL_ROOT_PASSWORD"
echo "CONTAINER SETTINGS"
echo "=================="
echo
echo " Container: ${CONTAINER}"
echo
echo " Address: ${!DB_ADDR}"
echo " Port: ${!DB_PORT}"
echo
if [[ -n "${!DB_NAME}" ]]
then
echo " Database: ${!DB_NAME}"
echo
fi
#
# 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_ADDR} -P ${!DB_PORT} -u root -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=$(ls -t | head -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