Files
MathApp/ops/pgbackup/basebackup.sh
Zareian a9cccc6550 Live off-site Postgres copy on 247 replaces the nightly pg_dump; backup retention in deploy.sh
- ops/pgbackup: SSH tunnel + pg_receivewal (slot zareian_247) + daily verified pg_basebackup (keep 7)
  and a restore check, all run on 78.38.35.247 as systemd units
- /api/health reports offsiteBackup (slot active/lag/lost) and warns; OFFSITE_WAL_SLOT setting
- removed statuslog backup_db/backup_verify and their health warnings
- deploy.sh keeps 3 pre-deploy dumps, 3 local patches, drops manual backups after 30 days
- docs: operations backup section, security item 14 closed, CLAUDE.md status

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 16:53:10 +03:30

29 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
# نسخه‌ی کاملِ روزانه از Postgresِ ۲۴۹، روی ۲۴۷. هفت نسخه‌ی آخر می‌ماند و WALِ قدیمی‌تر از قدیمی‌ترینِ
# آن‌ها پاک می‌شود؛ پس بازیابی تا هر لحظه از هفت روزِ اخیر ممکن است و حجم محدود می‌ماند.
# --checkpoint=spread و --max-rate بار روی ۲۴۹ را کم نگه می‌دارند (آن‌جا سرورِ اصلی است).
# نسخه تا pg_verifybackup قبولش نکند جای قبلی‌ها را نمی‌گیرد و چیزی پاک نمی‌شود.
set -euo pipefail
ROOT=/home/administrator/zareian-pgbackup
BIN=/usr/lib/postgresql/14/bin
KEEP=7
cd "$ROOT"
umask 077
ts=$(date +%Y%m%d-%H%M%S)
rm -rf base/.inprogress-*
tmp="base/.inprogress-$ts"
"$BIN/pg_basebackup" -h 127.0.0.1 -p 15440 -U zareian_replicator --no-password \
-D "$tmp" -Fp -X stream --checkpoint=spread --max-rate=20M
"$BIN/pg_verifybackup" "$tmp"
mv "$tmp" "base/$ts"
ls -1d base/2* | head -n -"$KEEP" | xargs -r rm -rf
oldest=$(ls -1d base/2* | head -1)
start_wal=$(sed -n 's/^START WAL LOCATION: .* (file \([0-9A-F]*\))$/\1/p' "$oldest/backup_label")
[ -n "$start_wal" ] || { echo "START WAL در $oldest/backup_label پیدا نشد" >&2; exit 1; }
"$BIN/pg_archivecleanup" wal "$start_wal"
echo "ok base/$ts ($(du -sh "base/$ts" | cut -f1)), kept $(ls -1d base/2* | wc -l), wal since $start_wal ($(du -sh wal | cut -f1))"