cleanup struktur und nginx restart

This commit is contained in:
root
2026-01-28 14:01:51 +01:00
parent 468534979e
commit ea2343a1df

View File

@@ -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,46 +33,50 @@ Optionen:
EOF
}
# Parameter-Parsing
while [[ "$#" -gt 0 ]]; do
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
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")
run_cleanup() {
local blocklist=$(get_blocklist)
local db_domains=$(get_active_domains)
if [ $? -ne 0 ]; then
echo "Fehler: Datenbankverbindung fehlgeschlagen."
if [ $? -ne 0 ]; then
echo "Fehler: Datenbankverbindung fehlgeschlagen." >&2
exit 1
fi
fi
# 3. Cleanup-Schleife
for file in "$CONFIG_DIR"/*.conf; do
for file in "$CONFIG_DIR"/*.conf; do
[ -e "$file" ] || continue
filename=$(basename "$file" .conf)
local filename=$(basename "$file" .conf)
# Blocklist Check
if echo "$BLOCKLIST" | grep -qxw "$filename"; then
# Prüfen, ob Datei in der Blocklist steht
if echo "$blocklist" | grep -qxw "$filename"; then
[ "$DRY_RUN" = true ] && echo "[SKIP] Blocklist: $filename"
continue
fi
# DB Check
if ! echo "$DB_DOMAINS" | grep -qxw "$filename"; then
# 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: $file"
echo "[Simuliert] Lösche verwaiste Datei: $file"
FILES_DELETED=true
else
echo "Lösche veraltete Config: $file"
@@ -70,22 +84,39 @@ for file in "$CONFIG_DIR"/*.conf; do
FILES_DELETED=true
fi
fi
done
done
}
# 4. Bedingter Nginx Reload
if [ "$FILES_DELETED" = true ]; then
reload_nginx() {
if [ "$FILES_DELETED" = true ]; then
if [ "$DRY_RUN" = true ]; then
echo "[Simuliert] Änderungen gefunden: nginx -t && systemctl reload nginx"
echo "[Simuliert] Syntax-Check und Reload: nginx -t && systemctl reload nginx"
else
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 in Nginx-Konfiguration! Reload abgebrochen."
echo "FEHLER: Nginx Syntax-Fehler! Kein Reload durchgeführt." >&2
exit 1
fi
fi
else
echo "Keine verwaisten Dateien gefunden. Kein Reload notwendig."
fi
else
echo "Keine Änderungen gefunden. Nginx-Betrieb unverändert."
fi
}
# --- MAIN ENGINE ---
main() {
parse_params "$@"
load_config
[ "$DRY_RUN" = true ] && echo "--- DRY-RUN MODUS AKTIV (Keine Änderungen) ---"
run_cleanup
reload_nginx
}
main "$@"