From ea2343a1dff53ee7b126d4272a5f84479dc98261 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 28 Jan 2026 14:01:51 +0100 Subject: [PATCH] cleanup struktur und nginx restart --- sync-ispconfig-cleanup.sh | 155 +++++++++++++++++++++++--------------- 1 file changed, 93 insertions(+), 62 deletions(-) diff --git a/sync-ispconfig-cleanup.sh b/sync-ispconfig-cleanup.sh index 47189a4..5767baf 100755 --- a/sync-ispconfig-cleanup.sh +++ b/sync-ispconfig-cleanup.sh @@ -1,15 +1,25 @@ #!/bin/bash -# Konfiguration laden -source /usr/local/bin/sync-ispconfig-proxy.conf -PROXY_CONF="/usr/local/bin/proxy_based_server.conf" +# ============================================================================== +# ISPConfig Proxy Cleanup Script - Version 1.1 +# ============================================================================== -# Standardeinstellungen -DRY_RUN=true +# --- KONFIGURATION --- +# Nutzt die zentrale Konfiguration des Proxy-Sync-Scripts +CONF_FILE="/usr/local/bin/sync-ispconfig-proxy.conf" +PROXY_CONF="/usr/local/bin/proxy_based_server.conf" CONFIG_DIR="/etc/nginx/conf.d/proxy_generated" + +# --- GLOBALE VARIABLEN --- +DRY_RUN=true FILES_DELETED=false -# Hilfe-Funktion +# --- FUNKTIONEN --- + +load_config() { + [ -f "$CONF_FILE" ] && source "$CONF_FILE" || { echo "Fehler: $CONF_FILE fehlt"; exit 1; } +} + show_help() { cat << EOF Nutzung: $(basename "$0") [OPTIONEN] @@ -23,69 +33,90 @@ Optionen: EOF } -# Parameter-Parsing -while [[ "$#" -gt 0 ]]; do - case $1 in - -e|--execute) DRY_RUN=false ;; - -h|--help) show_help; exit 0 ;; - *) echo "Fehler: Unbekannte Option $1"; show_help; exit 1 ;; - esac - shift -done +parse_params() { + while [[ "$#" -gt 0 ]]; do + case $1 in + -e|--execute) DRY_RUN=false ;; + -h|--help) show_help; exit 0 ;; + *) echo "Fehler: Unbekannte Option $1"; show_help; exit 1 ;; + esac + shift + done +} -if [ "$DRY_RUN" = true ]; then - echo "--- DRY-RUN MODUS: Keine Änderungen am System ---" -fi +get_blocklist() { + # Extrahiert die Blocklist aus der proxy_based_server.conf + sed -n '/^\[blocklist\]/,/^\[/p' "$PROXY_CONF" | grep -v '^\[' | grep -v '^#' | sed '/^$/d' +} -# 1. Blocklist extrahieren -BLOCKLIST=$(sed -n '/^\[blocklist\]/,/^\[/p' "$PROXY_CONF" | grep -v '^\[' | grep -v '^#' | sed '/^$/d') +get_active_domains() { + # Holt alle aktiven Domains aus der Datenbank + mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -Bse "SELECT domain FROM web_domain WHERE active = 'y';" +} -# 2. Aktive Domains aus DB holen -SQL_QUERY="SELECT domain FROM web_domain WHERE active = 'y';" -DB_DOMAINS=$(mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -Bse "$SQL_QUERY") - -if [ $? -ne 0 ]; then - echo "Fehler: Datenbankverbindung fehlgeschlagen." - exit 1 -fi - -# 3. Cleanup-Schleife -for file in "$CONFIG_DIR"/*.conf; do - [ -e "$file" ] || continue - filename=$(basename "$file" .conf) +run_cleanup() { + local blocklist=$(get_blocklist) + local db_domains=$(get_active_domains) - # Blocklist Check - if echo "$BLOCKLIST" | grep -qxw "$filename"; then - continue + if [ $? -ne 0 ]; then + echo "Fehler: Datenbankverbindung fehlgeschlagen." >&2 + exit 1 fi - - # DB Check - if ! echo "$DB_DOMAINS" | grep -qxw "$filename"; then + + for file in "$CONFIG_DIR"/*.conf; do + [ -e "$file" ] || continue + local filename=$(basename "$file" .conf) + + # Prüfen, ob Datei in der Blocklist steht + if echo "$blocklist" | grep -qxw "$filename"; then + [ "$DRY_RUN" = true ] && echo "[SKIP] Blocklist: $filename" + continue + fi + + # Prüfen, ob Domain noch in der Datenbank existiert + if ! echo "$db_domains" | grep -qxw "$filename"; then + if [ "$DRY_RUN" = true ]; then + echo "[Simuliert] Lösche verwaiste Datei: $file" + FILES_DELETED=true + else + echo "Lösche veraltete Config: $file" + rm "$file" + FILES_DELETED=true + fi + fi + done +} + +reload_nginx() { + if [ "$FILES_DELETED" = true ]; then if [ "$DRY_RUN" = true ]; then - echo "[Simuliert] Lösche: $file" - FILES_DELETED=true + echo "[Simuliert] Syntax-Check und Reload: nginx -t && systemctl reload nginx" else - echo "Lösche veraltete Config: $file" - rm "$file" - FILES_DELETED=true + echo "Änderungen vorgenommen. Prüfe Nginx Syntax..." + # Sicherheitscheck: Nur bei Erfolg reloaden + if nginx -t; then + systemctl reload nginx + echo "Nginx erfolgreich neu geladen." + else + echo "FEHLER: Nginx Syntax-Fehler! Kein Reload durchgeführt." >&2 + exit 1 + fi fi - fi -done - -# 4. Bedingter Nginx Reload -if [ "$FILES_DELETED" = true ]; then - if [ "$DRY_RUN" = true ]; then - echo "[Simuliert] Änderungen gefunden: nginx -t && systemctl reload nginx" else - echo "Änderungen vorgenommen. Prüfe Nginx Syntax..." - if nginx -t; then - systemctl reload nginx - echo "Nginx erfolgreich neu geladen." - else - echo "Fehler in Nginx-Konfiguration! Reload abgebrochen." - exit 1 - fi + echo "Keine Änderungen gefunden. Nginx-Betrieb unverändert." fi -else - echo "Keine verwaisten Dateien gefunden. Kein Reload notwendig." -fi +} + +# --- MAIN ENGINE --- + +main() { + parse_params "$@" + load_config + + [ "$DRY_RUN" = true ] && echo "--- DRY-RUN MODUS AKTIV (Keine Änderungen) ---" + + run_cleanup + reload_nginx +} + +main "$@"