vhost-stack/bin/vhost-mysql-db-del.sh
2022-08-22 13:22:16 -07:00

45 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# 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)
# load include file
source $(dirname $0)/vhost.sh
help()
{
thisfilename=$(basename -- "$0")
echo "Remove MySQL database and associated db user for the specified virtualhost."
echo ""
echo "usage: $thisfilename -d <domain>"
echo ""
echo " -h Print this help."
echo " -d <domain> Domain name of VirtualHost to delete MySQL db for."
exit
}
vhost:getoptions "$@"
# check for domain (virtualhost)
if [[ -z $domain ]]; then
echo "domain is required"
exit
fi
# set database name
database=${domain//./dot}
database=${database//-/dash}
# drop database
mysql -e "DROP DATABASE IF EXISTS $database;"
# set default username and attempt to drop user
if [ -d /srv/www/$domain ]; then
vhost_username=$(stat -c '%U' /srv/www/$domain)
username=$vhost_username@$domain
mysql -e "DROP USER IF EXISTS '$username'@'localhost';"
mysqladmin flush-privileges
fi