This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdbManager.bash
executable file
·356 lines (264 loc) · 8.44 KB
/
dbManager.bash
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/bin/bash
#
# Copyright (c) 2016 - Present Jeong Han Lee
# Copyright (c) 2016 - Present European Spallation Source ERIC
#
# The program is free software: you can redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of the
# License, or any newer version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see https://www.gnu.org/licenses/gpl-2.0.txt
#
# Author : Jeong Han Lee
# email : jeonghan.lee@gmail.com
# Date :
# version : 0.0.4
#
declare -gr SC_SCRIPT="$(realpath "$0")"
declare -gr SC_TOP="$(dirname "$SC_SCRIPT")"
declare -gr SC_DATE="$(date +%Y%m%d-%H%M)"
set -a
. ${SC_TOP}/env.conf
set +a
. ${SC_TOP}/functions
# Notice!
# No root password of MySQL or MariaDB in localhost is assumed.
#
declare -g root_pwd=""
declare -g SQL_ROOT_CMD="mysql --user=root --password=${root_pwd}"
declare -g SQL_DBUSER_CMD="mysql --user=${DB_USER_NAME} --password=${DB_USER_PWD} ${DB_NAME}"
declare -g SQL_BACKUP_CMD="mysqldump --user=${DB_USER_NAME} --password=${DB_USER_PWD} ${DB_NAME}"
declare -g SQL_CMD_OPTIONS="--skip-column-names --execute"
function no_db_msg() {
printf "\nThere is no >> %s << in the dababase, please check your SQL enviornment.\n\n" "${DB_NAME}"
}
function db_secure_setup() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
# MariaDB Secure Installation without MariaDB root password
# the same as mysql_secure_installation, but skip to setup
# the root password in the script. The reference of the sql commands
# is https://goo.gl/DnyijD
printf "Setup mysql_secure_installation...\n";
# UPDATE mysql.user SET Password=PASSWORD('$passwd') WHERE User='root';
${SQL_ROOT_CMD} <<EOF
-- DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
FLUSH PRIVILEGES;
EOF
__end_func ${func_name};
}
function db_create() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local db_name=${DB_NAME}
local db_user_name= ${DB_USER_NAME}
local db_user_pwd=${DB_USER_PWD}
printf "Create the Database %s if not exists...\n" "${db_name}";
${SQL_ROOT_CMD} <<EOF
CREATE DATABASE IF NOT EXISTS ${db_name}; GRANT ALL PRIVILEGES ON ${db_name}.* TO '${db_user_name}'@'localhost' IDENTIFIED BY '${db_user_pwd}';
EOF
__end_func ${func_name};
}
function db_drop() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local db_name=${DB_NAME}
printf "Drop the Database %s if not exists...\n" "${db_name}";
${SQL_ROOT_CMD} <<EOF
DROP DATABASE IF EXISTS ${db_name};
EOF
__end_func ${func_name};
}
function show_dbs() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local dBs=$(${SQL_ROOT_CMD} ${SQL_CMD_OPTIONS} 'SHOW DATABASES' | awk '{print $1}')
printf "\n";
for db in $dBs
do
printf ">>>>> %24s was found.\n" "${db}"
done
__end_func ${func_name};
}
function show_tables () {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local tables=""
local db_exist=$(isDb);
if [[ $db_exist -ne "$EXIST" ]]; then
no_db_msg;
exit;
else
tables=$(${SQL_DBUSER_CMD} ${SQL_CMD_OPTIONS} "SHOW TABLES" | awk '{print $1}' )
printf "\n";
for table in $tables
do
printf ">>>>> %24s was found.\n" "${table}"
done
fi
__end_func ${func_name};
}
function drop_tables () {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local tables="";
local db_exist=$(isDb);
if [[ $db_exist -ne "$EXIST" ]]; then
no_db_msg;
exit;
else
tables=$(${SQL_DBUSER_CMD} ${SQL_CMD_OPTIONS} 'SHOW TABLES' | awk '{print $1}' )
printf "\n";
for table in $tables
do
printf ". %24s was found. Dropping .... \n" "${table}"
${SQL_DBUSER_CMD} -e "DROP TABLE ${table}"
done
fi
__end_func ${func_name};
}
function fill_tables() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local aa_deploy_db_tables=${SC_TOP}/${AA_GIT_NAME}/src/main/org/epics/archiverappliance/config/persistence/archappl_mysql.sql
local aa_deploy_db_tables_new=${aa_deploy_db_tables}_dbmanager.sql
# DB setup is done when we execute it at the very first time, after this, if we run this script again,
# I would like to add the logic to check whether DB exists or not. So create a new db sql file with
# CREATE TABLE IF NOT EXISTS.
local db_exist=$(isDb);
if [[ $db_exist -ne "$EXIST" ]]; then
no_db_msg;
exit;
else
sed "s/CREATE TABLE /CREATE TABLE IF NOT EXISTS /g" ${aa_deploy_db_tables} > ${aa_deploy_db_tables_new};
${SQL_DBUSER_CMD} < ${aa_deploy_db_tables_new};
fi
__end_func ${func_name};
}
function select_all_from_tables_in_db() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local tables="";
local db_exist=$(isDb);
if [[ $db_exist -ne "$EXIST" ]]; then
no_db_msg;
exit;
else
tables=$(${SQL_DBUSER_CMD} ${SQL_CMD_OPTIONS} 'SHOW TABLES' | awk '{print $1}' )
printf "\n";
for table in $tables
do
printf ". %24s was found. Selecting all .... \n" "${table}"
${SQL_DBUSER_CMD} --execute "SELECT * from ${table}"
done
fi
__end_func ${func_name};
}
function backup_db() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local dbDir="";
local db_exist=$(isDb);
if [[ $db_exist -ne "$EXIST" ]]; then
no_db_msg;
exit;
else
dbDir=$(checkIfDir ${DB_BACKUP_PATH})
if [[ $dbDir -ne "$EXIST" ]]; then
mkdir -p ${SC_TOP}/${DB_BACKUP_PATH}
fi
${SQL_BACKUP_CMD} | gzip -9 > "${DB_BACKUP_PATH}/${DB_NAME}_${SC_DATE}.sql.gz"
fi
__end_func ${func_name};
}
function backup_db_list() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local dbDir=$(checkIfDir ${DB_BACKUP_PATH})
if [[ $dbDir -ne "$EXIST" ]]; then
printf "\nThere is no >> %s << directory, please check your enviornment.\n\n" "${DB_BACKUP_PATH}"
exit;
fi
ls -lta ${SC_TOP}/${DB_BACKUP_PATH}
__end_func ${func_name};
}
function restore_db() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local date=$1;
gunzip < "${DB_BACKUP_PATH}/${DB_NAME}_${date}.sql.gz" | ${SQL_DBUSER_CMD} ;
__end_func ${func_name};
}
function isDb {
local dbname=${DB_NAME};
local output=$(${SQL_ROOT_CMD} ${SQL_CMD_OPTIONS} "SELECT schema_name FROM information_schema.schemata WHERE schema_name=\"${dbname}\"")
local result=""
if [[ -z "${output}" ]]; then
result=${NON_EXIST} # does not exist
else
result=${EXIST} # exists
fi
echo "${result}"
}
db_date=$2
case "$1" in
ssetup_db)
db_secure_setup
;;
create_db)
db_create
show_dbs
;;
show_dbs)
show_dbs
;;
drop_db)
db_drop
show_dbs
;;
fill_tbs)
fill_tables
show_tables
;;
show_tbs)
show_tables
;;
drop_tbs)
drop_tables
show_tables
;;
select_all_from_tbs)
select_all_from_tables_in_db
;;
backup_db)
backup_db
;;
backup_db_list)
backup_db_list
;;
restore_db)
restore_db ${db_date}
;;
isDb)
isDb
;;
*)
echo "">&2
echo " DB >> ${DB_NAME} << Manager for the EPICS Archiver Applance ">&2
echo ""
echo " Usage: $0 <arg>">&2
echo ""
echo " <arg> : info">&2
echo ""
echo " show_dbs : show DBs in >> ${HOSTNAME} << ">&2
echo " create_db : create >> ${DB_NAME} << ">&2
echo " drop_db : drop >> ${DB_NAME} << ">&2
echo " fill_tbs : fill tables in >> ${DB_NAME} << ">&2
echo " show_tbs : show tables in >> ${DB_NAME} << ">&2
echo " drop_tbs : drop tables in >> ${DB_NAME} << ">&2
echo " backup_db : backup >> ${DB_NAME} << in ${DB_BACKUP_PATH}">&2
echo " backup_db_list : backup db list >> ${DB_NAME} << in ${DB_BACKUP_PATH}">&2
echo " restore_db <date> : restore db >> ${DB_NAME} << ">&2
echo " select_all_from_tbs >> ${DB_NAME} << ">&2
echo "">&2
exit 0
esac