#!/bin/bash # ============================================================================== # SIRO Maps โ€” Weekly Maintenance & Log Cleanup Script # ============================================================================== set -euo pipefail APP_DIR="${APP_DIR:-/home/hamzadoctor/app}" LOG_DIR="${APP_DIR}/logs" RETENTION_DAYS=14 echo "๐Ÿงน [$(date '+%Y-%m-%d %H:%M:%S')] Starting weekly log and docker maintenance..." # 1. Clean old log files if [ -d "$LOG_DIR" ]; then find "$LOG_DIR" -type f -name "*.log" -mtime +"$RETENTION_DAYS" -exec rm -f {} + echo "โœ… Removed log files older than ${RETENTION_DAYS} days." fi # 2. Truncate current active logs if they exceed 50MB for log in "$LOG_DIR"/*.log; do if [ -f "$log" ]; then size=$(stat -c%s "$log" 2>/dev/null || stat -f%z "$log" 2>/dev/null || echo 0) if [ "$size" -gt 52428800 ]; then # 50MB tail -n 10000 "$log" > "${log}.tmp" && mv "${log}.tmp" "$log" echo "โœ‚๏ธ Truncated large log file: $log" fi fi done # 3. Clean dangling docker images and build cache docker image prune -f --filter "until=168h" > /dev/null 2>&1 || true docker builder prune -f --keep-storage 2GB > /dev/null 2>&1 || true echo "๐Ÿ [$(date '+%Y-%m-%d %H:%M:%S')] Cleanup completed successfully."