-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait-for-database.sh
179 lines (165 loc) · 3.87 KB
/
wait-for-database.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
#!/bin/bash
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--host)
HOST="$2"
shift # past argument
shift # past value
;;
-u|--user)
USER="$2"
shift # past argument
shift # past value
;;
-p|--password)
PASSWORD="$2"
shift # past argument
shift # past value
;;
-d|--database)
DATABASE="$2"
shift # past argument
shift # past argument
;;
-t|--type)
TYPE="$2"
shift # past argument
shift # past argument
;;
-i|--interval)
INTERVAL="$2"
shift # past argument
shift # past argument
;;
-r|--retries)
RETRIES="$2"
shift # past argument
shift # past argument
;;
-y|--table)
TABLE="$2"
shift # past argument
shift # past argument
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
# sanity check
if [[ -z "${HOST}" ]]
then
echo "Error: argument --host is missing"
exit 1
elif [[ -z "${USER}" ]]
then
echo "Error: argument --user is missing"
exit 1
elif [[ -z "${PASSWORD}" ]]
then
echo "Error: argument --password is missing"
exit 1
elif [[ -z "${DATABASE}" ]]
then
echo "Error: argument --database is missing"
exit 1
elif [[ -z "${TYPE}" ]]
then
echo "Error: argument --type is missing"
exit 1
fi
if [[ -z "$INTERVAL" ]]
then
echo "Warn: argument --interval not found, default to 1(s)"
INTERVAL=1
fi
if [[ -z "$RETRIES" ]]
then
echo "Warn: argument --retires not found, default to 1"
RETRIES=1
fi
if [[ "$INTERVAL" -lt 1 ]]
then
echo "Warn: invalid interval '$INTERVAL', min: 1, default to 1(s)"
INTERVAL=1
fi
if [[ "$RETRIES" -lt 1 ]]
then
echo "Warn: invalid retries '$RETRIES', min: 1, default to 1"
RETRIES=1
fi
echo "HOST = ${HOST}"
echo "USER = ${USER}"
echo "PASSWORD = ${PASSWORD}"
echo "DATABASE = ${DATABASE}"
echo "INTERVAL = ${INTERVAL}"
echo "RETRIES = ${RETRIES}"
echo "TABLE = ${TABLE}"
# query initialize
if [[ $TYPE == "MSSQL" ]]
then
QUERY_CHECK_DATABASE="SELECT 1"
CMD_CHECK_DB=(sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASE -Q "${QUERY_CHECK_DATABASE}")
if ! [[ -z "$TABLE" ]]
then
QUERY_CHECK_TABLE="SET NOCOUNT ON; SELECT COUNT(object_id) FROM sys.tables WHERE name='${TABLE}'"
CMD_CHECK_TABLE=(sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASE -Q "${QUERY_CHECK_TABLE}" -h -1)
fi
elif [[ $TYPE == "ORACLE" ]]
then
CMD_CHECK_DB=(./check-oracle.sh $USER $PASSWORD $HOST $DATABASE)
if ! [[ -z "$TABLE" ]]
then
echo "Warn: check oracle table exists is not yet supported."
unset TABLE
fi
elif [[ $TYPE == "MYSQL" ]]
then
QUERY_CHECK_DATABASE="SELECT 1"
CMD_CHECK_DB=(mysqlshow -h $HOST -u$USER -p$PASSWORD $DATABASE)
if ! [[ -z "$TABLE" ]]
then
QUERY_CHECK_TABLE="SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA='${DATABASE}' AND TABLE_NAME='${TABLE}'"
CMD_CHECK_TABLE=(mysql -h $HOST -u$USER -p$PASSWORD -s -N -e "${QUERY_CHECK_TABLE}")
fi
else
echo "Error: unknown input argument type ${TYPE}, available type: MSSQL, ORACLE, MYSQL"
exit 1
fi
echo "CMD_CHECK_DB = ${CMD_CHECK_DB[@]}"
echo "CMD_CHECK_TABLE = ${CMD_CHECK_TABLE[@]}"
# main
COUNT=0
while [ $COUNT -lt $RETRIES ]
do
((COUNT=COUNT+1))
if [[ $("${CMD_CHECK_DB[@]}") ]]
then
echo "Database(${DATABASE}): exists"
if ! [[ -z "$TABLE" ]]
then
if [[ $("${CMD_CHECK_TABLE[@]}") -eq 1 ]]
then
echo "Table(${TABLE}): exists"
exit 0
else
echo "Table(${TABLE}): not found, sleep ${INTERVAL}s"
sleep $INTERVAL
fi
else
exit 0
fi
else
echo "Database(${DATABASE}): not found, sleep ${INTERVAL}s"
sleep $INTERVAL
fi
done
echo "Error: timeout waiting..."
exit 1