From 959cbf035ffa9b4113982e37dd1d13223432f260 Mon Sep 17 00:00:00 2001 From: olen maszk Date: Tue, 18 Apr 2017 19:50:26 -0700 Subject: [PATCH] initial script --- .gitignore | 3 ++ README.md | 23 ++++++++ mysql_surface-schema-to-yml.sh | 96 ++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 .gitignore create mode 100755 mysql_surface-schema-to-yml.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa807db --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +\#*\# +*~ diff --git a/README.md b/README.md index 6c020f5..0997b6d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,25 @@ # mysql-surface-schema bash script pulling mysql tables and column names, output in yml + +#### from the --help display +usage: mysql_surface-schema-to-yml.sh [-h | --help] [ U=[mysql-user] ] [ p=[mysql-password] ] [ D=[mysql-database] ] [ H=[mysql-host] ] [ P=[mysql-port] ] + + 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): + 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 + unset PW diff --git a/mysql_surface-schema-to-yml.sh b/mysql_surface-schema-to-yml.sh new file mode 100755 index 0000000..3b275a6 --- /dev/null +++ b/mysql_surface-schema-to-yml.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash + +function help() { +cat < Parameter order does not matter, but is case sensitive + + Env Vars (params take precedence): + 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 + unset PW +EOF +} + +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=(${@}) +for ((i=0; i< ${#args[@]}; i++)); do + if [[ "${args[$i]:0:2}" == "U=" ]]; then + UN="${args[$i]:2}" + fi + if [[ "${args[$i]:0:2}" == "p=" ]]; then + PW="${args[$i]:2}" + fi + if [[ "${args[$i]:0:2}" == "D=" ]]; then + DB="${args[$i]:2}" + fi + if [[ "${args[$i]:0:2}" == "H=" ]]; then + HN="${args[$i]:2}" + fi + if [[ "${args[$i]:0:2}" == "P=" ]]; then + HP="${args[$i]:2}" + fi +done + +[[ -z "$UN" ]] && UN=${MYSQL_USER} +[[ -z "$PW" ]] && PW=${MYSQL_PASSWORD} +[[ -z "$DB" ]] && DB=${MYSQL_DATABASE-mysql} +[[ -z "$HN" ]] && HN=${MYSQL_HOST-localhost} +[[ -z "$HP" ]] && HP=${MYSQL_HOST-3306} + + + + +function sqlexec() { + RES=$(mysql -u${UN} -p${PW} ${DB} -h ${HN} -P${HP} -Nse "${@}" 2>/dev/null) + if [[ 0 -ne $? ]]; then + echo "mysql -u${UN} -p ${DB} -h ${HN} -P${HP} -Nse " + echo " '${@}'" + exit 1 + fi + echo "$RES" | sort -u +} + + +TABLES=$(sqlexec "show tables;" ) +err_chk $? "${TABLES}" + +echo "---" +for T in $TABLES; do + echo "${T}:" + RES=$(sqlexec "show columns from ${T};") + err_chk $? "${RES}" + echo "${RES}" | awk '{print " - "$1}' +done +