Updated dbPrep Shell Script

main
Anthony 2024-08-30 19:19:37 +08:00
parent 0e2e11d166
commit b6cc807c70
1 changed files with 10 additions and 10 deletions

View File

@ -3,14 +3,14 @@
# 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)"
# Generate random database name, user, and password for the new WordPress installation
DB_NAME="wp_$(openssl rand -hex 4)"
DB_USER="wp_user_$(openssl rand -hex 4)"
DB_PASSWORD="$(openssl rand -base64 12)"
DB_HOST="localhost" # Change if your database is hosted elsewhere
echo "New root password will be: $new_root_password"
echo "New database credentials:"
echo "New WordPress database credentials:"
echo "Database Name: $DB_NAME"
echo "Database User: $DB_USER"
echo "Database Password: $DB_PASSWORD"
@ -63,8 +63,8 @@ else
exit 1
fi
# Create MySQL database and user with the new root password
echo "Creating MySQL database and user with the new root password..."
# Create WordPress database and user with the new root password
echo "Creating WordPress 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}';
@ -74,9 +74,9 @@ 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."
echo "WordPress 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."
echo "Failed to create WordPress database or user. Please check the MySQL status manually."
exit 1
fi
@ -90,7 +90,7 @@ escaped_db_user=$(printf '%s\n' "$DB_USER" | sed 's:[\/&]:\\&:g')
escaped_db_password=$(printf '%s\n' "$DB_PASSWORD" | sed 's:[\/&]:\\&:g')
escaped_db_host=$(printf '%s\n' "$DB_HOST" | sed 's:[\/&]:\\&:g')
echo "Updating wp-config.php with new database credentials..."
echo "Updating wp-config.php with new WordPress database credentials..."
# Update wp-config.php with new database credentials using more precise sed commands
sudo sed -i.bak -e "s/define( *'DB_NAME'.*/define('DB_NAME', '${escaped_db_name}');/" \
@ -100,7 +100,7 @@ sudo sed -i.bak -e "s/define( *'DB_NAME'.*/define('DB_NAME', '${escaped_db_name}
# Check if wp-config.php was updated successfully
if grep -q "$DB_NAME" "$WP_CONFIG" && grep -q "$DB_USER" "$WP_CONFIG" && grep -q "$DB_PASSWORD" "$WP_CONFIG"; then
echo "wp-config.php updated successfully with new database credentials."
echo "wp-config.php updated successfully with new WordPress database credentials."
else
echo "Failed to update wp-config.php. Please check the file manually."
exit 1