#!/bin/bash # ============================================================================= # Frigate Media Cleanup # Removes recordings, clips, and snapshots older than 90 days from NAS # Run via cron: 0 3 * * 0 /mnt/docker-data/scripts/cleanup-frigate-media.sh # ============================================================================= MEDIA_DIR="/mnt/docker-data/media/frigate" MAX_AGE_DAYS=90 LOG_FILE="/var/log/frigate-cleanup.log" DRY_RUN=false if [ "$1" = "--dry-run" ]; then DRY_RUN=true fi log() { echo "$(date '+%Y-%m-%d %H:%M:%S') $1" | tee -a "$LOG_FILE" } if [ ! -d "$MEDIA_DIR" ]; then log "ERROR: $MEDIA_DIR does not exist" exit 1 fi log "=== Frigate media cleanup started (max age: ${MAX_AGE_DAYS} days, dry_run: ${DRY_RUN}) ===" # Get size before cleanup SIZE_BEFORE=$(du -sh "$MEDIA_DIR" 2>/dev/null | cut -f1) log "Size before: $SIZE_BEFORE" # Find and remove old files in recordings/, clips/, exports/ for subdir in recordings clips exports; do DIR="$MEDIA_DIR/$subdir" if [ -d "$DIR" ]; then COUNT=$(find "$DIR" -type f -mtime +${MAX_AGE_DAYS} | wc -l) log "Found $COUNT files older than ${MAX_AGE_DAYS} days in $subdir/" if [ "$DRY_RUN" = true ]; then find "$DIR" -type f -mtime +${MAX_AGE_DAYS} -print | head -20 log "(dry run - no files deleted)" else find "$DIR" -type f -mtime +${MAX_AGE_DAYS} -delete # Remove empty directories left behind find "$DIR" -type d -empty -delete 2>/dev/null log "Cleaned $subdir/" fi fi done # Get size after cleanup SIZE_AFTER=$(du -sh "$MEDIA_DIR" 2>/dev/null | cut -f1) log "Size after: $SIZE_AFTER" log "=== Cleanup complete ==="