#!/bin/bash # Backup script for litellm-db and langfuse-db (openai stack postgres containers). # litellm-db holds provisioned virtual keys + spend; langfuse-db holds all traces. # Mirrors the seafile/vaultwarden backup.sh pattern (same repo): dump via # `docker exec pg_dump`, gzip, retention of last 5. Uses pg_dump (safe # against a live/running DB, no downtime needed — unlike gitea's stop-the-world dump). # Backup-freshness monitored via .age items. # # Run every 3 days via root crontab (same schedule as vaultwarden/seafile), e.g.: # 0 3 */3 * * /home/alvis/agap_git/ai/backup-llm-dbs.sh >> /mnt/backups/openai-llm-dbs/backup.log 2>&1 # # Restore (litellm-db example, langfuse-db is identical with its own container/user/db): # gunzip -c /mnt/backups/openai-llm-dbs//litellm-db.sql.gz | \ # docker exec -i litellm-db psql -U litellm -d litellm # # For langfuse-db: # gunzip -c /mnt/backups/openai-llm-dbs//langfuse-db.sql.gz | \ # docker exec -i langfuse-db psql -U langfuse -d langfuse # # If restoring into a fresh/empty DB, first drop+recreate the DB (or restore # # to a new container) since the dump is a plain SQL dump, not --clean. set -euo pipefail BACKUP_DIR="/mnt/backups/openai-llm-dbs" DATE=$(date '+%Y%m%d-%H%M') DEST="$BACKUP_DIR/$DATE" mkdir -p "$DEST" # Backup-freshness monitoring is now done via .age items (calculated fields showing # age of the backup). The .ts (timestamp) trappers were unreliable (history.push not # landing); removed in kb#189 in favor of .age overdue triggers. # --- litellm-db --- echo "Dumping litellm-db..." docker exec litellm-db pg_dump -U litellm litellm | gzip > "$DEST/litellm-db.sql.gz" echo "Dumped: litellm-db -> $DEST/litellm-db.sql.gz" # --- langfuse-db --- echo "Dumping langfuse-db..." docker exec langfuse-db pg_dump -U langfuse langfuse | gzip > "$DEST/langfuse-db.sql.gz" echo "Dumped: langfuse-db -> $DEST/langfuse-db.sql.gz" echo "$(date): Backup complete: $DEST" ls -la "$DEST/" # Rotate: keep last 5 backups ls -1dt "$BACKUP_DIR"/[0-9]*-[0-9]* 2>/dev/null | tail -n +6 | xargs -r rm -rf