Skip to content

Instantly share code, notes, and snippets.

@pbredenberg
Last active December 6, 2015 19:42
Show Gist options
  • Save pbredenberg/d072a2219dac41818d24 to your computer and use it in GitHub Desktop.
Save pbredenberg/d072a2219dac41818d24 to your computer and use it in GitHub Desktop.
Remove nginx server block created with serverblock.sh
#!/usr/bin/env bash
#
# Nginx - delete block
# Based on this post: http://clubmate.fi/how-to-make-an-nginx-server-block-manually-or-with-a-shell-script/
# Functions
ok() { echo -e '\e[32m'$1'\e[m'; } # Green
die() { echo -e '\e[1;31m'$1'\e[m'; exit 1; }
# Variables
NGINX_AVAILABLE_VHOSTS='/etc/nginx/sites-available'
NGINX_ENABLED_VHOSTS='/etc/nginx/sites-enabled'
WEB_DIR='/var/www'
CURRENT_TIME=$(date "+%Y%m%d-%H%M%S")
ARCHIVE_NAME=$CURRENT_TIME'.zip'
# Sanity check
[ $(id -g) != "0" ] && die "Script must be run as root."
[ $# != "1" ] && die "Usage: $(basename $0) domainName"
# Zip up and backup to archive site
echo "Archiving site $1"
mkdir -p $WEB_DIR/archive/$1
zip -r $WEB_DIR/archive/$1/$ARCHIVE_NAME $WEB_DIR/$1
# Delete serverblock directories
rm -rf $WEB_DIR/$1
# Remove site and symbolic link
rm -f $NGINX_ENABLED_VHOSTS/$1
rm -f $NGINX_AVAILABLE_VHOSTS/$1
# Restart
echo "Do you wish to restart nginx?"
select yn in "Yes" "No"; do
case $yn in
Yes ) /etc/init.d/nginx restart ; break;;
No ) exit;;
esac
done
ok "Site Removed for $1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment