-
Notifications
You must be signed in to change notification settings - Fork 8
/
loop.sh
70 lines (57 loc) · 1.76 KB
/
loop.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
#!/bin/bash
# set -x
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
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"
}
file_env 'MYSQL_PASSWORD'
file_env 'MYSQLDUMP_ADD_OPTS'
file_env 'MYSQL_CONNECTION_PARAMS'
file_env 'MYSQL_HOST'
file_env 'MYSQL_USER'
if [ -f "$MYSQL_ENV_MYSQL_ROOT_PASSWORD_FILE" ]; then
MYSQL_PASSWORD=$(<$MYSQL_ENV_MYSQL_ROOT_PASSWORD_FILE)
fi
if [ -z "$MYSQL_PASSWORD" ] ; then
MYSQL_PASSWORD="$MYSQL_ENV_MYSQL_ROOT_PASSWORD"
fi
echo "sleeping $BACKUP_FIRSTDELAY seconds before first backup"
sleep $BACKUP_FIRSTDELAY
while true ; do
# truncate the errorsfile. So if it is empty then everything is ok
# (BTW. truncate always touches the modification time of the file.)
truncate --size=0 /var/dbdumps/errorslastrun.log
if [ -z "$MYSQL_CONNECTION_PARAMS" ] ; then
cat << EOF > ~/.my.cnf
[client]
user = $MYSQL_USER
password = $MYSQL_PASSWORD
host = $MYSQL_HOST
EOF
chmod 0600 ~/.my.cnf
# cat ~/.my.cnf
MYSQL_CONNECTION_PARAMS=""
fi
echo "start backup"
./backup-all-mysql.sh "$@" $MYSQL_CONNECTION_PARAMS
# TODO: sleep BACKUP_INTERVAL minus duration of last dump
echo "sleeping $BACKUP_INTERVAL seconds"
sleep $BACKUP_INTERVAL
done