#!/usr/bin/env bash # Schedule cron for storage growth data collection (append to history.csv). # Usage: bash scripts/maintenance/schedule-storage-growth-cron.sh [--install|--show|--remove] set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" INSTALL_ROOT="${CRON_PROJECT_ROOT:-$PROJECT_ROOT}" COLLECT_SCRIPT="$INSTALL_ROOT/scripts/monitoring/collect-storage-growth-data.sh" PRUNE_SNAPSHOTS="$INSTALL_ROOT/scripts/monitoring/prune-storage-snapshots.sh" PRUNE_HISTORY="$INSTALL_ROOT/scripts/monitoring/prune-storage-history.sh" LOG_DIR="$INSTALL_ROOT/logs/storage-growth" # Every 6 hours CRON_STORAGE="0 */6 * * * cd $INSTALL_ROOT && bash $COLLECT_SCRIPT --append >> $LOG_DIR/cron.log 2>&1" # Weekly Sun 08:00: prune snapshots (30d) + history (~90d) CRON_PRUNE="0 8 * * 0 cd $INSTALL_ROOT && bash $PRUNE_SNAPSHOTS >> $LOG_DIR/cron.log 2>&1 && bash $PRUNE_HISTORY >> $LOG_DIR/cron.log 2>&1" validate_install_root() { if [[ "$INSTALL_ROOT" == /tmp/* ]]; then echo "Refusing to install cron from ephemeral path: $INSTALL_ROOT" echo "Set CRON_PROJECT_ROOT to a persistent checkout on the host, then rerun." exit 1 fi if [[ ! -f "$COLLECT_SCRIPT" || ! -f "$PRUNE_SNAPSHOTS" || ! -f "$PRUNE_HISTORY" ]]; then echo "One or more storage growth scripts are missing under: $INSTALL_ROOT" echo "Set CRON_PROJECT_ROOT to the host path that contains scripts/monitoring/collect-storage-growth-data.sh and prune helpers." exit 1 fi } case "${1:-}" in --install) validate_install_root mkdir -p "$LOG_DIR" { crontab -l 2>/dev/null | grep -v "collect-storage-growth-data.sh" | grep -v "prune-storage-snapshots.sh" | grep -v "prune-storage-history.sh" || true echo "$CRON_STORAGE" echo "$CRON_PRUNE" } | crontab - echo "Installed storage growth cron:" echo " $CRON_STORAGE" echo " $CRON_PRUNE" ;; --show) validate_install_root echo "Storage growth (append every 6h): $CRON_STORAGE" echo "Storage prune (weekly Sun 08:00): $CRON_PRUNE" ;; --remove) current=$(crontab -l 2>/dev/null || true) if echo "$current" | grep -qE "collect-storage-growth-data|prune-storage-snapshots|prune-storage-history"; then echo "$current" | grep -v "collect-storage-growth-data.sh" | grep -v "prune-storage-snapshots.sh" | grep -v "prune-storage-history.sh" | crontab - echo "Removed storage growth cron." else echo "No storage growth cron found in crontab." fi ;; *) echo "Usage: $0 [--install|--show|--remove]" exit 0 ;; esac