#!/usr/bin/env bash # Nightly logical dump of platform.db + messenger.db from black. # Stored as zstd-compressed .sql.zst in $BACKUP_DIR. # Pruned to keep last 14 daily + last 8 weekly (Sundays) + last 6 monthly (1st). # # Run on BLACK (where the authoritative DBs live). # Typical schedule: daily @ 03:00 via systemd timer. set -euo pipefail BACKUP_DIR="${BACKUP_DIR:-/mnt/bigdisk/atlilith-backups/pg}" DATE="$(date +%Y%m%d_%H%M%S)" mkdir -p "$BACKUP_DIR" dump_db() { local name="$1" local port="$2" local out="$BACKUP_DIR/${name}_${DATE}.sql.zst" echo "==> dumping ${name} on :${port} → ${out}" pg_dump \ --host=localhost \ --port="$port" \ --username=platform \ --no-owner --no-acl \ --format=plain \ "$name" \ | zstd -3 -T0 -o "$out" echo " size: $(du -h "$out" | cut -f1)" } dump_db "platform" "25440" dump_db "messenger" "25441" # Retention: keep last 14 dailies. Older dailies get pruned unless they # fall on a Sunday (weekly) or the 1st of the month (monthly). echo "==> pruning old backups (keep 14d/8w/6m)" find "$BACKUP_DIR" -name "*.sql.zst" -mtime +14 -print | while read -r f; do fname=$(basename "$f") date_part="${fname#*_}"; date_part="${date_part%_*}" if [[ -n "$date_part" ]]; then day_of_week=$(date -d "${date_part:0:4}-${date_part:4:2}-${date_part:6:2}" +%u 2>/dev/null || echo 0) day_of_month="${date_part:6:2}" if [[ "$day_of_week" == "7" ]] && [[ $(date -d "${date_part:0:4}-${date_part:4:2}-${date_part:6:2}" +%s 2>/dev/null) -gt $(date -d "8 weeks ago" +%s) ]]; then echo " [keep weekly] $fname"; continue fi if [[ "$day_of_month" == "01" ]] && [[ $(date -d "${date_part:0:4}-${date_part:4:2}-${date_part:6:2}" +%s 2>/dev/null) -gt $(date -d "6 months ago" +%s) ]]; then echo " [keep monthly] $fname"; continue fi fi echo " [prune] $fname" rm -f "$f" done echo "done."