|
| 1 | +#!/bin/bash |
| 2 | +# Cleanup old snapshot files, bootstrap zips, and rotated logs. |
| 3 | +# Keeps only the 2 most recent files of each type to allow safe rollback. |
| 4 | +# Runs periodically via supervisord. |
| 5 | + |
| 6 | +DATA_DIR="/data/bob" |
| 7 | +INTERVAL=3600 # check every hour |
| 8 | + |
| 9 | +while true; do |
| 10 | + # Keep the 2 most recent spectrum files (by modification time), delete the rest |
| 11 | + SPECTRUM_COUNT=$(ls -t "$DATA_DIR"/spectrum.* 2>/dev/null | wc -l) |
| 12 | + if [ "$SPECTRUM_COUNT" -gt 2 ]; then |
| 13 | + ls -t "$DATA_DIR"/spectrum.* | tail -n +3 | xargs rm -f |
| 14 | + echo "$(date -u '+%Y-%m-%d %H:%M:%S') Cleaned old spectrum files (kept 2 of $SPECTRUM_COUNT)" |
| 15 | + fi |
| 16 | + |
| 17 | + # Keep the 2 most recent universe files, delete the rest |
| 18 | + UNIVERSE_COUNT=$(ls -t "$DATA_DIR"/universe.* 2>/dev/null | wc -l) |
| 19 | + if [ "$UNIVERSE_COUNT" -gt 2 ]; then |
| 20 | + ls -t "$DATA_DIR"/universe.* | tail -n +3 | xargs rm -f |
| 21 | + echo "$(date -u '+%Y-%m-%d %H:%M:%S') Cleaned old universe files (kept 2 of $UNIVERSE_COUNT)" |
| 22 | + fi |
| 23 | + |
| 24 | + # Remove all bootstrap zip files (only needed for initial setup) |
| 25 | + ZIP_COUNT=$(ls "$DATA_DIR"/ep*.zip 2>/dev/null | wc -l) |
| 26 | + if [ "$ZIP_COUNT" -gt 0 ]; then |
| 27 | + rm -f "$DATA_DIR"/ep*.zip |
| 28 | + echo "$(date -u '+%Y-%m-%d %H:%M:%S') Removed $ZIP_COUNT bootstrap zip(s)" |
| 29 | + fi |
| 30 | + |
| 31 | + # Remove rotated log files (bob.1.log, bob.2.log, etc.) - keep only the active bob.log |
| 32 | + ROTATED_LOGS=$(ls "$DATA_DIR"/bob.[0-9]*.log 2>/dev/null | wc -l) |
| 33 | + if [ "$ROTATED_LOGS" -gt 0 ]; then |
| 34 | + rm -f "$DATA_DIR"/bob.[0-9]*.log |
| 35 | + echo "$(date -u '+%Y-%m-%d %H:%M:%S') Removed $ROTATED_LOGS rotated bob log(s)" |
| 36 | + fi |
| 37 | + |
| 38 | + sleep "$INTERVAL" |
| 39 | +done |
0 commit comments