#!/bin/bash # # vhost-stack # https://git.stack-source.com/msb/vhost-stack # MIT License Copyright (c) 2021 Matthew Saunders Brown # load config source /usr/local/etc/vhost.conf || echo "ERROR: Either you do not have vhost user permissions or the config file is missing." && exit # check for and set virtualhost if [ -n "$1" ]; then virtualhost=$1 else echo "virtualhost not set" exit 1 fi # 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 # check for and set mysql username if [ -n "$2" ]; then username=$2 else username=$(stat -c '%U' /srv/www/$virtualhost)@$virtualhost 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 echo "database '$database' created with username '$username' and password '$password'"