Logging: nur Einträge nach x Tagen werden gelöscht sonst nichts
This commit is contained in:
@@ -5,13 +5,29 @@ FILE_URL="http://fileserver.fritz.box/Mobaxterm/Homelab.mxtsessions"
|
||||
INPUT_FILE="Homelab.mxtsessions"
|
||||
OUTPUT_FILE="serverliste.csv"
|
||||
LOG_FILE="script.log"
|
||||
LOG_RETENTION_DAYS=2 # Log-Einträge älter als X Tage werden entfernt
|
||||
|
||||
log() {
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] $1" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
clean_old_logs() {
|
||||
tmp_log="${LOG_FILE}.tmp"
|
||||
cutoff_date=$(date -d "-${LOG_RETENTION_DAYS} days" +'%Y-%m-%d %H:%M:%S')
|
||||
|
||||
awk -v cutoff="$cutoff_date" '
|
||||
/^\[[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\]/ {
|
||||
timestamp = substr($0, 2, 19)
|
||||
if (timestamp >= cutoff) print $0
|
||||
next
|
||||
}
|
||||
# Alle anderen Zeilen behalten (auch ohne Zeitstempel)
|
||||
{ print $0 }
|
||||
' "$LOG_FILE" > "$tmp_log" && mv "$tmp_log" "$LOG_FILE"
|
||||
}
|
||||
|
||||
download_file() {
|
||||
rm -f Homelab.mxtsessions* 2>/dev/null
|
||||
rm -f "$INPUT_FILE" 2>/dev/null
|
||||
if wget -q "$FILE_URL" -O "$INPUT_FILE"; then
|
||||
log "Datei erfolgreich heruntergeladen."
|
||||
else
|
||||
@@ -80,18 +96,8 @@ show_menu() {
|
||||
}
|
||||
|
||||
main() {
|
||||
# Alte Logeinträge aus der Logdatei entfernen (basierend auf konfigurierbarem Zeitraum)
|
||||
LOG_RETENTION_DAYS=7
|
||||
tmp_log="${LOG_FILE}.tmp"
|
||||
cutoff_date=$(date -d "${LOG_RETENTION_DAYS} days ago" +'%Y-%m-%d %H:%M:%S')
|
||||
awk -v cutoff="$cutoff_date" '
|
||||
match($0, /^\[[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\]/) {
|
||||
timestamp = substr($0, 2, 19)
|
||||
if (timestamp >= cutoff) print $0
|
||||
}
|
||||
' "$LOG_FILE" > "$tmp_log" && mv "$tmp_log" "$LOG_FILE"
|
||||
# Alte Logeinträge löschen, wenn älter als 7 Tage
|
||||
find . -name "$LOG_FILE" -type f -mtime +7 -exec rm {} \;
|
||||
[[ -f "$LOG_FILE" ]] && clean_old_logs
|
||||
|
||||
log "Skript gestartet."
|
||||
download_file
|
||||
extract_data
|
||||
|
||||
Reference in New Issue
Block a user