Created
October 5, 2025 15:47
-
-
Save ILPlais/4c4f914aa500c8f37e1d99ae1c1e5d03 to your computer and use it in GitHub Desktop.
Simplified script to archive all old logs into a single archive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Simplified script to archive all old logs into a single archive | |
| LOG_DIR="." | |
| ARCHIVE_NAME="archive.7z" | |
| PATTERN="????-??-??T??-??-?? - *.txt" | |
| # Deadline: more than 30 days ago (example) | |
| #CUTOFF_DATE=$(date --date="30 days ago" +%s) | |
| # Deadline: more than one week ago | |
| CUTOFF_DATE=$(date --date="last week" +%s) | |
| echo "[*] Collecting old files…" | |
| # Array for collecting all eligible files | |
| OLD_FILES=() | |
| for logfile in $LOG_DIR/$PATTERN; do | |
| [[ ! -f "$logfile" ]] && continue | |
| filename=$(basename "$logfile") | |
| date_part=$(echo "$filename" | cut -d' ' -f1) | |
| formatted_date=$(echo "$date_part" | sed -e 's/\([[:digit:]]\{4\}\)\-\([[:digit:]]\{2\}\)\-\([[:digit:]]\{2\}\)T\([[:digit:]]\{2\}\)\-\([[:digit:]]\{2\}\)\-\([[:digit:]]\{2\}\)/\1-\2-\3 \4:\5:\6/') | |
| file_timestamp=$(date -d "$formatted_date" +%s 2>/dev/null) | |
| [[ $? -ne 0 ]] && continue | |
| if [[ $file_timestamp -lt $CUTOFF_DATE ]]; then | |
| OLD_FILES+=("$logfile") | |
| echo "[+] To archive: $filename" | |
| fi | |
| done | |
| # Archive all old files at once | |
| if [[ ${#OLD_FILES[@]} -gt 0 ]]; then | |
| echo "======================================================================" | |
| echo "[*] Create the archive: $ARCHIVE_NAME" | |
| echo "[*] Files to archive: ${#OLD_FILES[@]}" | |
| 7zr a "$ARCHIVE_NAME" "${OLD_FILES[@]}" -mx=9 -stl -sdel -bb | |
| if [[ $? -eq 0 ]]; then | |
| echo "[*] Archive created with success" | |
| else | |
| echo "[!] Error while creating the archive" | |
| fi | |
| else | |
| echo "[!] No old files found" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment