wordpress-tools/bin/wp-create-db-user-from-config.sh
2022-08-22 14:01:24 -07:00

43 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
#
# wordpress-tools
# https://git.stack-source.com/msb/wordpress-tools
# Copyright (c) 2022 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
VHOST=$1
if [ ! -f /srv/www/$VHOST/html/wp-config.php ]; then
echo "Config file /srv/$VHOST/html/wp-config.php does not exist."
exit
fi
# set username
VHOST_USER=$(stat -c '%U' /srv/$VHOST)
cd /srv/www/$VHOST/html/
DB_NAME=`wp --allow-root config get DB_NAME`
DB_USER=`wp --allow-root config get DB_USER`
DB_PASSWORD=`wp --allow-root config get DB_PASSWORD`
DB_HOST=`wp --allow-root config get DB_HOST`
#if [ $DB_HOST == '127.0.0.1' ]; then
# DB_HOST=localhost
#fi
if [ -d /var/lib/mysql/$DB_NAME ]; then
echo "NOTICE: $DB_NAME already exists"
echo " before proceeding do manual checks for db user"
echo
else
echo mysqladmin create $DB_NAME
fi
echo mysql -e \"CREATE USER \'$DB_USER\'@\'$DB_HOST\' IDENTIFIED BY \'$DB_PASSWORD\'\;\"
echo mysql -e \"GRANT ALL PRIVILEGES ON $DB_NAME.* TO \'$DB_USER\'@\'$DB_HOST\'\;\"