Skip to content

Instantly share code, notes, and snippets.

@bajpangosh
Last active December 26, 2024 14:41
Show Gist options
  • Save bajpangosh/a9ffbed2a4903aa1f02c89439bfd6d8f to your computer and use it in GitHub Desktop.
Save bajpangosh/a9ffbed2a4903aa1f02c89439bfd6d8f to your computer and use it in GitHub Desktop.
de-domains.sh
#!/bin/bash
# Define directories for Nginx configuration
AVAILABLE_DIR="/etc/nginx/sites-available"
ENABLED_DIR="/etc/nginx/sites-enabled"
# Check if the directories exist
if [ ! -d "$AVAILABLE_DIR" ] || [ ! -d "$ENABLED_DIR" ]; then
echo "Error: Nginx configuration directories not found."
exit 1
fi
# List available domains (server block files)
DOMAINS=($(ls $AVAILABLE_DIR))
if [ ${#DOMAINS[@]} -eq 0 ]; then
echo "No domains found in $AVAILABLE_DIR."
exit 0
fi
# Display the list of domains to the user
echo "Available domains:"
for i in "${!DOMAINS[@]}"; do
echo "$((i + 1)). ${DOMAINS[i]}"
done
echo ""
read -p "Enter the numbers of the domains to delete (e.g., 1 3 5): " -a SELECTIONS
# Validate input
for num in "${SELECTIONS[@]}"; do
if ! [[ "$num" =~ ^[0-9]+$ ]] || [ "$num" -lt 1 ] || [ "$num" -gt ${#DOMAINS[@]} ]; then
echo "Warning: Invalid selection: $num"
fi
done
# Confirm deletion
echo "You have selected the following domains to delete:"
for num in "${SELECTIONS[@]}"; do
echo "- ${DOMAINS[$((num - 1))]}"
done
read -p "Are you sure you want to delete these domains? (y/N): " CONFIRM
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
echo "Operation cancelled."
exit 0
fi
# Delete the selected domains
for num in "${SELECTIONS[@]}"; do
DOMAIN_FILE="${DOMAINS[$((num - 1))]}"
echo "Deleting $DOMAIN_FILE..."
# Remove from sites-enabled if linked
if [ -L "$ENABLED_DIR/$DOMAIN_FILE" ]; then
rm "$ENABLED_DIR/$DOMAIN_FILE"
echo "Removed symbolic link: $ENABLED_DIR/$DOMAIN_FILE"
fi
# Remove from sites-available
if [ -f "$AVAILABLE_DIR/$DOMAIN_FILE" ]; then
rm "$AVAILABLE_DIR/$DOMAIN_FILE"
echo "Removed file: $AVAILABLE_DIR/$DOMAIN_FILE"
fi
done
# Test Nginx configuration
nginx -t
if [ $? -eq 0 ]; then
echo "Nginx configuration is valid. Reloading Nginx..."
systemctl reload nginx
echo "Nginx reloaded."
else
echo "Error in Nginx configuration. Please check manually."
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment