diff --git a/conf/drop_tables.conf.sample b/conf/drop_tables.conf.sample new file mode 100644 index 0000000..5d3ced4 --- /dev/null +++ b/conf/drop_tables.conf.sample @@ -0,0 +1,34 @@ + +## =================================================================== +## - Configuration File for "drop_tables.sh" Script +## =================================================================== + + +# - MYSQL_CREDENTIAL_ARGS +# - +# - MySQL / MariaDB credentials +# - +# - Giving password on command line is insecure an sind mysql 5.5 +# - you will get a warning doing so. +# - +# - Reading username/password fro file ist also possible, using MySQL/MariaDB +# - commandline parameter '--defaults-file'. +# - +# - Since Mysql Version 5.6, you can read username/password from +# - encrypted file. +# - +# - Create (encrypted) option file: +# - $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock --user=root --password +# - $ Password: +# - +# - Use of option file: +# - $ mysql --login-path=local ... +# - +# - Example +# - MYSQL_CREDENTIAL_ARGS="--login-path=local" +# - MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" (Debian default) +# - MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" +# - +# - Defaults to MYSQL_CREDENTIAL_ARGS="--login-path=local" +# - +#MYSQL_CREDENTIAL_ARGS="" diff --git a/drop_tables.sh b/drop_tables.sh new file mode 100755 index 0000000..7d9121b --- /dev/null +++ b/drop_tables.sh @@ -0,0 +1,311 @@ +#!/usr/bin/env bash + +working_dir="$(dirname $(realpath $0))" +log_dir="${working_dir}/log" + +conf_file="${working_dir}/conf/drop_tables.conf" + +tmp_log_file="$(mktemp)" + +# ------------- +# - Variable settings +# ------------- + +#DEFAULT_ACTION='create' +DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local" + +DATABASE_NAME="" +DATABASE_NAME_NEEDED=true +QUIET_MODE=false +NON_INTERACTIVE_MODE=false + + +# ------------- +# --- Some functions +# ------------- + +usage() { + echo + [ -n "$1" ] && echo -e "Error: $1\n" + + cat< + The name of the domain, which is requested for deletion. If not set, script + will ask for the database name. + + -I + Non-interactive mode. Script will act in non-interactice mode. At least the + database name must be given using parameter '-d'. Default is acting in + interactive mode + +EOF +clean_up 1 +} + + +clean_up() { + + # Perform program exit housekeeping + rm -f $tmp_log_file + exit $1 +} + +fatal(){ + echo "" + if $terminal ; then + if [[ -n "$*" ]] ; then + echo -e " [ \033[31m\033[1mFatal\033[m ]: $*" + else + echo " \033[31m\033[1mFatal error\033[m:" + fi + echo "" + echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m" + else + if [[ -n "$*" ]] ; then + echo " [ Fatal ]: $*" + else + echo " Fatal error:" + fi + echo "" + echo " Script was terminated...." + fi + echo "" + clean_up 1 +} + +echononl(){ + if $terminal && ! $QUIET_MODE ; then + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e -n "$*\\c" 1>&2 + else + echo -e -n "$*" 1>&2 + fi + rm /tmp/shprompt$$ + fi +} + +error(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ]: $*" + else + echo "Error: $*" + fi + echo "" +} + +warn (){ + if $terminal && ! $QUIET_MODE ; then + echo "" + echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" + echo "" + fi +} + +info (){ + if $terminal && ! $QUIET_MODE ; then + echo "" + echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" + echo "" + fi +} + +echo_ok() { + if $terminal && ! $QUIET_MODE ; then + echo -e "\033[80G[ \033[32mok\033[m ]" + fi +} +echo_failed(){ + if $terminal && ! $QUIET_MODE ; then + echo -e "\033[80G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal && ! $QUIET_MODE ; then + echo -e "\033[80G[ \033[37mskipped\033[m ]" + fi +} + + +trap clean_up SIGHUP SIGINT SIGTERM + + + + +# ------------- +# - Read Commandline Parameters +# ------------- + +while getopts d:hIqu:U opt ; do + case $opt in + d) DATABASE_NAME="$OPTARG" + DATABASE_NAME_NEEDED=false + ;; + I) NON_INTERACTIVE_MODE=true + ;; + h) usage + ;; + *) usage + esac +done + +shift $(expr $OPTIND - 1) +[[ "$#" -gt 0 ]] && usage "Wrong number of arguments given!" + +# - If not running in a terminal, be silent and non-interactive +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false + QUIET_MODE=true + NON_INTERACTIVE_MODE=true +fi + + +if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then + fatal "In quiet mode the database name must be given on th command line!" +fi + + + +# ------------- +# - Load Settings from configuration file create_drop_database.conf +# ------------- + +if ! $QUIET_MODE ; then + echo "" +fi +echononl " Loading configuration settings from $(basename ${conf_file}).." +if [[ -f "$conf_file" ]]; then + source "$conf_file" > $tmp_log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fatal "$(cat $tmp_log_file)" + fi +else + echo_skipped + warn "No Configuration File found. Loading defaults.." +fi + +[[ -n "$MYSQL_CREDENTIAL_ARGS" ]] || MYSQL_CREDENTIAL_ARGS="$DEFAULT_MYSQL_CREDENTIAL_ARGS" + + +if ! $NON_INTERACTIVE_MODE ; then + + clear + if $DATABASE_NAME_NEEDED ; then + echo "" + echo -e "\033[32m--\033[m" + echo "" + echo "Insert Database name from which all tables should be deleted.." + echo "" + echo "" + echononl "Database name: " + read DATABASE_NAME + while [ "X$DATABASE_NAME" = "X" ] ; do + echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n" + echononl "Database name: " + read DATABASE_NAME + done + fi +fi + +if ! $QUIET_MODE ; then + echo "" + echo "" + echo -e "\033[32m\033[1m====================\033[m" + echo "Settings: Drop all MySQL tables" + echo -e "\033[32m\033[1m====================\033[m" + echo "" + echo " Database name................: $DATABASE_NAME" + echo "" + echo "" +fi + +if ! $NON_INTERACTIVE_MODE ; then + + info "To Continue dropping all tables from \033[1m$DATABASE_NAME\033[m type uppercase 'YES'" + echo -e -n "\033[1mContinue:\033[m " + read OK + + if [[ "$OK" != "YES" ]] ; then + fatal "Abort by user request - Answer as not 'YES'" + fi + +fi + +_result="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")" +if [[ -z "$_result" ]]; then + fatal "No database '$DATABASE_NAME' found!" +fi + + +if ! $QUIET_MODE ; then + echo "" + echo "" + echo -e " Processing database \033[1m$DATABASE_NAME\033[m" + echo "" +fi + +echononl " Get table list from database '$DATABASE_NAME' .." +_TABLES="$(mysql $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "show tables" 2> $tmp_log_file)" +if [[ $? -ne 0 ]] ; then + echo_failed + fatal "$(cat $tmp_log_file)" +else + echo_ok +fi + +if [[ -z "$_TABLES" ]]; then + warn "No tables found in database '$DATABASE_NAME'." + clean_up 0 +fi + +echononl " Drop tables form database '$DATABASE_NAME' .." +_failed=false +> $tmp_log_file +for _table in $_TABLES ; do + mysql $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "DROP TABLE \`$_table\`" >> $tmp_log_file 2>&1 + if [[ $? -ne 0 ]] ; then + _failed=true + _tables_not_deleted+=("$_table") + fi +done +if $_failed ; then + + echo_failed + error "$(cat $tmp_log_file)" + + if [[ ${#_tables_not_deleted[@]} -gt 0 ]] ; then + echo "" + echo "Tables are NOT deleted:" + for _table in "${_tables_not_deleted[@]}" ; do + echo " $_table" + done + echo "" + fi +else + echo_ok +fi + +if ! $QUIET_MODE ; then + echo "" +fi + +clean_up 0