#!/bin/bash# Safe cleanup script for wiping temporary files and old system logs# Strict mode: exit on error, treat unset variables as errorset-euopipefail
# Require root privilegesif["$EUID"-ne0];thenecho"Error: This script must be run as root (or with sudo).">&2exit1fi# Configuration VariablesTEMP_DIR="/tmp"LOG_DIR="/var/log"DAYS=30echo"=== Starting System Cleanup: $(date) ==="# 1. Clean /tmp safely (only delete files older than $DAYS)if[-d"$TEMP_DIR"];thenecho"Cleaning temporary files older than $DAYS days in $TEMP_DIR..."find"$TEMP_DIR"-typef-mtime+$DAYS-delete2>/dev/null||truefi# 2. Safely truncate or compress old log files (without deleting active log files)if[-d"$LOG_DIR"];thenecho"Compressing or truncating old log files in $LOG_DIR..."# Target archived log files (.gz, .1, .old) older than $DAYS days instead of active log filesfind"$LOG_DIR"-typef\(-name"*.gz"-o-name"*.1"-o-name"*.old"\)-mtime+$DAYS-delete2>/dev/null||truefi# 3. Clean systemd journal logs (if systemd is present)ifcommand-vjournalctl&>/dev/null;thenecho"Vacuuming systemd journal logs older than ${DAYS}d..."journalctl--vacuum-time="${DAYS}d"fiecho"=== Cleanup Completed Successfully: $(date) ==="