#!/bin/bash # ----------------------------------------------------------------------------------------------- # --------------------------------- Clear Logs Script ------------------------------------------ # ----------------------------------------------------------------------------------------------- # Clears unRAID system logs and Docker container logs safely. # Log file paths are configured in Master.conf under LOG_FILES. # Supports --dry-run to preview what would be cleared without making changes. # ----------------------------------------------------------------------------------------------- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../Master.conf" source "$SCRIPT_DIR/../common.sh" parse_args "$@" # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_GEAR Setup ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━ $ICON_GEAR Setup ━━━" # ROOT CHECK if [[ "$EUID" -ne 0 ]]; then error "Must be run as root" exit 1 fi success "Running as root" # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Status ━━━ # ----------------------------------------------------------------------------------------------- if [[ "$SHOW_STATUS" == true ]]; then echo "" echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━" echo "$ICON_HEALTH System Logs: ${LOG_FILES[*]}" echo "$ICON_CONTAINERS Docker Logs: /var/lib/docker/containers" echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "━━━━━━━━━━━━━━━━━━━━━━━" exit 0 fi # ----------------------------------------------------------------------------------------------- # FUNCTIONS # ----------------------------------------------------------------------------------------------- # Clears a single log file if it exists. # Skips with a warning if the file is not found. clear_file() { local file="$1" if [[ ! -f "$file" ]]; then warn "Not found: $file — skipping" return fi if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — would clear: $file" else : > "$file" success "Cleared: $file" fi } # Finds and clears all Docker container json log files. # Skips gracefully if Docker directory or log files are not found. clear_docker_logs() { if [[ ! -d /var/lib/docker/containers ]]; then warn "$ICON_CONTAINERS Docker directory not found — skipping" return fi local files files=$(find /var/lib/docker/containers/ -name "*-json.log" 2>/dev/null || true) if [[ -z "$files" ]]; then warn "$ICON_CONTAINERS No Docker logs found — skipping" return fi while IFS= read -r file; do if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — would clear Docker log: $file" else : > "$file" success "Cleared Docker log: $(basename "$(dirname "$file")")" fi done <<< "$files" } # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_HEALTH Clear Logs ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━ $ICON_HEALTH Clear Logs ━━━" echo "$ICON_HEALTH System Logs: ${LOG_FILES[*]}" echo "$ICON_CONTAINERS Docker Logs: enabled" echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "" [[ "$DRY_RUN" == true ]] && warn "DRY RUN — no changes will be made" START=$(date +%s) CLEAR_FAILED=false for logfile in "${LOG_FILES[@]}"; do clear_file "$logfile" || CLEAR_FAILED=true done echo "" echo "━━━ $ICON_CONTAINERS Docker ━━━" clear_docker_logs END=$(date +%s) # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Summary ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━━━ $ICON_SUMMARY LOG CLEANER SUMMARY ━━━━━" echo "$ICON_HEALTH System Logs: ${LOG_FILES[*]}" echo "$ICON_CONTAINERS Docker Logs: $([[ "$DRY_RUN" == true ]] && echo "skipped (dry run)" || echo "cleared")" echo "$ICON_TIME Duration: $(format_duration $((END - START)))" if [[ "$DRY_RUN" == true ]]; then echo "$ICON_WARN Status: DRY RUN — no changes made" elif [[ "$CLEAR_FAILED" == true ]]; then echo "$ICON_ERROR Status: $ICON_ERROR SOME LOGS FAILED TO CLEAR" notify "Log clear completed with errors on $(hostname)" "Clear Logs" "warning" else echo "$ICON_DONE Status: $ICON_SUCCESS DONE" notify "Logs cleared successfully on $(hostname)" "Clear Logs" "normal" fi echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"