-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.sh
executable file
·104 lines (85 loc) · 2.09 KB
/
import.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
#!/usr/bin/env bash
throw_exception() {
# cleanup launched ec2 instances
if [[ ! -z "${cleanup_ec2}" ]] && [[ ! -z "${ec2_id}" ]]; then
consolelog "Deleting ec2: ${ec2_id}" "error"
aws::del_ec2 "${ec2_id}"
ec2_id=
fi
consolelog "Ooops!" "error"
echo 'Stack trace:' 1>&2
while caller $((n++)) 1>&2; do :; done;
exit 1
}
consolelog() {
local color
local ts
# el-cheapo way to detect if timestamp prefix needed
if [[ ! -z "${JENKINS_HOME}" ]]; then
ts=''
else
ts="[$(date -u +'%Y-%m-%d %H:%M:%S')] "
fi
color_reset='\e[0m'
case "${2}" in
success )
color='\e[0;32m'
;;
error )
color='\e[1;31m'
;;
* )
color='\e[0;37m'
;;
esac
if [[ ! -z "${1}" ]] && [[ ! -z "${2}" ]] && [[ "${2}" = "error" ]]; then
printf "${color}%s%s: %s${color_reset}\n" "${ts}" "${0##*/}" "${1}" >&2
elif [[ ! -z "${1}" ]]; then
printf "${color}%s%s: %s${color_reset}\n" "${ts}" "${0##*/}" "${1}"
fi
return 0
}
waitfor::mysql() {
while ! mysql -e "SELECT VERSION();"; do
sleep 5
done
sleep 10
echo ''
}
set -E
trap 'throw_exception' ERR
# add client cnf
cat <<EOF > ~/.my.cnf
[client]
host = 127.0.0.1
user = root
password = ${MYSQL_ROOT_PASSWORD}
EOF
# remove problematic server config options
find /etc/mysql/ -name '*.cnf' -print0 \
| xargs -0 grep -lZE '^(datadir)' \
| xargs -rt -0 sed -Ei 's/^(datadir)/#&/'
# set datadir away from VOLUME
cat <<EOF > /etc/mysql/conf.d/datadir.cnf
[mysqld]
datadir = /var/lib/mysql2
EOF
/entrypoint.sh mysqld &
sleep 10
consolelog "waiting for mysqld..."
waitfor::mysql
sleep 10
for sqlfile in /dumps/*_structure.sql; do
base_name="${sqlfile##*/}"
db="${base_name/_structure.sql/}"
mysql -e "CREATE DATABASE IF NOT EXISTS \`${db}\`;"
consolelog "importing /dumps/${db}_structure.sql ..."
cat "/dumps/${db}_structure.sql" | mysql "${db}"
consolelog "importing /dumps/${db}_data* ..."
cat "/dumps/${db}_data"*.sql | mysql "${db}"
done
if [[ -f "/dumps/custom.sql" ]]; then
consolelog "importing /dumps/custom.sql ..."
mysql < "/dumps/custom.sql"
fi
killall mysqld