Files
proxmox/scripts/monitoring/prune-storage-snapshots.sh

52 lines
1.3 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Prune old storage growth snapshot files under logs/storage-growth/.
# Keeps history.csv and cron.log; deletes snapshot_*.txt older than KEEP_DAYS.
# Usage: ./scripts/monitoring/prune-storage-snapshots.sh [--days N] [--dry-run]
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
LOG_DIR="${LOG_DIR:-$PROJECT_ROOT/logs/storage-growth}"
KEEP_DAYS="${KEEP_DAYS:-30}"
DRY_RUN=0
while [ $# -gt 0 ]; do
case "$1" in
--days)
KEEP_DAYS="${2:-30}"
shift 2
;;
--dry-run)
DRY_RUN=1
shift
;;
*)
shift
;;
esac
done
if [ ! -d "$LOG_DIR" ]; then
echo "Directory $LOG_DIR does not exist; nothing to prune."
exit 0
fi
# Delete snapshot_YYYYMMDD_HHMMSS.txt older than KEEP_DAYS (by mtime)
count=0
while IFS= read -r -d '' f; do
[ -z "$f" ] && continue
if [ "$DRY_RUN" -eq 1 ]; then
echo "[dry-run] would remove: $f"
else
rm -f "$f"
fi
((count++)) || true
done < <(find "$LOG_DIR" -maxdepth 1 -name 'snapshot_*.txt' -mtime "+${KEEP_DAYS}" -print0 2>/dev/null || true)
if [ "$DRY_RUN" -eq 1 ]; then
echo "Dry run: would remove $count snapshot(s) older than ${KEEP_DAYS} days."
else
echo "Removed $count snapshot(s) older than ${KEEP_DAYS} days from $LOG_DIR."
fi