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 #!/bin/bash
# Konfiguration laden # ==============================================================================
source /usr/local/bin/sync-ispconfig-proxy.conf # ISPConfig Proxy Cleanup Script - Version 1.1
PROXY_CONF="/usr/local/bin/proxy_based_server.conf" # ==============================================================================
# Standardeinstellungen # --- KONFIGURATION ---
DRY_RUN=true # 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" CONFIG_DIR="/etc/nginx/conf.d/proxy_generated"
# --- GLOBALE VARIABLEN ---
DRY_RUN=true
FILES_DELETED=false FILES_DELETED=false
# Hilfe-Funktion # --- FUNKTIONEN ---
load_config() {
[ -f "$CONF_FILE" ] && source "$CONF_FILE" || { echo "Fehler: $CONF_FILE fehlt"; exit 1; }
}
show_help() { show_help() {
cat << EOF cat << EOF
Nutzung: $(basename "$0") [OPTIONEN] Nutzung: $(basename "$0") [OPTIONEN]
@@ -23,7 +33,7 @@ Optionen:
EOF EOF
} }
# Parameter-Parsing parse_params() {
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do
case $1 in case $1 in
-e|--execute) DRY_RUN=false ;; -e|--execute) DRY_RUN=false ;;
@@ -32,37 +42,41 @@ while [[ "$#" -gt 0 ]]; do
esac esac
shift shift
done done
}
if [ "$DRY_RUN" = true ]; then get_blocklist() {
echo "--- DRY-RUN MODUS: Keine Änderungen am System ---" # Extrahiert die Blocklist aus der proxy_based_server.conf
fi sed -n '/^\[blocklist\]/,/^\[/p' "$PROXY_CONF" | grep -v '^\[' | grep -v '^#' | sed '/^$/d'
}
# 1. Blocklist extrahieren get_active_domains() {
BLOCKLIST=$(sed -n '/^\[blocklist\]/,/^\[/p' "$PROXY_CONF" | grep -v '^\[' | grep -v '^#' | sed '/^$/d') # 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 run_cleanup() {
SQL_QUERY="SELECT domain FROM web_domain WHERE active = 'y';" local blocklist=$(get_blocklist)
DB_DOMAINS=$(mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -Bse "$SQL_QUERY") local db_domains=$(get_active_domains)
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Fehler: Datenbankverbindung fehlgeschlagen." echo "Fehler: Datenbankverbindung fehlgeschlagen." >&2
exit 1 exit 1
fi fi
# 3. Cleanup-Schleife
for file in "$CONFIG_DIR"/*.conf; do for file in "$CONFIG_DIR"/*.conf; do
[ -e "$file" ] || continue [ -e "$file" ] || continue
filename=$(basename "$file" .conf) local filename=$(basename "$file" .conf)
# Blocklist Check # Prüfen, ob Datei in der Blocklist steht
if echo "$BLOCKLIST" | grep -qxw "$filename"; then if echo "$blocklist" | grep -qxw "$filename"; then
[ "$DRY_RUN" = true ] && echo "[SKIP] Blocklist: $filename"
continue continue
fi fi
# DB Check # Prüfen, ob Domain noch in der Datenbank existiert
if ! echo "$DB_DOMAINS" | grep -qxw "$filename"; then if ! echo "$db_domains" | grep -qxw "$filename"; then
if [ "$DRY_RUN" = true ]; then if [ "$DRY_RUN" = true ]; then
echo "[Simuliert] Lösche: $file" echo "[Simuliert] Lösche verwaiste Datei: $file"
FILES_DELETED=true FILES_DELETED=true
else else
echo "Lösche veraltete Config: $file" echo "Lösche veraltete Config: $file"
@@ -71,21 +85,38 @@ for file in "$CONFIG_DIR"/*.conf; do
fi fi
fi fi
done done
}
# 4. Bedingter Nginx Reload reload_nginx() {
if [ "$FILES_DELETED" = true ]; then if [ "$FILES_DELETED" = true ]; then
if [ "$DRY_RUN" = 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 else
echo "Änderungen vorgenommen. Prüfe Nginx Syntax..." echo "Änderungen vorgenommen. Prüfe Nginx Syntax..."
# Sicherheitscheck: Nur bei Erfolg reloaden
if nginx -t; then if nginx -t; then
systemctl reload nginx systemctl reload nginx
echo "Nginx erfolgreich neu geladen." echo "Nginx erfolgreich neu geladen."
else else
echo "Fehler in Nginx-Konfiguration! Reload abgebrochen." echo "FEHLER: Nginx Syntax-Fehler! Kein Reload durchgeführt." >&2
exit 1 exit 1
fi fi
fi fi
else else
echo "Keine verwaisten Dateien gefunden. Kein Reload notwendig." echo "Keine Änderungen gefunden. Nginx-Betrieb unverändert."
fi fi
}
# --- MAIN ENGINE ---
main() {
parse_params "$@"
load_config
[ "$DRY_RUN" = true ] && echo "--- DRY-RUN MODUS AKTIV (Keine Änderungen) ---"
run_cleanup
reload_nginx
}
main "$@"