myvesta/bin/v-change-database-password-for-wordpress

123 lines
3.9 KiB
Bash

#!/bin/bash
# info: change database password for wordpress
# options:
#
# The command is used for changing database password for wordpress.
#----------------------------------------------------------#
# 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
# Check if number of arguments is 2
if [ $# -eq 2 ]; then
user=$2
else
user=$(/usr/local/vesta/bin/v-search-domain-owner $domain)
fi
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[^']+")
db_user=$(grep "DB_USER" $wp_config_path | grep -oP "define\s*\(\s*'DB_USER'\s*,\s*'\K[^']+")
new_password=''
found_existing_password=0
if [ -f "/root/remember-db-user-pass.txt" ]; then
db_user_pass=$(grep "$db_user:" /root/remember-db-user-pass.txt)
if [ -n "$db_user_pass" ]; then
new_password=$(echo "$db_user_pass" | cut -d':' -f2)
echo "= Using existing password for $db_user"
found_existing_password=1
fi
fi
if [ -z "$new_password" ]; then
new_password=$(generate_password)
fi
echo "DB name: $db_name"
echo "DB user: $db_user"
echo "New DB password: $new_password"
if [ $found_existing_password -eq 0 ] && [ -f "/root/remember-db-user-pass.txt" ]; then
echo "$db_user:$new_password" >> /root/remember-db-user-pass.txt
fi
/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