vhost-stack/bin/vhost-mysql-db-del.sh
Matthew Saunders Brown 8d05a58698 database name fix
2022-03-11 08:50:19 -08:00

44 lines
1009 B
Bash
Executable File

#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load include file
source $(dirname $0)/vhost.sh
help()
{
thisfilename=$(basename -- "$0")
echo "Remove MySQL database and default 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 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