#!/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)" COLLECT_SCRIPT="$PROJECT_ROOT/scripts/monitoring/collect-storage-growth-data.sh" PRUNE_SNAPSHOTS="$PROJECT_ROOT/scripts/monitoring/prune-storage-snapshots.sh" PRUNE_HISTORY="$PROJECT_ROOT/scripts/monitoring/prune-storage-history.sh" LOG_DIR="$PROJECT_ROOT/logs/storage-growth" # Every 6 hours CRON_STORAGE="0 */6 * * * cd $PROJECT_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 $PROJECT_ROOT && bash $PRUNE_SNAPSHOTS >> $LOG_DIR/cron.log 2>&1 && bash $PRUNE_HISTORY >> $LOG_DIR/cron.log 2>&1" case "${1:-}" in --install) mkdir -p "$LOG_DIR" added="" if ! crontab -l 2>/dev/null | grep -q "collect-storage-growth-data.sh"; then (crontab -l 2>/dev/null; echo "$CRON_STORAGE") | crontab - added="collect" fi if ! crontab -l 2>/dev/null | grep -q "prune-storage-snapshots.sh"; then (crontab -l 2>/dev/null; echo "$CRON_PRUNE") | crontab - added="${added:+$added + }prune" fi if [ -n "$added" ]; then echo "Installed storage growth cron:" echo " $CRON_STORAGE" echo " $CRON_PRUNE" else echo "Storage growth cron already present in crontab." fi ;; --show) 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