wordpress-tools/bin/wp-create-db-user-from-config.sh

42 lines
1.0 KiB
Bash
Raw Normal View History

2021-04-07 09:43:20 -07:00
#!/bin/bash
#
# wordpress-tools
# https://git.stack-source.com/msb/wordpress-tools
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# require root
if [ "${EUID}" -ne 0 ]; then
echo "This script must be run as root"
exit
fi
VHOST=$1
if [ ! -f /srv/$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/
2021-04-07 09:43:20 -07:00
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\'\;\"