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")
|
|
|
|
echo "Creates default MySQL database and db user for specified virtualhost."
|
|
|
|
echo ""
|
2021-10-03 12:18:08 -07:00
|
|
|
echo "usage: $thisfilename virtualhost [OPTIONS]"
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
|
|
|
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-03 12:18:08 -07:00
|
|
|
echo " -s Save db info to /home/username/.my.cnf. Warning! This inlcudes the unencrypted password."
|
|
|
|
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."
|
2021-10-03 12:18:08 -07:00
|
|
|
echo " It is highly recommended to use either the -s or -v option if you don't use -p."
|
2021-09-16 16:21:35 -07:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2021-10-03 12:18:08 -07:00
|
|
|
while getopts "hu:p:sv" opt; do
|
|
|
|
case "${opt}" in
|
|
|
|
h )
|
|
|
|
help
|
|
|
|
exit;;
|
|
|
|
u )
|
|
|
|
username=${OPTARG}
|
|
|
|
;;
|
|
|
|
p )
|
|
|
|
password=${OPTARG}
|
|
|
|
;;
|
|
|
|
s )
|
|
|
|
save=true
|
|
|
|
;;
|
|
|
|
v )
|
|
|
|
verbose=true
|
|
|
|
;;
|
|
|
|
\? )
|
|
|
|
echo "Invalid option: $OPTARG"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
: )
|
|
|
|
echo "Invalid option: $OPTARG requires an argument"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND-1))
|
2021-09-16 16:21:35 -07:00
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
# check for and set virtualhost
|
|
|
|
if [ -n "$1" ]; then
|
2021-10-03 12:18:08 -07:00
|
|
|
virtualhost="${1,,}"
|
2021-04-04 13:28:22 -07:00
|
|
|
else
|
|
|
|
echo "virtualhost not set"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-10-03 12:18:08 -07:00
|
|
|
echo "virtualhost=$virtualhost username=$username password=$password save=$save verbose=$verbose"
|
|
|
|
|
|
|
|
exit
|
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
# make sure virtualhost exists
|
|
|
|
if [ ! -d /srv/www/$virtualhost ]; then
|
|
|
|
echo "virtualhost $virtualhost does not exist"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set database name
|
|
|
|
database=${virtualhost//./dot}
|
|
|
|
database=${database//-/dash}
|
|
|
|
|
|
|
|
# make sure database doesn't already exist
|
|
|
|
if [ -d /var/lib/mysql/$database ]; then
|
|
|
|
echo "database $database already exists"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-09-16 16:21:35 -07:00
|
|
|
# get & set username of virtualhost
|
|
|
|
vhost_username=$(stat -c '%U' /srv/www/$virtualhost)
|
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
# check for and set mysql username
|
|
|
|
if [ -n "$2" ]; then
|
|
|
|
username=$2
|
|
|
|
else
|
2021-09-16 16:21:35 -07:00
|
|
|
username=$vhost_username@$virtualhost
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check for and set mysql password
|
|
|
|
if [ -n "$3" ]; then
|
|
|
|
password=$3
|
|
|
|
else
|
|
|
|
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-03 12:18:08 -07:00
|
|
|
if [ -n $save ]; then
|
|
|
|
touch /home/$vhost_username/.my.cnf
|
|
|
|
chown $vhost_username:$vhost_username /home/$vhost_username/.my.cnf
|
|
|
|
chmod 640 /home/$vhost_username/.my.cnf
|
|
|
|
echo "[client]" > /home/$vhost_username/.my.cnf
|
|
|
|
echo "host=127.0.0.1" >> /home/$vhost_username/.my.cnf
|
|
|
|
echo "database=$database" >> /home/$vhost_username/.my.cnf
|
|
|
|
echo "user=$username" >> /home/$vhost_username/.my.cnf
|
|
|
|
echo "password=$password" >> /home/$vhost_username/.my.cnf
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n $verbose ]; then
|
|
|
|
echo "database=$database user=$username password=$password"
|
|
|
|
fi
|