new wp-update-config-db-info.sh script

This commit is contained in:
Matthew Saunders Brown 2023-06-19 11:41:12 -07:00
parent f89b543c1b
commit 44fbf02a42

261
bin/wp-update-config-db-info.sh Executable file
View File

@ -0,0 +1,261 @@
#!/bin/bash
#
# wordpress-tools
# https://git.stack-source.com/msb/wordpress-tools
# Copyright (c) 2023 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>\
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# require root
if [ "${EUID}" -ne 0 ]; then
echo "This script must be run as root"
exit
fi
virtualhost_basedir="/srv/www"
virtualhost_htdocsdir="html"
declare -a input_keys_index
input_keys_index=(s d u p x)
declare -A input_keys_array
input_keys_array[s]="MySQL Hostname (Server)"
input_keys_array[d]="MySQL Database"
input_keys_array[u]="MySQL Username"
input_keys_array[p]="MySQL Password"
input_keys_array[x]="MySQL Table Prefix"
declare -A input_values_array
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename - Update wp-config.php DB info."
echo ""
echo "Usage: $thisfilename [-v <virtualhost>] [-s <hostname>] [-d <database>] [-u <username>] [-p <password>] [-x <table_prefix>]"
echo ""
echo " -v <virtualhost> VirtualHost of WordPress config that will be updated. If not set attempt to autodect based on current/working dir."
echo " -s <hostname> MySQL Hostname, defaults to 127.0.0.1"
echo " -d <database> MySQL Database, defaults to 'VirtualHost' with dots & dashes changed from symbols to words if not specified."
echo " -u <username> MySQL Username, defaults to owner of html dir if not specified."
echo " -p <password MySQL Password, random password is generated if not set."
echo " -x <table_prefix> WordPress db table prefix, defaults to wp_"
echo " -f Force update - skips confirmation step, for automation."
echo " -h Print this help."
echo ""
echo " You will be prompted to any options that were not"
echo " specified on the command line and given a chance"
echo " to confirm before performing the install unless the"
echo " -f option is specified."
exit
}
# force (-f) defaults to false
force=false
# set any options that were passed
while getopts "v:s:d:u:p:x:fh" opt; do
case "${opt}" in
h )
help
exit;;
v )
input_values_array[v]=${OPTARG}
;;
s )
input_values_array[s]=${OPTARG}
;;
d )
input_values_array[d]=${OPTARG}
;;
u )
input_values_array[u]=${OPTARG}
;;
p )
input_values_array[p]=${OPTARG}
;;
x )
input_values_array[x]=${OPTARG}
;;
f )
force=true
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit 1;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1;;
esac
done
# v - virtualhost
function validate_v() {
if [[ -d "$virtualhost_basedir/${input_values_array[v]}" ]]; then
if [[ ! -d "$virtualhost_basedir/${input_values_array[v]}/$virtualhost_htdocsdir" ]]; then
echo "${input_values_array[v]} is installed, but DocumentRoot for ${input_values_array[v]} does not exist."
echo "$virtualhost_basedir/${input_values_array[v]}/$virtualhost_htdocsdir"
exit
fi
else
echo "VirtualHost ${input_values_array[v]} is not installed on this server."
echo "$virtualhost_basedir/${input_values_array[v]}/$virtualhost_htdocsdir"
unset input_values_array[v]
fi
}
if [[ -n "${input_values_array[v]}" ]] ; then
validate_v
else
cwd=`pwd`
echo "cwd = $cwd"
if echo $cwd |grep -q ^/srv/www/ ; then
input_values_array[v]=`echo $cwd |cut -d '/' -f 4`
validate_v
else
while [ -z "${input_values_array[v]}" ]; do
read -p "Enter the VirtualHost of WordPress config " virtualhost
if [ -n "$virtualhost" ] ; then
input_values_array[v]=$virtualhost
validate_v
fi
done
fi
fi
# s - Shell Username
# get shell user value, user can't override
shell_username=$(stat -c '%U' $virtualhost_basedir/${input_values_array[v]})
# m - MySQL Hostname
if [[ -z "${input_values_array[s]}" ]] ; then
if [[ -f "$virtualhost_basedir/${input_values_array[v]}/.my.cnf" ]]; then
input_values_array[s]=`grep host /srv/www/${input_values_array[v]}/.my.cnf |cut -d = -f 2`
else
input_values_array[s]="127.0.0.1"
fi
fi
# d - MySQL Database
if [[ -z "${input_values_array[d]}" ]] ; then
if [[ -f "$virtualhost_basedir/${input_values_array[v]}/.my.cnf" ]]; then
input_values_array[d]=`grep database /srv/www/${input_values_array[v]}/.my.cnf |cut -d = -f 2`
else
mysql_database=${input_values_array[v]}
mysql_database=${mysql_database//./dot}
mysql_database=${mysql_database//-/dash}
input_values_array[d]=$mysql_database
fi
fi
# u - MySQL Username
if [[ -z "${input_values_array[u]}" ]] ; then
if [[ -f "$virtualhost_basedir/${input_values_array[v]}/.my.cnf" ]]; then
input_values_array[u]=`grep user /srv/www/${input_values_array[v]}/.my.cnf |cut -d = -f 2`
else
input_values_array[u]="$shell_username@${input_values_array[v]}"
fi
fi
# p - MySQL Password
if [[ -z "${input_values_array[p]}" ]] ; then
if [[ -f "$virtualhost_basedir/${input_values_array[v]}/.my.cnf" ]]; then
input_values_array[p]=`grep password /srv/www/${input_values_array[v]}/.my.cnf |cut -d = -f 2`
fi
fi
# p - MySQL Password
function validate_p() {
mysql_username_count=`mysql -s -N -e "SELECT COUNT(*) FROM mysql.user WHERE User='${input_values_array[n]}'"`
if [[ "$mysql_username_count" -gt 0 ]]; then
mysql_password_verification=`mysql -s -A -N -e "SELECT COUNT(1) Password_is_OK FROM mysql.user WHERE user='${input_values_array[n]}' AND password=PASSWORD('${input_values_array[w]}')"`
if [[ "$mysql_password_verification" -ne 1 ]]; then
read -p "ERROR: Invalid MySQL Password for existing MySQL User ${input_values_array[n]}, please enter current password: " mysql_password
if [ -n "$mysql_password" ] ; then
input_values_array[p]=$mysql_password
restart_script
fi
fi
fi
}
# x - MySQL DB Prefix
function validate_x() {
if mysql -e "USE ${input_values_array[d]}" 2> /dev/null; then
mysql_db_prefix=`echo ${input_values_array[x]} |sed 's|_|\\\_|g'`
mysql_db_prefix_count=`mysql -s -N -e "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '${input_values_array[d]}' AND TABLE_NAME LIKE '$mysql_db_prefix%'"`
if [[ "$mysql_db_prefix_count" -gt 0 ]]; then
read -p "One or more MySQL tables with DB Prefix '${input_values_array[x]}' already exist, please enter new prefix: " mysql_db_prefix
if [ -n "$mysql_db_prefix" ] ; then
input_values_array[x]=$mysql_db_prefix
restart_script
fi
fi
fi
}
if [ -z "${input_values_array[x]}" ] ; then
input_values_array[x]="wp_"
fi
# display confirmation
function display_confirmation() {
echo
for key in "${!input_keys_index[@]}" ; do
value=${input_keys_index[$key]}
echo "$value - ${input_keys_array[$value]}: ${input_values_array[$value]}"
done
echo
echo "i - install now with these settings"
echo "q - quit without installing"
echo
read -p "Select option to edit or install/quit: " option
echo
}
function restart_script() {
# set command line args
cli_args=
for key in "${!input_values_array[@]}" ; do
value=${input_values_array[$key]}
cli_args="$cli_args -$key '$value'"
done
# echo "${0} $cli_args"
echo
eval ${0} $cli_args
exit
}
if [[ "$force" == "false" ]]; then
display_confirmation
else
option="i"
fi
if [[ "$option" == "i" ]]; then
# validate MySQL DB Prefix
validate_x
# validate MySQL User Password
validate_p
echo
echo "Updating WP config db info for VirtualHost ${input_values_array[v]} now..."
# switch to Shell User and update config
# sudo -u ${input_values_array[s]} --shell /bin/bash << EOF
echo cd $virtualhost_basedir/${input_values_array[v]}/$virtualhost_htdocsdir
echo wp config set DB_HOST ${input_values_array[s]}
echo wp config set DB_NAME ${input_values_array[d]}
echo wp config set DB_USER ${input_values_array[u]}
echo wp config set DB_PASSWORD ${input_values_array[p]}
# EOF
elif [[ "$option" == "q" ]]; then
echo "Quiting without updating config."
elif [[ " ${input_keys_index[@]} " =~ " $option " ]]; then
read -p "Enter new value for ${input_keys_array[$option]} (${input_values_array[$option]}): " new_value
if [ -n "$new_value" ] ; then
input_values_array[$option]=$new_value
fi
restart_script
else
echo "ERROR: Invalid entry, try again."
fi