Files
coruna-lab/scripts/prune-c2-photos-before.sh
T
2026-09-16 22:29:13 +08:00

78 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Delete photo files under storage/app/private/c2 older than a cutoff (mtime).
# Deletes as it finds — does not collect the full list first.
# Default cutoff: 2026-09-15 00:00:00 (keeps the 15th and later).
#
# ./scripts/prune-c2-photos-before.sh
# ./scripts/prune-c2-photos-before.sh --execute
# BEFORE=2026-09-15 ./scripts/prune-c2-photos-before.sh --execute
set -u
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
if [ -z "${APP_ROOT:-}" ]; then
APP_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
fi
BEFORE="${BEFORE:-2026-09-15}"
CUTOFF="${BEFORE} 00:00:00"
EXECUTE=0
if [ "${1:-}" = "--execute" ]; then
EXECUTE=1
fi
PHOTOS="${APP_ROOT}/storage/app/private/c2/photos"
PREVIEWS="${APP_ROOT}/storage/app/private/c2/photo-previews"
if [ ! -d "$PHOTOS" ]; then
echo "ERROR: missing $PHOTOS" >&2
exit 2
fi
echo "=== prune c2 photos by mtime ==="
echo "app : $APP_ROOT"
echo "cutoff : before $CUTOFF"
echo "mode : $([ "$EXECUTE" -eq 1 ] && echo EXECUTE || echo DRY-RUN)"
echo "hint : deletes while scanning; a line every 200 files"
echo
prune_dir() {
local dir="$1"
local label="$2"
if [ ! -d "$dir" ]; then
echo "$label: skip (missing)"
return 0
fi
echo "$label: $dir"
if [ "$EXECUTE" -eq 1 ]; then
find "$dir" -type f ! -newermt "$CUTOFF" -print -delete
else
find "$dir" -type f ! -newermt "$CUTOFF" -print
fi | awk -v label="$label" '
{
n++
if (n <= 5 || n % 200 == 0) {
print " " label " " n " " $0
fflush()
}
}
END {
print " " label " total=" (n + 0)
fflush()
}
'
}
prune_dir "$PHOTOS" "photos"
prune_dir "$PREVIEWS" "previews"
if [ "$EXECUTE" -eq 1 ]; then
find "$PHOTOS" -mindepth 1 -type d -empty -delete 2>/dev/null || true
if [ -d "$PREVIEWS" ]; then
find "$PREVIEWS" -mindepth 1 -type d -empty -delete 2>/dev/null || true
fi
echo "done (deleted as found)"
else
echo "dry-run only; pass --execute to delete"
fi