mb-admin/scripts/dbPreparation.sh

117 lines
4.0 KiB
Bash

#!/bin/bash
# Source the utility script
source "$(dirname "$0")/utils.sh"
# Automatically generate a new secure password for the root user
new_root_password=$(openssl rand -base64 12)
# Generate random database name, user, and password for the new database
DB_NAME="db_$(openssl rand -hex 4)"
DB_USER="user_$(openssl rand -hex 4)"
DB_PASSWORD="$(openssl rand -base64 12)"
DB_HOST="127.0.0.1" # Change if your database is hosted elsewhere
echo "New root password will be: $new_root_password"
echo "New database credentials:"
echo "Database Name: $DB_NAME"
echo "Database User: $DB_USER"
echo "Database Password: $DB_PASSWORD"
echo "Attempting to stop the MariaDB service..."
# Stop the MariaDB service
sudo systemctl stop mariadb
echo "Starting MariaDB in safe mode..."
# Start MariaDB in safe mode with no networking and no grants
sudo mysqld_safe --skip-grant-tables --skip-networking &
# Wait for MariaDB to fully start in safe mode
sleep 5
echo "Resetting the root password..."
# Reset the root password in safe mode
sudo mysql -u root <<EOF
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY '$new_root_password';
ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY '$new_root_password';
FLUSH PRIVILEGES;
EOF
# Check if the password reset was successful
if [ $? -eq 0 ]; then
echo "Root password reset successful."
else
echo "Failed to reset the root password. Exiting."
sudo pkill -f mariadbd
sudo pkill -f mysqld_safe
exit 1
fi
echo "Stopping the MariaDB safe mode process..."
# Terminate the MariaDB safe mode processes
sudo pkill -f mysqld_safe
sleep 2
sudo pkill -f mariadbd
sleep 2
echo "Starting the MariaDB service normally..."
# Start the MariaDB service normally
sudo systemctl start mariadb
# Check if MariaDB started successfully
if sudo systemctl is-active --quiet mariadb; then
echo "MariaDB service is running. Root password has been reset to '$new_root_password'."
else
echo "Failed to start MariaDB service. Please check the service status manually."
exit 1
fi
# Introduce a delay to allow MariaDB to fully initialize
sleep 5
# Create MySQL database and user with the new root password
echo "Creating MySQL database and user with the new root password..."
mysql -u root -p"$new_root_password" <<EOF
CREATE DATABASE ${DB_NAME};
CREATE USER '${DB_USER}'@'${DB_HOST}' IDENTIFIED BY '${DB_PASSWORD}';
GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'${DB_HOST}' IDENTIFIED BY '${DB_PASSWORD}';
FLUSH PRIVILEGES;
EOF
# Check if the database and user creation was successful
if [ $? -eq 0 ]; then
echo "Database ${DB_NAME} and user ${DB_USER} created successfully with the specified password."
else
echo "Failed to create database or user. Please check the MySQL status manually."
exit 1
fi
# Backup the wp-config.php file before making changes
WP_CONFIG="/var/www/webroot/ROOT/wp-config.php"
if [ ! -f "$WP_CONFIG" ]; then
echo "Error: wp-config.php not found at $WP_CONFIG. Please ensure WordPress is installed correctly."
exit 1
fi
sudo cp $WP_CONFIG $WP_CONFIG.bak
echo "Updating wp-config.php with new database credentials..."
# Update wp-config.php with new database credentials using the reusable function
search_and_replace "define( *'DB_NAME', '.*' *);" "define('DB_NAME', '${DB_NAME}');" "$WP_CONFIG"
search_and_replace "define( *'DB_USER', '.*' *);" "define('DB_USER', '${DB_USER}');" "$WP_CONFIG"
search_and_replace "define( *'DB_PASSWORD', '.*' *);" "define('DB_PASSWORD', '${DB_PASSWORD}');" "$WP_CONFIG"
search_and_replace "define( *'DB_HOST', '.*' *);" "define('DB_HOST', '${DB_HOST}');" "$WP_CONFIG"
# Check if wp-config.php was updated successfully
if grep -q "define( *'DB_NAME', '${DB_NAME}' *);" "$WP_CONFIG" && \
grep -q "define( *'DB_USER', '${DB_USER}' *);" "$WP_CONFIG" && \
grep -q "define( *'DB_PASSWORD', '${DB_PASSWORD}' *);" "$WP_CONFIG"; then
echo "wp-config.php updated successfully with new database credentials."
else
echo "Failed to update wp-config.php. Please check the file manually."
exit 1
fi