-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQL_upload_single.sh
86 lines (77 loc) · 1.87 KB
/
MySQL_upload_single.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
#!/bin/bash
# backup one database file
# sh MySQL_upload_single.sh -u {user} -p{password} -h {server_ip} {path_to_sql_file} -v;
usage() {
echo "$0 wrong usage." >&2
echo "use it like this:"
echo " -> sh MySQL_upload_single.sh -u{user} -p{password} -h {server_ip} {path_to_sql_file} -v;"
exit 1
}
ARGS=()
FILES=()
arg() {
ARGS=("${ARGS[@]}" "$1")
}
file() {
FILES=("${FILES[@]}" "$1")
}
[[ $# == 0 ]] && usage
# Slightly modified getopts alternative
while [[ $# != 0 ]] ; do
case $1 in
--)
break
;;
-\?)
usage
;;
-[uphS])
arg $1
arg $2
shift
;;
-*)
arg $1
;;
*)
file $1
;;
esac
shift
done
for FILE in "${FILES[@]}" ; do
if [[ ! -f $FILE ]] ; then
echo "-> Skipping '$FILE' (not a file)"
continue
fi
NAME=${FILE##*/}
echo "││┬ 📋 create result directory"
mkdir results
echo "││└ ✅"
echo "││" # ----
if [[ $FILE == *.[gG][zZ] ]] ; then
NAME=${NAME%.[gG][zZ]}
DB=${NAME%.[sS][qQ][lL]}
echo "││┬ 📋 create result file for $DB"
db_res="results/$DB.result.txt"
touch "$db_res"
echo "││└ ✅"
echo "││" # ----
echo "││┬ 📋 uploading DB \033[1m$DB\033[0m from file \033[1m$FILE\033[0m"
mysql "${ARGS[@]}" -e "create database if not exists \`$DB\`;" | sed 's/^/ /'
gunzip < "$FILE" | mysql $DB "${ARGS[@]}" -f >> $db_res 2>&1 | sed 's/^/ /'
echo "││└ ✅"
# the command | sed 's/^/ /' is only for formatting
else
DB=${NAME%.[sS][qQ][lL]}
echo "││┬ 📋 create result file for $DB"
db_res="results/$DB.result.txt"
touch "$db_res"
echo "││└ ✅"
echo "││" # ----
echo "││┬ 📋 uploading DB \033[1m$DB\033[0m from file \033[1m$FILE\033[0m"
mysql "${ARGS[@]}" -e "create database if not exists \`$DB\`;" | sed 's/^/ /'
mysql $DB "${ARGS[@]}" < "$FILE" -f >> $db_res 2>&1 | sed 's/^/ /'
echo "││└ ✅"
fi
done