From c6cf2602d34a4a51f81812b92cc9dd3c0c695b9a Mon Sep 17 00:00:00 2001 From: Blackwhitebear8 Date: Fri, 3 Oct 2025 11:03:45 +0200 Subject: [PATCH] Add zone-cleanup.sh --- zone-cleanup.sh | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 zone-cleanup.sh diff --git a/zone-cleanup.sh b/zone-cleanup.sh new file mode 100644 index 0000000..fda714f --- /dev/null +++ b/zone-cleanup.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# Script to find DNS zones not linked to a user +# and optionally delete them. + +echo "Checking for DNS zones without a linked user..." +echo "======================================================" + +# --- Preparation --- + +# Check if required files/directories exist +if [[ ! -d "/var/named" ]]; then + echo "Error: Directory /var/named not found" + exit 1 +fi + +if [[ ! -f "/etc/userdomains" ]]; then + echo "Error: File /etc/userdomains not found" + exit 1 +fi + +# Create temporary files for comparison +ZONES_FILE=$(mktemp) +DOMAINS_FILE=$(mktemp) +ORPHANED_ZONES_FILE=$(mktemp) + +# Clean up temporary files on exit (even on error) +trap 'rm -f "$ZONES_FILE" "$DOMAINS_FILE" "$ORPHANED_ZONES_FILE"' EXIT + +# --- Analysis --- + +# 1. Create a list of all DNS zones +for db_file in /var/named/*.db; do + if [[ -f "$db_file" ]]; then + basename "$db_file" .db + fi +done | sort > "$ZONES_FILE" + +# 2. Create a list of all domains linked to users +cut -d: -f1 /etc/userdomains | sort > "$DOMAINS_FILE" + +# 3. Compare the lists and save the "orphaned" zones +comm -23 "$ZONES_FILE" "$DOMAINS_FILE" > "$ORPHANED_ZONES_FILE" + +# --- Action & Confirmation --- + +# Check if the file with orphaned zones is empty or not +if [[ ! -s "$ORPHANED_ZONES_FILE" ]]; then + echo + echo "Good news! No orphaned DNS zones found." +else + ZONE_COUNT=$(wc -l < "$ORPHANED_ZONES_FILE") + echo + echo "Found $ZONE_COUNT zone(s) that are NOT linked to a user:" + echo "---------------------------------------------------------" + # Display the list of zones that can be deleted + cat "$ORPHANED_ZONES_FILE" + echo "---------------------------------------------------------" + echo + + # --- FIRST CONFIRMATION --- + read -p "Do you want to proceed with deleting these $ZONE_COUNT zone(s)? (yes/no): " confirm1 + + if [[ "$confirm1" == "yes" ]]; then + echo + echo "WARNING: This action is permanent and cannot be undone." + + # --- SECOND CONFIRMATION --- + read -p "Are you ABSOLUTELY sure you want to delete the zones shown? (yes/no): " confirm2 + + if [[ "$confirm2" == "yes" ]]; then + echo + echo "Confirmation received. The following zones will now be deleted:" + + # Loop through the file and delete each zone + while read -r domain; do + # Make sure the line is not empty + if [[ -n "$domain" ]]; then + echo " -> Deleting DNS zone for: $domain" + # Execute the WHM API command + whmapi1 killdns domain="$domain" + fi + done < "$ORPHANED_ZONES_FILE" + + echo + echo "All selected zones have been processed." + else + echo "Second confirmation not received. Action canceled." + fi + else + echo "First confirmation not received. Action canceled." + fi +fi + +echo +echo "Script finished." \ No newline at end of file