#!/usr/bin/env bash working_dir="$(dirname $(realpath $0))" tmp_log_file="$(mktemp)" # ------------- # - Variable settings # ------------- MARIADB_SERVICE_FILE="mariadb.service" MARIADB_SYSV_INIT_SCRIPT="mysql.server" _mysql_bin="/usr/local/mysql/bin/mysql" # ------------- # --- Some functions # ------------- clean_up() { # Perform program exit housekeeping rm -f $tmp_log_file exit $1 } echononl(){ 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$$ } fatal(){ echo "" 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" echo "" clean_up 1 } error(){ echo "" echo -e " [ \033[31m\033[1mError\033[m ]: $*" echo "" } warn (){ echo "" echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" echo "" } info (){ echo "" echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" echo "" } echo_ok() { echo -e "\033[80G[ \033[32mok\033[m ]" } echo_failed(){ echo -e "\033[80G[ \033[1;31mfailed\033[m ]" } echo_skipped() { echo -e "\033[80G[ \033[37mskipped\033[m ]" } trap clean_up SIGHUP SIGINT SIGTERM clear echo "" echo "" echo -e " \033[32m\033[1m===========================\033[m" echo " Remove MariaDB Installation" echo -e " \033[32m\033[1m===========================\033[m" echo "" echo "" echo -n " Remove also the database directory? [yes/no]: " read OK if [[ "$(echo "${OK,,}")" = "yes" ]] ; then remove_data_dir=true else remove_data_dir=false fi echo "" if $remove_data_dir ; then warn "This will also remove the MariaDB Data Directory!" else info "The MariaDB Data Directory will be left untouched." fi echo -e " \033[32m--\033[m" echo "" echo -e -n " Type \033[1muppercase 'YES'\033[m to continue:\033[m " read OK if [[ "$OK" != "YES" ]] ; then echo "" echo -e " \033[32m--\033[m" fatal "Abort by user request - Answer as not 'YES'" else echo "" echo -e " \033[32m--\033[m" echo "" fi _MARIADB_INITSCRIPT= if [ -f "/etc/init.d/$MARIADB_SYSV_INIT_SCRIPT" ];then _MARIADB_INITSCRIPT="${MARIADB_SYSV_INIT_SCRIPT}" elif [ -f /etc/init.d/mysql.server ];then _MARIADB_INITSCRIPT="mysql.server" elif [ -f /etc/init.d/mysql ];then _MARIADB_INITSCRIPT="mysql" fi _SYSTEMD_SERVICE_FILE= if [[ -f "/etc/systemd/system/$MARIADB_SERVICE_FILE" ]]; then _SYSTEMD_SERVICE_FILE="${MARIADB_SERVICE_FILE}" elif [[ -f /etc/systemd/system/mysql.service ]] ; then _SYSTEMD_SERVICE_FILE="mysql.service" elif [[ -f /etc/systemd/system/mysqld.service ]] ; then _SYSTEMD_SERVICE_FILE="mysqld.service" fi echo "" echononl " Determine installed version number.." if [[ -x "$_mysql_bin" ]]; then CURRENT_VERSION="$($_mysql_bin -V | grep -o -E "Distrib\s+[0-9]+\.[0-9]+\.[0-9]+" | awk '{print$2}')" if [[ -n "$CURRENT_VERSION" ]] ; then echo_ok else echo_failed fi else CURRENT_VERSION="" echo_skipped fi echo "" _found=false if [[ -f "/etc/init.d/$_MARIADB_INITSCRIPT" ]] || [[ -h "$_MARIADB_INITSCRIPT" ]] ; then echononl " Stop MariaDB service.." /etc/init.d/$_MARIADB_INITSCRIPT stop > $tmp_log_file 2>&1 if [[ $? -ne 0 ]] ; then echo_failed error "$(cat $tmp_log_file)" else echo_ok fi echononl " Disable MariaDB service.." update-rc.d -f $_MARIADB_INITSCRIPT remove > $tmp_log_file 2>&1 if [[ $? -ne 0 ]] ; then echo_failed error "$(cat $tmp_log_file)" else echo_ok fi echononl " Remove '/etc/init.d/$_MARIADB_INITSCRIPT'.." rm "/etc/init.d/$_MARIADB_INITSCRIPT" > $tmp_log_file 2>&1 if [[ $? -ne 0 ]] ; then echo_failed error "$(cat $tmp_log_file)" else echo_ok fi _found=true elif [[ -f "/etc/systemd/system/$_SYSTEMD_SERVICE_FILE" ]] ; then echononl " Stop MariaDB service.." systemctl stop $_SYSTEMD_SERVICE_FILE > $tmp_log_file 2>&1 if [[ $? -ne 0 ]] ; then echo_failed error "$(cat $tmp_log_file)" else echo_ok fi echononl " Disable MariaDB service.." systemctl disable $_SYSTEMD_SERVICE_FILE > $tmp_log_file 2>&1 if [[ $? -ne 0 ]] ; then echo_failed error "$(cat $tmp_log_file)" else echo_ok fi echononl " Remove '/etc/systemd/system/$_SYSTEMD_SERVICE_FILE'.." rm "/etc/systemd/system/$_SYSTEMD_SERVICE_FILE" if [[ $? -ne 0 ]] ; then echo_failed error "$(cat $tmp_log_file)" else echo_ok fi _found=true else warn "No SysV Initscript or Systemd Service File found." fi echo "" _failed=false echononl " Remove MariaDB data directory" if $remove_data_dir ; then if [[ -h "/data/mysql" ]]; then _real_dir="$(realpath /data/mysql)" rm "/data/mysql" > $tmp_log_file 2>&1 if [[ $? -ne 0 ]]; then _failed=true echo_failed error " rm \"/data/mysql\": \n$(cat $tmp_log_file)" fi if [[ -d "${_real_dir}" ]]; then rm -r "${_real_dir}" > $tmp_log_file 2>&1 if [[ $? -ne 0 ]]; then if ! $_failed ; then echo_failed _failed=true fi error " rm -r \"${_real_dir}\": \n$(cat $tmp_log_file)" fi fi if ! $_failed ; then echo_ok fi else echo_skipped fi fi _failed=false echononl " Remove MariaDB installation directory" if [[ -h "/usr/local/mysql" ]]; then _real_dir="$(realpath "/usr/local/mysql")" rm "/usr/local/mysql" > $tmp_log_file 2>&1 if [[ $? -ne 0 ]]; then _failed=true echo_failed error " rm \"/usr/local/mysql\": \n$(cat $tmp_log_file)" fi if [[ -d "${_real_dir}" ]]; then rm -r "${_real_dir}" > $tmp_log_file 2>&1 if [[ $? -ne 0 ]]; then if $_failed ; then echo_failed _failed=true fi error " rm -r \"${_real_dir}\": \n$(cat $tmp_log_file)" fi fi if ! $_failed ; then echo_ok fi else echo_skipped fi if [[ -n "$CURRENT_VERSION" ]] ; then echo "" echononl " Remove directory '${working_dir}/log-${CURRENT_VERSION}'.." if [[ -d "${working_dir}/log-${CURRENT_VERSION}" ]] ; then rm -r ${working_dir}/log-${CURRENT_VERSION} > $tmp_log_file 2>&1 if [[ $? -ne 0 ]] ; then echo_failed error "$(cat $tmp_log_file)" else echo_ok fi else echo_skipped fi echononl " Remove directory '${working_dir}/mariadb-${CURRENT_VERSION}'.." if [[ -d "${working_dir}/mariadb-${CURRENT_VERSION}" ]] ; then rm -r ${working_dir}/mariadb-${CURRENT_VERSION} > $tmp_log_file 2>&1 if [[ $? -ne 0 ]] ; then echo_failed error "$(cat $tmp_log_file)" else echo_ok fi else echo_skipped fi fi echo "" clean_up 0