#!/bin/bash # info: change db password to wordpress database # options: # # The command is used for changing db password to wordpress database. #----------------------------------------------------------# # Variable&Function # #----------------------------------------------------------# whoami=$(whoami) if [ "$whoami" != "root" ]; then echo "You must be root to execute this script" exit 1 fi # Importing system environment source /etc/profile # Argument definition domain=$1 user=$(/usr/local/vesta/bin/v-search-domain-owner $domain) USER=$user if [ -z "$user" ]; then echo "ERROR: Domain $domain not found" exit 1; fi if [ ! -d "/home/$user" ]; then echo "ERROR: User $user doesn't exist"; exit 1; fi # Includes source /usr/local/vesta/func/main.sh #----------------------------------------------------------# # Action # #----------------------------------------------------------# check_args '1' "$#" 'DOMAIN' is_format_valid 'domain' is_object_valid 'user' 'USER' "$user" is_object_unsuspended 'user' 'USER' "$user" if [ ! -d "/home/$user/web/$domain/public_html" ]; then echo "ERROR: Domain doesn't exist"; exit 1; fi #----------------------------------------------------------# # Action # #----------------------------------------------------------# if [ -f "/home/$user/web/$domain/public_html/wp-config.php" ]; then echo "=== Domain: $domain" wp_config_path="/home/$user/web/$domain/public_html/wp-config.php" if grep -q $'\r' $wp_config_path; then echo "=== removing CRLF from wp-config.php" tr -d '\r' < $wp_config_path > /tmp/wp-config.php && mv /tmp/wp-config.php $wp_config_path chown $user:$user $wp_config_path fi db_name=$(grep "DB_NAME" $wp_config_path | grep -oP "define\s*\(\s*'DB_NAME'\s*,\s*'\K[^']+") new_password=$(generate_password) echo "DB name: $db_name" echo "New DB password: $new_password" # echo "executing: /usr/local/vesta/bin/v-change-database-password \"$user\" \"$db_name\" \"$new_password\"" /usr/local/vesta/bin/v-change-database-password "$user" "$db_name" "$new_password" if [ $? -ne 0 ]; then echo "*************** ERROR: Failed to change database password ***************" exit 1; fi line="define('DB_PASSWORD', '$new_password');" chattr -i $wp_config_path sed -i "s/.*define(.*DB_PASSWORD'.*/$line/" $wp_config_path new_password_line=$(grep "DB_PASSWORD" $wp_config_path) echo "New DB password line: $new_password_line" if [ "$new_password_line" != "$line" ]; then echo "*************** ERROR: line in wp-config.php is not what we expected ***************" echo "Expected: $line" echo "Actual : $new_password_line" echo "*************** ERROR: Please check wp-config.php manually ***************" exit 1; fi else echo "ERROR: WP-config.php not found" exit 1; fi #----------------------------------------------------------# # Vesta # #----------------------------------------------------------# # Logging log_event "$OK" "$ARGUMENTS" exit