From a842cf59067830dd9d7ece82448ff20b5a9f0a2b Mon Sep 17 00:00:00 2001 From: Christoph Date: Sun, 24 Jun 2018 15:40:19 +0200 Subject: [PATCH] optimize_mysql_tables.sh: if a database is given at the command line, only check tables of this database. --- optimize_mysql_tables.sh | 64 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/optimize_mysql_tables.sh b/optimize_mysql_tables.sh index 2b3708d..bf366ca 100755 --- a/optimize_mysql_tables.sh +++ b/optimize_mysql_tables.sh @@ -16,6 +16,42 @@ DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local" # --- Some functions # ------------- +usage() { + + [[ -n "$1" ]] && error "$1" + + [[ $terminal ]] && echo -e " +\033[1mUsage:\033[m + + $(basename $0) [DB-Name] + +\033[1mDescription\033[m + + Script checks (and reorganizes) all tables of all databases by executing MySQL + command 'OPTIMIZE TABLE'. + + If a database is given at the command line, only tables of that database + will be checked. + + If a check on a table fails, MySQL command 'REPAIR TABLE' will be executed on + that table. + +\033[1mOptions\033[m + + No Options available + +\033[1mFiles\033[m + + $conf_file: Configuration file + + +" + + clean_up 1 + + +} + clean_up() { @@ -108,14 +144,18 @@ echo_skipped() { fi } +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + + trap clean_up SIGHUP SIGINT SIGTERM - - - - # - Is this script running on terminal ? # - if [[ -t 1 ]] ; then @@ -135,11 +175,23 @@ if [ -z "$mysql" ]; then fi +# - Print help? +# - +if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then + usage +fi + # ------------- # - Load Settings from configuration file # ------------- +if [[ -n "$1 " ]] ; then + DATABASES="$1" +else + DATABASES="" +fi + if $terminal ; then echo "" fi @@ -183,7 +235,9 @@ for _val in ${mysql_credential_args_arr[@]} ; do echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: optimize (and repair) tables of databases at host '$(hostname -f)'." fi - DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"` + if [[ -z "$DATABASES" ]] ; then + DATABASES="$($mysql $mysql_credential_args -N -s -e "show databases")" + fi length_table_name=0 for db in $DATABASES ; do