vhost-stack/bin/vhost-destroy.sh

65 lines
1.6 KiB
Bash
Raw Normal View History

2021-04-04 13:28:22 -07:00
#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
2022-08-22 13:22:16 -07:00
# 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)
2021-04-04 13:28:22 -07:00
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 "Remove virtualhost and associated user & database & db user from this server."
echo ""
2021-10-05 11:33:24 -07:00
echo "usage: $thisfilename -d <domain>"
2021-09-16 16:21:35 -07:00
echo ""
echo " -h Print this help."
2021-10-05 11:33:24 -07:00
echo " -d <domain> Domain name of VirtualHost to remove."
2021-09-16 16:21:35 -07:00
exit
}
2021-10-05 11:33:24 -07:00
vhost:getoptions "$@"
# check for domain (virtualhost)
if [[ -z $domain ]]; then
echo "domain is required"
exit
fi
# check that virtualhost dir exists
if [[ ! -d /srv/www/$domain ]]; then
echo "virtualhost dir does not exist"
exit 1
2021-04-04 13:28:22 -07:00
fi
2021-09-16 16:21:35 -07:00
# check for database and delete if it exists
2021-10-05 11:33:24 -07:00
database=${domain//./dot}
2021-09-16 16:21:35 -07:00
database=${database//-/dash}
2021-10-05 11:33:24 -07:00
if [[ -d /var/lib/mysql/$database ]]; then
/usr/local/bin/vhost-mysql-db-del.sh -d $domain
2021-04-04 13:28:22 -07:00
fi
2021-09-16 16:21:35 -07:00
# get & set username for this virtualhost
2021-10-05 11:33:24 -07:00
username=$(stat -c '%U' /srv/www/$domain)
2021-04-04 13:28:22 -07:00
2021-09-16 16:21:35 -07:00
# del virtualhost files & configs
/usr/local/bin/vhost-del.sh -d $domain
2021-04-04 13:28:22 -07:00
2021-09-16 16:21:35 -07:00
# 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
2022-09-13 12:59:36 -07:00
existingvirtualhosts=false
2021-09-16 16:21:35 -07:00
vhost::set-virtualhostArray
for v in "${virtualhostArray[@]}"
do
2021-10-05 11:33:24 -07:00
if [[ $(stat -c '%U' /srv/www/$v) = $username ]]; then
2021-09-16 16:21:35 -07:00
existingvirtualhosts=true
fi
done
2022-09-13 12:59:36 -07:00
if [[ $existingvirtualhosts == "false" ]]; then
/usr/local/bin/vhost-user-del.sh -u $username
2021-04-04 13:28:22 -07:00
fi