-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.sh
executable file
·452 lines (366 loc) · 11 KB
/
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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/bin/bash
# ======
# DB-CLI
# ======
#set working dir
CURRENT_DIR=$(pwd -P)
DIR_ROOT=$(dirname -- "$(realpath "$0")")
cd "$DIR_ROOT" || exit 1
source config/autoload.sh
# ===========
# = Actions =
# ===========
# Dispatcher
function dispatcher() {
local action=$1
#remove one argument
shift
case $action in
"-h" | "--help") man ;;
"l" | "list") database_list ;;
"ls" | "listsize") database_list_size ;;
"c" | "create") database_create "$@" ;;
"d" | "delete" | "drop") database_drop "$@" ;;
"i" | "import") database_import "$@" ;;
"e" | "export") database_export "$@" ;;
"ba" | "backupall") database_backup_all "$@" ;;
"r" | "restore") database_restore "$@" ;;
"copy") database_copy "$@" ;;
"exec") database_exec "$@" ;;
"cleartmp") database_cleartmp "$@" ;;
*)
log.fatal "$1" "ERROR Unknown command"
man
;;
esac
}
function man() {
log.header "-- DB-CLI --"
log.header "Hash: $(git branch) #$(git rev-parse --short HEAD)"
log.header "To show the manual: man db-cli"
}
function database_list() {
mysql.request_auth
log.header "List of databases"
mysql.list "$CFG_DB_USER" "$CFG_DB_PASSWORD"
}
function database_list_size() {
mysql.request_auth
log.header "List of databases (non-empty) and their size"
mysql.list_size "$CFG_DB_USER" "$CFG_DB_PASSWORD"
}
function database_create() {
log.header "Create database $1"
helper.request_db_param "$1"
mysql.request_auth
mysql.create "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$1"
log.success "Database $1 created"
}
function database_drop() {
log.header "Deleting database $1"
helper.request_db_param "$1"
if input.askcontinue "Are you sure you want to delete the database \"$1\" ?"; then
mysql.request_auth
mysql.drop "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$1"
log.success "Database $1 deleted"
else
log.info "Aborted"
fi
}
function database_export() {
local file size remoteArg
local remote="localhost"
local port=22
log.header "Export database $1"
#opts
POSITIONAL=()
for i in "$@"; do
case $i in
--file=*) file="${i#*=}" ;;
--remote-arg=*) remoteArg="${i#*=}" ;;
--remote*) remote="${i#*=}" ;;
-*) shift ;; #unknown params with dash
*) POSITIONAL+=("$i") ;;
esac
done
#restore positional parameters
set -- "${POSITIONAL[@]}"
helper.request_db_param "$1"
date.datetime date true
date=${date//:/-}
#export at current location
if [[ $file == "." ]]; then
file=$CURRENT_DIR/$date.$1.sql.gz
fi
#if output file is not specified
if [ -z "$file" ]; then
backup.get_backup_dir dir "$1"
file=$date.$1.sql.gz
#if db dir exist put file in db dir
if [ -n "$dir" ]; then
file=$dir/$file
fi
fi
# Relative path
if [[ $file != /* ]]; then
file="$CURRENT_DIR/$file"
fi
file=$(realpath "$file")
#export from localhost
if [ "$remote" == "localhost" ]; then
mysql.request_auth
if ! (mysql.exist "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$1"); then
log.error "Database $1 not exist"
exit 1
fi
log.info "Exporting DB $1"
mysql.export "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$1" "$file"
if [ -f "$file" ]; then
#check file size
file.size.readable size "$file"
log.success "DONE $file ($size)"
else
log.error "Problem with export"
exit 1
fi
else
#export from remote server
#remote value is not localhost ask for server
if [ "$remote" == "--remote" ]; then
log.newline
mysql.remote.ask_server remote
log.newline
fi
if [ -z "$remote" ]; then
log.error "Remote server is not set"
exit 1
fi
log.info "Exporting remote DB $1 from $remote"
if ! mysql.remote.request_auth; then
log.warning "Invalid authentification"
exit 1
fi
timer.start
mysql.remote.export "$file" "$remote" "$port" "$REMOTE_SSH_USER" "$1" "$REMOTE_DB_USER" "$REMOTE_DB_PASSWORD" "$remoteArg"
#check file size
file.size.readable size "$file"
#check error
if [[ "$size" == 0* ]]; then
log.error "Error"
exit 1
else
log.success "DONE $file ($size) ($(timer.end)s)"
fi
fi
}
function database_import() {
local file realfile remoteArg
local remote="localhost"
local port=22
log.header "Import database $1"
#opts
POSITIONAL=()
for i in "$@"; do
case $i in
--file=*) file="${i#*=}" ;;
--remote-arg=*) remoteArg="${i#*=}" ;;
--remote*) remote="${i#*=}" ;;
-*) shift ;; #unknown params with dash
*) POSITIONAL+=("$i") ;;
esac
done
#restore positional parameters
set -- "${POSITIONAL[@]}"
if [[ -z $1 ]]; then
log.info "Usage: db i [dbName]"
log.info "List of existing backups"
log.newline
backup.get_backup_dir dir
ls "$dir" | nl
log.newline
fi
helper.request_db_param "$1"
mysql.request_auth
#import from localhost
if [ "$remote" == "localhost" ]; then
#if input file is not specified
if [ -z "$file" ]; then
backup.get_backup_dir dir "$1"
#if db dir exist get file from db dir
if [ -n "$dir" ]; then
lastFile=$(ls "$dir" | tail -n 1)
if [[ -n $lastFile ]]; then
file=$dir/$(ls "$dir" | tail -n 1)
fi
fi
fi
#input file not exist because of relative path
if [[ -n $file && ! -f $file && -f $CURRENT_DIR/$file ]]; then
file=$(realpath "$CURRENT_DIR/$file")
fi
if [[ -f $file ]]; then
file=$(realpath "$file")
fi
if [[ $file =~ \.gz$ || $file =~ \.zip$ ]]; then
log.info "Unzipping $file"
fi
helper.get_dump realfile "$file"
if [[ -z $realfile ]]; then
log.error "Input file not specified or not found in backup dir $dir"
exit 1
fi
if [ ! -f "$realfile" ]; then
log.error "File $realfile does not exist"
exit 1
fi
file.size.readable size "$file"
#begin import
log.info "Importing DB $1 from $file ($size)"
timer.start
mysql.import "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$1" "$realfile"
log.success "DONE ($(timer.end)s)"
else
#remote value is not localhost ask for server
if [ "$remote" == "--remote" ]; then
log.newline
mysql.remote.ask_server remote
log.newline
fi
if [ -z "$remote" ]; then
log.error "Remote server is not set"
exit 1
fi
log.info "Importing remote DB $1 from $remote"
if ! mysql.remote.request_auth; then
log.warning "Invalid authentification"
exit 1
fi
timer.start
if mysql.remote.import "$remote" "$port" "$REMOTE_SSH_USER" "$1" "$REMOTE_DB_USER" "$REMOTE_DB_PASSWORD" "$remoteArg"; then
log.success "DONE ($(timer.end)s)"
else
log.error "Error"
exit 1
fi
fi
}
function database_restore() {
local file
log.header "Restore database $1"
helper.request_db_param "$1"
backup.list file "$1" || exit 1
#incorrect number
if [ -z "$file" ]; then
log.error "Incorrect number"
exit 1
fi
database_import "$@" --file="$file"
}
function database_copy() {
log.header "Copy database"
mysql.request_auth
helper.request_db_param "$1"
helper.request_db_param "$2" 3
#check if db source exist
if ! (mysql.exist "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$1"); then
log.error "Database $1 not exist"
exit 1
fi
#check if destination exist
if mysql.exist "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$2"; then
if ! (input.askcontinue "Do you want to overwrite database \"$2\" ?"); then
log.info "Aborted"
exit
fi
fi
#delete then create destination DB
mysql.drop "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$2"
mysql.create "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$2"
log.info "Copying $1 to $2"
#copy using flux
if [ -z "$CFG_DB_PASSWORD" ]; then
mysqldump --single-transaction -u "$CFG_DB_USER" "$1" | mysql -u "$CFG_DB_USER" "$2"
else
mysqldump --single-transaction -u "$CFG_DB_USER" -p"$CFG_DB_PASSWORD" "$1" | mysql -u "$CFG_DB_USER" -p"$CFG_DB_PASSWORD" "$2"
fi
log.success "DONE"
}
function database_exec() {
log.header "Execute SQL"
mysql.request_auth
#check sql
if [ -z "$1" ]; then
log.error "Missing parameter 2"
exit 1
fi
shopt -s nocasematch
#show process list
if [[ "$1" =~ "processlist" ]] || [[ "$1" =~ "show tables" ]]; then
mysql.exec_full "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$1" "$2"
else
mysql.exec "$CFG_DB_USER" "$CFG_DB_PASSWORD" "$1" "$2"
fi
}
function database_backup_all() {
local ignore dir dbs today file user pass
local subdir=$1
log.header "Backup all databases"
today=$(date '+%Y%m%d')
if [[ -v CFG_DB_USER ]]; then
user=$CFG_DB_USER
else
user=$CFG_DB_BACKUP_USER
fi
if [[ -v CFG_DB_PASSWORD ]]; then
pass=$CFG_DB_PASSWORD
else
pass=$CFG_DB_BACKUP_PASSWORD
fi
#list all database
if [[ -n $pass ]]; then
dbs="$(mysql -u "$user" -p"$pass" -Bse 'show databases')"
else
dbs="$(mysql -u "$user" -Bse 'show databases')"
fi
dir=$CFG_DB_BACKUP_DIR
#point to subdir if define
[[ -n $subdir ]] && dir=$dir/$subdir
mkdir -p "$dir"
for db in $dbs; do
#trim white space
db="${db%"${db##*[![:space:]]}"}"
ignore=false
#if array count is not empty
if [ ${#CFG_DB_BACKUP_IGNORE[@]} -ne 0 ]; then
for i in "${CFG_DB_BACKUP_IGNORE[@]}"; do
[[ $db == "$i" ]] && ignore=true && break
done
fi
#dump DB if not ignored
if ! $ignore; then
log.info "Backing up DB $db"
file="${dir}/${CFG_DB_BACKUP_PREFIX}${today}.${db}.sql.gz"
if [[ -n $pass ]]; then
mysqldump --single-transaction --lock-tables=false -u "$user" -p"$pass" "$db" | gzip >"$file"
else
mysqldump --single-transaction --lock-tables=false -u "$user" "$db" | gzip >"$file"
fi
file.size.readable size "$file"
log.info ">> $file ($size)"
fi
done
}
function database_cleartmp() {
if [[ -d $DIR_ROOT/runtime/tmp ]]; then
rm -f "$DIR_ROOT"/runtime/tmp/*.sql
fi
echo "Tmp DB cleared"
}
# ========
# = INIT =
# ========
#case number of args in command line
case $# in
0) man ;;
*) dispatcher "$@" ;;
esac