2021-04-04 13:28:22 -07:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# vhost-stack
|
|
|
|
# https://git.stack-source.com/msb/vhost-stack
|
|
|
|
# MIT License Copyright (c) 2021 Matthew Saunders Brown
|
|
|
|
|
2021-04-04 14:15:16 -07:00
|
|
|
# load include file
|
|
|
|
source $(dirname $0)/vhost.sh
|
2021-04-04 13:28:22 -07:00
|
|
|
|
2021-09-16 16:21:35 -07:00
|
|
|
help()
|
|
|
|
{
|
|
|
|
thisfilename=$(basename -- "$0")
|
2022-07-18 19:11:44 -07:00
|
|
|
echo "Creates MySQL database and db user for specified virtualhost."
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "usage: $thisfilename -d <domain> [OPTIONS]"
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
2022-07-18 19:11:44 -07:00
|
|
|
echo " -d <domain> Domain name of VirtualHost to add db for."
|
2021-10-05 11:33:24 -07:00
|
|
|
echo " -u <username> Username for accessing the database. Optional, autogenerated if none specified."
|
|
|
|
echo " -p <password> Password for username. Optional, random password generated if none specified."
|
2021-10-23 13:44:21 -07:00
|
|
|
echo " -w Write db info to /srv/www/domain/.my.cnf and create include in"
|
|
|
|
echo " /home/username/.my.cnf. Warning! This includes the unencrypted password."
|
2021-10-03 12:18:08 -07:00
|
|
|
echo " -v Verbose - output newly created db info to console."
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
2021-10-03 12:18:08 -07:00
|
|
|
echo " MySQL database names is based on virtualhost with . replaced by the word 'dot'"
|
2021-09-16 16:21:35 -07:00
|
|
|
echo " and - replaced by the word 'dash'. If username is autogenerated it is based on"
|
|
|
|
echo " the shell username & the virtualhost name."
|
|
|
|
echo " e.g. for virtualost example.com the db name will be 'exampledotcom' and the"
|
|
|
|
echo " username will be examplec@example.com."
|
2022-07-18 19:11:44 -07:00
|
|
|
echo " It is highly recommended to use either the -w or -v option if you don't use -p."
|
2021-09-16 16:21:35 -07:00
|
|
|
}
|
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
vhost:getoptions "$@"
|
2021-09-16 16:21:35 -07:00
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
# check for domain (virtualhost)
|
|
|
|
if [[ -z $domain ]]; then
|
|
|
|
echo "domain is required"
|
|
|
|
exit
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# make sure virtualhost exists
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ ! -d /srv/www/$domain ]]; then
|
|
|
|
echo "virtualhost $domain does not exist"
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set database name
|
2021-10-05 13:52:02 -07:00
|
|
|
database=${domain//./dot}
|
2021-04-04 13:28:22 -07:00
|
|
|
database=${database//-/dash}
|
|
|
|
|
|
|
|
# make sure database doesn't already exist
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -d /var/lib/mysql/$database ]]; then
|
2021-04-04 13:28:22 -07:00
|
|
|
echo "database $database already exists"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-09-16 16:21:35 -07:00
|
|
|
# get & set username of virtualhost
|
2021-10-05 11:33:24 -07:00
|
|
|
vhost_username=$(stat -c '%U' /srv/www/$domain)
|
2021-09-16 16:21:35 -07:00
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
# check for and set mysql username
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -z $username ]]; then
|
|
|
|
username=$vhost_username@$domain
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check for and set mysql password
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -z $password ]]; then
|
2021-04-04 13:28:22 -07:00
|
|
|
password=`/usr/bin/pwgen 16 1`
|
|
|
|
fi
|
|
|
|
|
|
|
|
mysqladmin create $database
|
|
|
|
mysql -e "CREATE USER '$username'@'localhost' IDENTIFIED BY '$password';"
|
|
|
|
mysql -e "GRANT ALL PRIVILEGES ON $database.* TO '$username'@'localhost';"
|
|
|
|
mysqladmin flush-privileges
|
|
|
|
|
2021-09-16 16:21:35 -07:00
|
|
|
# save mysql db info to file
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -n $write ]]; then
|
2021-10-23 13:44:21 -07:00
|
|
|
touch /srv/www/$domain/.my.cnf
|
|
|
|
chown $vhost_username:$vhost_username /srv/www/$domain/.my.cnf
|
|
|
|
chmod 640 /srv/www/$domain/.my.cnf
|
|
|
|
echo '[client]' > /srv/www/$domain/.my.cnf
|
|
|
|
echo "host=127.0.0.1" >> /srv/www/$domain/.my.cnf
|
|
|
|
echo "database=$database" >> /srv/www/$domain/.my.cnf
|
|
|
|
echo "user=$username" >> /srv/www/$domain/.my.cnf
|
|
|
|
echo "password=$password" >> /srv/www/$domain/.my.cnf
|
2021-10-03 12:18:08 -07:00
|
|
|
touch /home/$vhost_username/.my.cnf
|
|
|
|
chown $vhost_username:$vhost_username /home/$vhost_username/.my.cnf
|
|
|
|
chmod 640 /home/$vhost_username/.my.cnf
|
2021-10-23 13:44:21 -07:00
|
|
|
echo "#[client]" > /home/$vhost_username/.my.cnf
|
|
|
|
echo '!include' "/srv/www/$domain/.my.cnf" >> /home/$vhost_username/.my.cnf
|
2021-10-03 12:18:08 -07:00
|
|
|
fi
|
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -n $verbose ]]; then
|
2021-10-03 12:18:08 -07:00
|
|
|
echo "database=$database user=$username password=$password"
|
|
|
|
fi
|