vhost-stack/bin/vhost-destroy.sh
2021-10-02 15:32:38 -07:00

64 lines
1.4 KiB
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 virtualhost and associated user & database & db user from this server."
echo ""
echo "usage: $thisfilename virtualhost"
echo ""
echo " -h Print this help."
exit
}
# check for and set virtualhost
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
elif [ ! -d /srv/www/$1 ]; then
echo "virtualhost dir does not exist"
exit 1
else
virtualhost="${1,,}"
fi
else
help
fi
# check for database and delete if it exists
database=${virtualhost//./dot}
database=${database//-/dash}
if [ -d /var/lib/mysql/$database ]; then
/usr/local/bin/vhost-mysql-db-del.sh $virtualhost
fi
# get & set username for this virtualhost
username=$(stat -c '%U' /srv/www/$virtualhost)
# check for a delete varnish config
# del virtualhost files & configs
/usr/local/bin/vhost-del.sh $virtualhost
# check for any remaining virtualhosts before deleting user
# same check is done in vhost-user-del.sh
# but doing this here avoids generating any errors
vhost::set-virtualhostArray
for v in "${virtualhostArray[@]}"
do
if [ $(stat -c '%U' /srv/www/$v) = $username ]; then
existingvirtualhosts=true
fi
done
if [ -n "$existingvirtualhosts" ]; then
/usr/local/bin/vhost-user-del.sh $username
fi