#!/usr/bin/env bash # Prune old rows from logs/storage-growth/history.csv to cap file size. # Keeps header and either last KEEP_DAYS (as proxy: days*200 rows) or last MAX_ROWS. # Usage: ./scripts/monitoring/prune-storage-history.sh [--days N] [--max-rows 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}" HISTORY_CSV="${HISTORY_CSV:-$LOG_DIR/history.csv}" KEEP_DAYS="${KEEP_DAYS:-90}" MAX_ROWS="${MAX_ROWS:-0}" ROWS_PER_DAY=200 DRY_RUN=0 while [ $# -gt 0 ]; do case "$1" in --days) KEEP_DAYS="${2:-90}" shift 2 ;; --max-rows) MAX_ROWS="${2:-0}" shift 2 ;; --dry-run) DRY_RUN=1 shift ;; *) shift ;; esac done if [ ! -f "$HISTORY_CSV" ]; then echo "File $HISTORY_CSV does not exist; nothing to prune." exit 0 fi header=$(head -1 "$HISTORY_CSV") total_lines=$(wc -l < "$HISTORY_CSV") data_lines=$((total_lines - 1)) if [ "$data_lines" -le 0 ]; then echo "No data rows in $HISTORY_CSV." exit 0 fi if [ "$MAX_ROWS" -le 0 ]; then MAX_ROWS=$((KEEP_DAYS * ROWS_PER_DAY)) fi if [ "$data_lines" -le "$MAX_ROWS" ]; then echo "Data rows ($data_lines) <= keep ($MAX_ROWS); no prune needed." exit 0 fi remove=$((data_lines - MAX_ROWS)) if [ "$DRY_RUN" -eq 1 ]; then echo "[dry-run] Would trim to last $MAX_ROWS rows (remove $remove rows)." exit 0 fi { echo "$header"; tail -n "$MAX_ROWS" "$HISTORY_CSV"; } > "${HISTORY_CSV}.$$" mv "${HISTORY_CSV}.$$" "$HISTORY_CSV" echo "Trimmed $HISTORY_CSV to last $MAX_ROWS data rows (removed $remove)."