diff --git a/flush_host_cache.sh b/flush_host_cache.sh index 7246437..e4da7d1 100755 --- a/flush_host_cache.sh +++ b/flush_host_cache.sh @@ -3,6 +3,8 @@ working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/mysql_credetials.conf" +tmp_log_file="$(mktemp)" + #LOGGING=true LOGGING=false @@ -11,6 +13,27 @@ LOGGING=false # --- Some functions # ------------- +clean_up() { + + # Perform program exit housekeeping + rm -f $tmp_log_file + blank_line + exit $1 +} + + +echononl(){ + if $terminal ; 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 +} + fatal(){ echo "" if $terminal ; then @@ -34,6 +57,65 @@ fatal(){ clean_up 1 } +error(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ]: $*" + else + echo " [ Error ]: $*" + fi + echo "" +} + +warn (){ + if $terminal ; then + echo "" + echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" + echo "" + else + echo " [ Warning ]: $*" + fi +} + +info (){ + if $terminal ; then + echo "" + echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" + echo "" + else + echo " [ Info ]: $*" + fi +} + +echo_ok() { + if $terminal ; then + echo -e "\033[80G[ \033[32mok\033[m ]" + fi +} +echo_failed(){ + if $terminal ; then + echo -e "\033[80G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal ; then + echo -e "\033[80G[ \033[37mskipped\033[m ]" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + detect_mysql_version () { _MYSQLD_VERSION="$(mysqld -V 2>/dev/null)" @@ -87,16 +169,23 @@ fi #----------------------------- #--------------------------------------- +blank_line +echononl " Loading configuration settings from $(basename ${conf_file}).." if [[ -f "$conf_file" ]]; then - source "$conf_file" + 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 + if $terminal ;then + warn "No Configuration File found. Loading defaults.." + fi fi -#[[ -z "$mysql_credential_args" ]] && mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf" - - -if $LOGGING ;then - echo -e "\n[ `date` ] Going to flush host cache.." -fi if [[ -z "$mysql_credential_args" ]]; then @@ -135,14 +224,30 @@ if [[ -z "$mysql_credential_args" ]]; then fi -# Flush host cache -# -$mysqladmin $mysql_credential_args flush-hosts - -[[ $? -gt 0 ]] && echo -e "\t[ Error ]: Flushing host cache failed !!" - -if $LOGGING ;then - echo -e "\n[ `date` ] End flushing host cache" +if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then + mysql_credential_args_arr[0]="default:$mysql_credential_args" fi -exit 0 +blank_line +declare -i index_arr=0 +while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do + + IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}" + mysql_version="${_val_arr[0]}" + mysql_credential_args="${_val_arr[1]}" + + echononl " [ ${mysql_version} ]: Flush host cache.." + $mysqladmin $mysql_credential_args flush-hosts + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $tmp_log_file)" + fi + + (( index_arr++ )) + +done + + +clean_up 0 diff --git a/flush_query_cache.sh b/flush_query_cache.sh index 61b215e..126d44e 100755 --- a/flush_query_cache.sh +++ b/flush_query_cache.sh @@ -3,8 +3,151 @@ working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/mysql_credetials.conf" -#LOGGING=true -LOGGING=false +tmp_log_file="$(mktemp)" + + +# ------------- +# --- Some functions +# ------------- + +clean_up() { + + # Perform program exit housekeeping + rm -f $tmp_log_file + blank_line + exit $1 +} + + +echononl(){ + if $terminal ; 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 +} + +fatal(){ + echo "" + if $terminal ; then + if [[ -n "$*" ]] ; then + echo -e " [ \033[31m\033[1mFatal\033[m ]: $*" + echo "" + echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m" + else + echo -e " \033[31m\033[1mFatal error\033[m: \033[1mScript will be interrupted.\033[m" + fi + else + if [[ -n "$*" ]] ; then + echo " [ Fatal ]: $*" + echo "" + echo " Script was terminated.." + else + echo " Fatal error: Script was terminated.." + fi + fi + echo "" + clean_up 1 +} + + +error(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ]: $*" + else + echo " [ Error ]: $*" + fi + echo "" +} + +warn (){ + if $terminal ; then + echo "" + echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" + echo "" + else + echo " [ Warning ]: $*" + fi +} + +info (){ + if $terminal ; then + echo "" + echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" + echo "" + else + echo " [ Info ]: $*" + fi +} + +echo_ok() { + if $terminal ; then + echo -e "\033[80G[ \033[32mok\033[m ]" + fi +} +echo_failed(){ + if $terminal ; then + echo -e "\033[80G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal ; then + echo -e "\033[80G[ \033[37mskipped\033[m ]" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + +detect_mysql_version () { + + _MYSQLD_VERSION="$(mysqld -V 2>/dev/null)" + + if [[ -z "$_MYSQLD_VERSION" ]]; then + fatal "No installed MySQL server or distribution found!" + elif [[ "$_MYSQLD_VERSION" =~ MariaDB ]]; then + MYSQL_CUR_DISTRIBUTION="MariaDB" + elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ percona- ]]; then + MYSQL_CUR_DISTRIBUTION="Percona" + elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mysql- ]]; then + MYSQL_CUR_DISTRIBUTION="MySQL" + fi + + MYSQL_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)" + MYSQL_MAJOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1)" + MYSQL_MINOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f2)" + MYSQL_PATCH_LEVEL="$(echo $MYSQL_VERSION | cut -d '.' -f3)" + MYSQL_MAIN_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1,2)" + +} + + + + +# - Is this script running on terminal ? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + mysql=`realpath $(which mysql) 2>/dev/null` if [ -z "$mysql" ]; then @@ -18,41 +161,6 @@ if [ -z "$mysql" ]; then fi fi -# Get current version -# -_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)" -CURRENT_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)" - -MYSQL_MAIN_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f1,2` -MYSQL_MAJOR_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f1` -MYSQL_MINOR_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f2` -MYSQL_PATCH_LEVEL=`echo $CURRENT_VERSION | cut -d '.' -f3` - -if [[ -z "$_MYSQLD_VERSION" ]]; then - echo "" - echo -e "\t[ Error ]: No installed MySQL server or distribution found!" - echo "" -elif [[ "$_MYSQLD_VERSION" =~ MariaDB ]]; then - MYSQL_CUR_DISTRIBUTION="MariaDB" -elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ percona- ]]; then - MYSQL_CUR_DISTRIBUTION="Percona" -elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mysql- ]]; then - MYSQL_CUR_DISTRIBUTION="MySQL" -fi - -if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]]; then - if [[ "$MYSQL_MAJOR_VERSION" -gt 8 ]] \ - || ( [[ "$MYSQL_MAJOR_VERSION" -eq 8 ]] && [[ "$MYSQL_MINOR_VERSION" -gt 0 ]] ) \ - || ( [[ "$MYSQL_MAJOR_VERSION" -eq 8 ]] && [[ "$MYSQL_MINOR_VERSION" -eq 0 ]] \ - && [[ $MYSQL_PATCH_LEVEL -ge 3 ]] ); then - echo "" - echo -e "\t[ Error ]: Query cache is no longer supported since (MySQL 8.0.3)" - echo "" - exit - fi -fi - - #--------------------------------------- #----------------------------- @@ -60,16 +168,35 @@ fi #----------------------------- #--------------------------------------- +blank_line +echononl " Loading configuration settings from $(basename ${conf_file}).." if [[ -f "$conf_file" ]]; then - source "$conf_file" + 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 + if $terminal ;then + warn "No Configuration File found. Loading defaults.." + fi fi -#[[ -z "$mysql_credential_args" ]] && mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf" if [[ -z "$mysql_credential_args" ]]; then - if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MYSQL_MAJOR_VERSION -gt 10 ]] \ - || ( [[ $MYSQL_MAJOR_VERSION -eq 10 ]] && [[ $MYSQL_MINOR_VERSION -gt 3 ]] )) ; then + detect_mysql_version + + + MAJOR_VERSION="$MYSQL_MAJOR_VERSION" + MINOR_VERSION="$MYSQL_MINOR_VERSION" + PATCH_LEVEL="$MYSQL_PATCH_LEVEL" + + if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ + || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then if [[ -S "/tmp/mysql.sock" ]]; then mysql_credential_args="-u root -S /tmp/mysql.sock" elif [[ -S "/run/mysqld/mysqld.sock" ]]; then @@ -94,24 +221,34 @@ if [[ -z "$mysql_credential_args" ]]; then parameter manually." fi fi - fi - -if $LOGGING ;then - echo -e "\n[ `date` ] Going to flush query cache.." +if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then + mysql_credential_args_arr[0]="default:$mysql_credential_args" fi -# Flush Query Cache -# -$mysql $mysql_credential_args -N -s -e "FLUSH QUERY CACHE" +blank_line +declare -i index_arr=0 +while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do -[[ $? -gt 0 ]] && echo -e "\t[ Error ]: Flushing query cache failed !!" + IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}" + mysql_version="${_val_arr[0]}" + mysql_credential_args="${_val_arr[1]}" -if $LOGGING ;then - echo -e "\n[ `date` ] End flushing query cache" -fi + echononl " [ ${mysql_version} ]: Flush query cache.." + $mysql $mysql_credential_args -N -s -e "FLUSH QUERY CACHE" > $tmp_log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $tmp_log_file)" + fi -exit 0 + (( index_arr++ )) + +done + + +clean_up 0