Payment event ledger (PaymentEvent) replicated to the backup server, plus CSV export script on 247

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-13 14:18:47 +03:30
parent 947f946909
commit 8d8665edf3
10 changed files with 199 additions and 3 deletions

View File

@@ -0,0 +1,53 @@
#!/bin/bash
# روی سرورِ بکاپ (۲۴۷). دفترِ رویدادهای خرید (payment_paymentevent) و سفارش/تراکنش را از روی همین
# بکاپ (آخرین نسخه‌ی کامل + همه‌ی WALِ رسیده) به CSVِ خوانا در `payment-log/` کنارِ `base/` و `wal/` می‌نویسد.
# هیچ اتصالی به ۲۴۹ یا Postgresِ دیگرانِ این ماشین ندارد: همان روشِ restore-check.sh، نمونه‌ی موقت روی
# سوکتِ داخلِ پوشه (بی‌پورتِ شبکه) که آخرش پاک می‌شود. فقط زیرِ ~/zareian-pgbackup می‌نویسد.
# خروجی: payment-log/payment-events-<UTC>.csv، orders-…csv، transactions-…csv؛ ۱۴ فایلِ آخرِ هر نوع می‌ماند.
# توجه: نسخه‌ی کامل تا چند دقیقه‌ی WAL عقب‌تر از ۲۴۹ می‌شود، ولی WALِ رسیده همه اعمال می‌شود.
set -euo pipefail
ROOT=/home/administrator/zareian-pgbackup
BIN=/usr/lib/postgresql/14/bin
OUT="$ROOT/payment-log"
mkdir -p "$OUT"
# دیسکِ این ماشین مشترک است؛ زیرِ ۳ گیگ فضای آزاد، شروع نمی‌کنیم.
free_kb=$(df --output=avail -k "$ROOT" | tail -1)
[ "$free_kb" -gt 3000000 ] || { echo "disk too full ($free_kb KB free); skipping" >&2; exit 1; }
latest=$(ls -1d "$ROOT"/base/2* | tail -1)
work=$(mktemp -d "$ROOT/export-events.XXXXXX")
trap '"$BIN/pg_ctl" -D "$work/data" -m immediate stop >/dev/null 2>&1 || true; rm -rf "$work"' EXIT
cp -a "$latest" "$work/data"
mkdir "$work/wal"
cp "$ROOT"/wal/* "$work/wal/"
for f in "$work"/wal/*.partial; do [ -e "$f" ] && mv "$f" "${f%.partial}"; done
cat > "$work/data/postgresql.conf" <<CONF
listen_addresses = ''
unix_socket_directories = '$work'
restore_command = 'cp $work/wal/%f %p'
recovery_target_action = 'promote'
CONF
echo 'local all all trust' > "$work/data/pg_hba.conf"
: > "$work/data/postgresql.auto.conf"
touch "$work/data/recovery.signal"
"$BIN/pg_ctl" -D "$work/data" -l "$work/log" -w -t 600 start >/dev/null
for _ in $(seq 120); do
[ "$("$BIN/psql" -h "$work" -U postgres -d zareian_mathapp -Atc 'select pg_is_in_recovery()')" = f ] && break
sleep 1
done
stamp=$(date -u +%Y%m%dT%H%M%SZ)
q() { "$BIN/psql" -h "$work" -U postgres -d zareian_mathapp -c "\\copy ($1) to '$2' csv header"; }
q "select id, created_at, order_id, user_id, provider, event, detail from payment_paymentevent order by id" "$OUT/payment-events-$stamp.csv"
q "select id, created_at, user_id, plan_id, plan_title, amount, status, provider, gateway_authority, subscription_id from payment_order order by id" "$OUT/orders-$stamp.csv"
q "select id, created_at, order_id, gateway, gateway_ref_id, status, paid_at from payment_transaction order by id" "$OUT/transactions-$stamp.csv"
chmod 600 "$OUT"/*-"$stamp".csv
for kind in payment-events orders transactions; do
ls -1t "$OUT/$kind"-*.csv | tail -n +15 | xargs -r rm -f --
done
echo "wrote $OUT/*-$stamp.csv from base $(basename "$latest")"

View File

@@ -0,0 +1,7 @@
[Unit]
Description=Zareian MathApp: export payment events from the backup copy to CSV
[Service]
Type=oneshot
User=administrator
ExecStart=/home/administrator/zareian-pgbackup/bin/export-payment-events.sh

View File

@@ -0,0 +1,9 @@
[Unit]
Description=Zareian MathApp: payment events CSV every 6 hours
[Timer]
OnCalendar=*-*-* 01,07,13,19:15:00
Persistent=true
[Install]
WantedBy=timers.target