diff --git a/bin/my_run_sql.sh b/bin/my_run_sql.sh index 92cfe44..89ba4b2 100755 --- a/bin/my_run_sql.sh +++ b/bin/my_run_sql.sh @@ -3,7 +3,7 @@ ### VARIABLES BASE=$( dirname "$0" ) HOST='' -MY_CNF='' +CONF='' FILES=() @@ -32,9 +32,9 @@ get_files() { prep() { - MY_CNF=~/.my.cnf.${HOST} - [[ -f "${MY_CNF}" ]] || die "File not found '${MY_CNF}'" - rsync "${MY_CNF}" ${HOST}:.my.cnf + CONF=~/.my.cnf.${HOST} + [[ -f "${CONF}" ]] || die "config file not found '${CONF}'" + rsync "${CONF}" ${HOST}:.my.cnf } diff --git a/bin/pg_run_sql.sh b/bin/pg_run_sql.sh new file mode 100755 index 0000000..4ffad2f --- /dev/null +++ b/bin/pg_run_sql.sh @@ -0,0 +1,83 @@ +#!/usr/bin/bash + +### VARIABLES +BASE=$( dirname "$0" ) +HOST='' +CONF='' +FILES=() +PGHOST='' +PGUSER='' +PGPORT='' +PGDB='' + + +### FUNCTIONS + +die() { + echo "ERROR: $*" 1>&2 + kill 0 + exit 99 +} + + +get_host() { + HOST="$1" + [[ -z "$HOST" ]] && die 'Missing host' +} + + +get_files() { + local _fn + for _fn in "${@}"; do + [[ -f "${_fn}" ]] || die "File not found '${_fn}'" + FILES+=( "${_fn}" ) + done + [[ ${#FILES[@]} -lt 1 ]] && die "Missing sql files for host: '$HOST'" +} + + +prep() { + CONF=~/.pgpass.cnf.${HOST} + [[ -f "${CONF}" ]] || die "config file not found '${CONF}'" + rsync "${CONF}" ${HOST}:.pgpass + ssh ${HOST} chmod 600 .pgpass +} + + +get_connection_parameters() { + PGHOST=$( head -1 "${CONF}" | cut -d: -f1 ) + PGPORT=$( head -1 "${CONF}" | cut -d: -f2 ) + PGDB=$( head -1 "${CONF}" | cut -d: -f3 ) + PGUSER=$( head -1 "${CONF}" | cut -d: -f4 ) +} + + +test_connection() { + ssh $HOST "pg_isready -q -h ${PGHOST} -p ${PGPORT} -d ${PGDB} -U ${PGUSER}" +} + + +run_sql() { + fn="$1" + # cat "$fn" | ssh $HOST 'psql -d jsmdb -h localhost -U jsmdb_user' + cat "$fn" | ssh $HOST "psql -h ${PGHOST} -p ${PGPORT} -d ${PGDB} -U ${PGUSER}" +} + + +### MAIN +get_host "$1" +shift +get_files "${@}" + +prep + +get_connection_parameters + +test_connection || die "connection not ready" + +for fn in "${FILES[@]}"; do + set -x + run_sql "$fn" + set +x + echo +done diff --git a/cleanup/pg_run_sql.sh b/cleanup/pg_run_sql.sh deleted file mode 100755 index bdd924d..0000000 --- a/cleanup/pg_run_sql.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/bash - -### VARIABLES -BASE=$( dirname "$0" ) - - -### FUNCTIONS - -die() { - echo "ERROR: $*" 1>&2 - exit 99 -} - -prep() { - rsync "$BASE"/pgpass.cnf ${HOST}:.pgpass - ssh ${HOST} chmod 600 .pgpass -} - - -test_conn() { - ssh $HOST 'pg_isready -d jsmdb -h localhost -U jsmdb_user' -} - - -run_sql() { - fn="$1" - cat "$fn" | ssh $HOST 'psql -d jsmdb -h localhost -U jsmdb_user' -} - - -### MAIN -HOST="$1" -[[ -z "$HOST" ]] && die "Missing hostname" - -FN="$2" -[[ -z "$FN" ]] && die "Missing filename" -case "$FN" in - (/*) SQL="$FN" ;; - (*) SQL="${BASE}/${FN}" ;; -esac -[[ -r "$SQL" ]] || die "Cannot read file '$SQL'" - -prep - -test_conn || die "connection not ready" - -run_sql "$SQL" diff --git a/wiki_cleanup/ls_views.sql b/wiki_cleanup/ls_views.sql new file mode 100644 index 0000000..a2e61e0 --- /dev/null +++ b/wiki_cleanup/ls_views.sql @@ -0,0 +1,3 @@ +/* https://stackoverflow.com/questions/51246974/error-is-not-base-table-on-repairing-corrupted-table */ + +SHOW FULL TABLES in ncsa_wiki WHERE TABLE_TYPE LIKE 'VIEW'; diff --git a/wiki_cleanup/pg_run_sql.sh b/wiki_cleanup/pg_run_sql.sh new file mode 120000 index 0000000..2d396a7 --- /dev/null +++ b/wiki_cleanup/pg_run_sql.sh @@ -0,0 +1 @@ +../bin/pg_run_sql.sh \ No newline at end of file