HIRS/package/scripts/db/db_drop.sh

63 lines
1.9 KiB
Bash
Raw Normal View History

#!/bin/bash
2018-09-06 13:47:33 +00:00
2023-10-10 18:29:37 +00:00
DB_SRV_CONF=/etc/my.cnf.d/mariadb-server.cnf
DB_CLIENT_CONF=/etc/my.cnf.d/client.cnf
2023-08-11 13:47:30 +00:00
SCRIPT_DIR=$( dirname -- "$( readlink -f -- "$0"; )";)
2023-08-23 20:30:06 +00:00
LOG_FILE=/dev/null
2023-09-18 20:48:29 +00:00
DB_ADMIN_PWD=$1
2023-08-11 13:47:30 +00:00
2023-09-18 20:48:29 +00:00
#source /etc/hirs/aca/aca.properties;
source $SCRIPT_DIR/mysql_util.sh
2023-10-10 18:29:37 +00:00
source /etc/os-release
2023-08-23 20:30:06 +00:00
# Check for sudo or root user, not actually needed but a good idea
if [ "$EUID" -ne 0 ]
then echo "This script requires root. Please run as root"
exit 1
fi
2023-10-10 18:29:37 +00:00
# Setup distro specifc paths and variables
if [ $ID = "ubuntu" ]; then
DB_SRV_CONF="/etc/mysql/mariadb.conf.d/50-server.cnf"
DB_CLIENT_CONF="/etc/mysql/mariadb.conf.d/50-client.cnf"
fi
2023-08-23 20:30:06 +00:00
if [ -d /opt/hirs/scripts/db ]; then
2023-08-11 13:47:30 +00:00
MYSQL_DIR="/opt/hirs/scripts/db"
else
MYSQL_DIR="$SCRIPT_DIR"
fi
2023-08-23 20:30:06 +00:00
echo "dropping hirs_db database"
2023-05-03 16:54:35 +00:00
if pgrep mysqld >/dev/null 2>&1; then
2023-09-18 20:48:29 +00:00
mysql -u root --password=$DB_ADMIN_PWD -e "FLUSH HOSTS; FLUSH LOGS; FLUSH STATUS; FLUSH PRIVILEGES; FLUSH USER_RESOURCES"
2023-08-28 20:18:08 +00:00
mysql -u root --password=$DB_ADMIN_PWD -e "DROP USER 'hirs_db'@'localhost';"
mysql -u root --password=$DB_ADMIN_PWD -e "DROP DATABASE IF EXISTS hirs_db;"
echo "hirs_db database and hirs_db user removed"
2023-08-23 20:30:06 +00:00
else
2023-08-28 20:18:08 +00:00
echo "mysql is not running. DB was not removed."
fi
2023-08-23 20:30:06 +00:00
# reset the mysql root if the password was left in the properties fiel
2023-09-18 20:48:29 +00:00
if [ ! -z $DB_ADMIN_PWD ]; then
2023-08-23 20:30:06 +00:00
echo "Resetting mysql root password to empty"
2023-09-18 20:48:29 +00:00
mysql -u root --password=$DB_ADMIN_PWD -e "SET PASSWORD FOR "root@localhost" = PASSWORD('');"
2023-08-28 20:18:08 +00:00
mysql -u "root" -e "FLUSH LOGS;"
2023-08-23 20:30:06 +00:00
else
echo "Note root password was NOT reset"
fi
# Remove key , cert and truststore entries from client.cnf andf mariadb.cnf
echo "Removing hirs cert references from mariadb configuration files"
2023-10-10 18:29:37 +00:00
grep -v "hirs" $DB_SRV_CONF > tmpfile && mv tmpfile $DB_SRV_CONF
grep -v "hirs" $DB_CLIENT_CONF > tmpfile && mv tmpfile $DB_CLIENT_CONF
2023-08-23 20:30:06 +00:00
echo "restarting mariadb"
2023-08-28 20:18:08 +00:00
mysql -u root -e "SHUTDOWN"
sleep 2
2023-08-23 20:30:06 +00:00
check_for_container
start_mysqlsd