-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_surface-schema.sh
executable file
·112 lines (96 loc) · 2.55 KB
/
mysql_surface-schema.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
#!/usr/bin/env bash
function help() {
cat <<EOF
usage: ${0##*/} [ U=[mysql-user] ] [ p=[mysql-password] ] [ D=[mysql-database] ] [ H=[mysql-host] ] [ P=[mysql-port] ] [ --mode=[yml|anything] ]
Creates a yml friendly database 'schema' (ordered alphabetically) where only details are table and column names.
> Parameter order does not matter, but is case sensitive
Env Vars (params take precedence, even over .myncf):
MYSQL_USER
MYSQL_PASSWORD
MYSQL_DATABASE
MYSQL_HOST
MYSQL_PORT
Requirements:
awk
bash 4+
mysql
example:
read -s PW
MYSQL_PASSWORD=\$PW ./mysql_surface-schema-to-yml.sh U=user D=db H=localhost P=3306 --mode=yml
unset PW
EOF
}
FILTER_ERR='Using a password on the command line interface can be insecure'
function err_chk() {
if [[ 0 -ne $1 ]]; then
echo -e "ERROR:\n $2" >&2
echo "" >&2
help >&2
exit 1
fi
}
# regex arguments for combinations of --help (help, -h, ect)
for x in ${@}; do
if [[ "${x,,}" =~ ^(-+h(elp)?|-*help)$ ]]; then
help
exit 0
fi
done
args=(${@})
MODE=""
for ((i=0; i< ${#args[@]}; i++)); do
if [[ "${args[$i]:0:2}" == "U=" ]]; then
tUN="${args[$i]:2}"
fi
if [[ "${args[$i]:0:2}" == "p=" ]]; then
tPW="${args[$i]:2}"
fi
if [[ "${args[$i]:0:2}" == "D=" ]]; then
DB="${args[$i]:2}"
fi
if [[ "${args[$i]:0:2}" == "H=" ]]; then
tHN="${args[$i]:2}"
fi
if [[ "${args[$i]:0:2}" == "P=" ]]; then
tHP="${args[$i]:2}"
fi
if [[ "${args[$i]:0:7}" == "--mode=" ]]; then
MODE="${args[$i]:7}"
fi
done
[[ -z "$tUN" ]] && tUN=${MYSQL_USER}
[[ -z "$tPW" ]] && tPW=${MYSQL_PASSWORD}
[[ -z "$DB" ]] && DB=${MYSQL_DATABASE}
[[ -z "$tHN" ]] && tHN=${MYSQL_HOST}
[[ -z "$tHP" ]] && tHP=${MYSQL_PORT}
[[ -n "$tUN" ]] && UN="-u${tUN}"
[[ -n "$tPW" ]] && PW="-p${tPW}"
# no DB alterations
[[ -n "$tHN" ]] && HN="-h ${tHN}"
[[ -n "$tHP" ]] && HP="-P${tHP}"
function sqlexec() {
RES=$( mysql ${UN} ${PW} ${DB} ${HN} ${HP} -Nse "${@}" 2> >( awk "! /${FILTER_ERR}/") )
if [[ 0 -ne $? ]]; then
echo "mysql ${UN} ${DB} ${HN} ${HP} -Nse "
echo " '${@}'"
exit 1
fi
echo "$RES" | sort -u
}
TABLES=$(sqlexec "show tables;" )
err_chk $? "${TABLES}"
if [[ "$MODE" == "yml" ]]; then
echo "---"
for T in $TABLES; do
echo "${T}:"
RES=$(sqlexec "show columns from ${T};")
err_chk $? "${RES}"
echo "${RES}" | awk '{print " - "$1}'
done
else
for T in $TABLES; do
RES=$(sqlexec "show columns from ${T};")
err_chk $? "${RES}"
echo "${RES}" | awk '{print $1}' | sed "s/^/$T./g"
done
fi