Add script 'remove_mysql_installation.sh'.

This commit is contained in:
Christoph 2018-02-22 01:55:26 +01:00
parent 7a446ef2ea
commit afe30b381c

330
remove_mysql_installation.sh Executable file
View File

@ -0,0 +1,330 @@
#!/usr/bin/env bash
working_dir="$(dirname $(realpath $0))"
tmp_log_file="$(mktemp)"
# -------------
# - Variable settings
# -------------
MYSQL_SERVICE_FILE="mysqld.service"
MYSQL_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 MYSQL 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 MYSQL Data Directory!"
else
info "The MYSQL 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
_MYSQL_INITSCRIPT=
if [ -f "/etc/init.d/$MYSQL_SYSV_INIT_SCRIPT" ];then
_MYSQL_INITSCRIPT="${MYSQL_SYSV_INIT_SCRIPT}"
elif [ -f /etc/init.d/mysql.server ];then
_MYSQL_INITSCRIPT="mysql.server"
elif [ -f /etc/init.d/mysql ];then
_MYSQL_INITSCRIPT="mysql"
fi
_SYSTEMD_SERVICE_FILE=
if [[ -f "/etc/systemd/system/$MYSQL_SERVICE_FILE" ]]; then
_SYSTEMD_SERVICE_FILE="${MYSQL_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/$_MYSQL_INITSCRIPT" ]] || [[ -h "$_MYSQL_INITSCRIPT" ]] ; then
echononl " Stop MYSQL service.."
/etc/init.d/$_MYSQL_INITSCRIPT stop > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
echononl " Disable MYSQL service.."
update-rc.d -f $_MYSQL_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/$_MYSQL_INITSCRIPT'.."
rm "/etc/init.d/$_MYSQL_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 MYSQL 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 MYSQL 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 MYSQL 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 MYSQL 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}/mysql-${CURRENT_VERSION}'.."
if [[ -d "${working_dir}/mysql-${CURRENT_VERSION}" ]] ; then
rm -r ${working_dir}/mysql-${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