Files

47 lines
1.7 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Archive sessions older than 7 days to S3, keep archives 30 days, then delete.
set -euo pipefail
ARCHIVE_DIR="${HERMES_HOME:-$HOME/.hermes}/archives"
RETENTION_DAYS=30
AGE_DAYS=7
S3_BUCKET="hermes-archives"
S3_ENDPOINT="https://s3.keller-laurent.org"
DATE=$(date +%F)
ARCHIVE_FILE="sessions-${DATE}.jsonl"
mkdir -p "$ARCHIVE_DIR"
# 1. Export sessions older than AGE_DAYS to a dated archive file
if hermes sessions export "$ARCHIVE_DIR/$ARCHIVE_FILE" 2>/dev/null; then
echo "✅ Exported sessions to $ARCHIVE_DIR/$ARCHIVE_FILE"
# 2. Upload to S3
mc cp "$ARCHIVE_DIR/$ARCHIVE_FILE" "minio/$S3_BUCKET/" 2>/dev/null && \
echo "✅ Uploaded to s3://$S3_BUCKET/$ARCHIVE_FILE"
else
echo "️ No sessions to export (or export failed)"
fi
# 3. Prune sessions older than AGE_DAYS from the DB
yes | hermes sessions prune --older-than "$AGE_DAYS" 2>/dev/null && \
echo "✅ Pruned sessions older than ${AGE_DAYS} days" || \
echo "️ No sessions to prune"
# 4. Delete local archive files older than RETENTION_DAYS
find "$ARCHIVE_DIR" -name 'sessions-*.jsonl' -type f -mtime +"$RETENTION_DAYS" -delete 2>/dev/null
echo "✅ Cleaned local archives older than ${RETENTION_DAYS} days"
# 5. Clean old archives from S3 (older than RETENTION_DAYS)
mc find "minio/$S3_BUCKET" --name "sessions-*.jsonl" --older-than "${RETENTION_DAYS}d" \
--exec "mc rm {}" 2>/dev/null || true
echo "✅ Cleaned S3 archives older than ${RETENTION_DAYS} days"
# 6. Report
echo ""
echo "=== Archives dans s3://$S3_BUCKET ==="
mc ls "minio/$S3_BUCKET/" 2>/dev/null || echo "(empty)"
echo ""
echo "=== Archives locales ==="
ls -lh "$ARCHIVE_DIR"/ 2>/dev/null || echo "(empty)"