-
-
Save xyzulu/cf6e6e0609f465b17f723bf3d4f5208e to your computer and use it in GitHub Desktop.
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 | |
LOG_USER_DIR="/var/www" | |
LOG_SERVER_DIR="/var/log" | |
TRUNC_USER_SIZE="+3M" | |
TRUNC_USER_BYTES=3145728 | |
TRUNC_SERVER_SIZE="+50M" | |
TRUNC_SERVER_BYTES=52428800 | |
JOURNAL_RETENTION_DAYS=10 | |
POSTFIX_LOG="/var/log/postfix.log" | |
truncate_logs() { | |
local dir=$1 | |
local size_filter=$2 | |
local label=$3 | |
local bytes files file temp_file | |
printf "=== [%s] Scanning %s for files > %s ===\n" "$label" "$dir" "$size_filter" | |
if [[ "$label" == "User Logs" ]]; then | |
bytes=$TRUNC_USER_BYTES | |
files=$(find "$dir" \ | |
-path "*/cpf_logs/*" -prune -o \ | |
\( -name "*.log" -o -name "*_log" \) -type f -size "$size_filter" -print 2>/dev/null) | |
else | |
bytes=$TRUNC_SERVER_BYTES | |
files=$(find "$dir" \( -name "*.log" -o -name "*_log" \) -type f -size "$size_filter" 2>/dev/null) | |
fi | |
if [[ -z "$files" ]]; then | |
printf "[%s] No files to process.\n\n" "$label" | |
return | |
fi | |
while IFS= read -r file; do | |
printf "[%s] Processing: %s\n" "$label" "$file" | |
if [[ ! -f "$file" || ! -r "$file" || ! -w "$file" ]]; then | |
printf "[%s] Skipping inaccessible file: %s\n" "$label" "$file" | |
continue | |
fi | |
if [[ "$file" == "$POSTFIX_LOG" ]]; then | |
printf "[%s] Clearing postfix log: %s\n" "$label" "$file" | |
if : > "$file"; then | |
printf "[%s] Successfully cleared postfix log\n" "$label" | |
else | |
printf "[%s] ERROR clearing postfix log\n" "$label" | |
fi | |
continue | |
fi | |
temp_file=$(mktemp) || { | |
printf "[%s] ERROR creating temporary file\n" "$label" | |
continue | |
} | |
printf "[%s] Truncating to last %d bytes\n" "$label" "$bytes" | |
if tail -c "$bytes" "$file" > "$temp_file" && cp "$temp_file" "$file"; then | |
printf "[%s] Successfully truncated: %s\n" "$label" "$file" | |
else | |
printf "[%s] ERROR truncating %s\n" "$label" "$file" | |
fi | |
rm -f "$temp_file" | |
done <<< "$files" | |
printf "[%s] Done.\n\n" "$label" | |
} | |
clean_journal() { | |
printf "=== [Journal] Vacuuming logs older than %sd ===\n" "$JOURNAL_RETENTION_DAYS" | |
if sudo journalctl --vacuum-time="${JOURNAL_RETENTION_DAYS}d" > /dev/null 2>&1; then | |
printf "[Journal] Vacuum completed successfully.\n\n" | |
else | |
printf "[Journal] ERROR during vacuum operation.\n\n" | |
fi | |
} | |
main() { | |
printf ">>> Starting log maintenance at %s <<<\n\n" "$(date '+%Y-%m-%d %H:%M:%S')" | |
truncate_logs "$LOG_USER_DIR" "$TRUNC_USER_SIZE" "User Logs" | |
truncate_logs "$LOG_SERVER_DIR" "$TRUNC_SERVER_SIZE" "Server Logs" | |
clean_journal | |
printf ">>> Finished at %s <<<\n" "$(date '+%Y-%m-%d %H:%M:%S')" | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment