Files
momentum devandClaude Fable 5.1 3ea83d568b fix: run pg_dump/pg_restore windowless so closing a console cannot kill the nightly backup
The LiveDbBackup task runs under pythonw, but pg_dump.exe is a console program, so
Windows opened a visible console for it. On 2026-10-06 that window was closed and the
dump died with 0xC000013A before writing a byte (the Ross recap died the same way ten
minutes earlier). Pass CREATE_NO_WINDOW to both subprocess calls, the same fix the
judge paper trader got in 7e6f881.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LXxioW7HA18AY1gqnzA2HN
2026-10-08 12:58:53 -05:00

78 lines
3.7 KiB
Python

"""Nightly off-box backup of the LIVE (Tradier) Postgres, from the owner's workstation.
The live stack on TrueNAS has no db-backup service; the paper stack backs up only its own database. This keeps the
live test's full record (events, trades, settings) somewhere other than the TrueNAS pool.
- pg_dump -Fc (compressed, restorable with pg_restore) of the live database (DATABASE_URL host, port 5434)
- verified: pg_restore --list must read it and it must contain the events and config_overrides tables
- keeps the newest KEEP dumps (default 7) in LIVE_BACKUP_DIR (default C:\\Users\\Admin\\momentum-backups)
- writes <dir>/status.json for research.daily_report to alert on a missed or failed backup
usage: python deploy/backup_live_db.py (Task Scheduler "Momentum\\LiveDbBackup", nightly)
restore: pg_restore -d <empty database> --no-owner <dump>
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
import time
from datetime import datetime
from pathlib import Path
from urllib.parse import urlparse
from zoneinfo import ZoneInfo
# pg_dump/pg_restore are console programs: launched from pythonw they would pop a console window,
# and closing that window kills the dump (exit 0xC000013A, seen 2026-10-06). Run them windowless.
NO_WINDOW = getattr(subprocess, "CREATE_NO_WINDOW", 0)
ROOT = Path(__file__).resolve().parent.parent
PG_BIN = Path(os.environ.get("PG_BIN", r"C:\Users\Admin\pgsql18\bin"))
OUT = Path(os.environ.get("LIVE_BACKUP_DIR", r"C:\Users\Admin\momentum-backups"))
KEEP = int(os.environ.get("LIVE_BACKUP_KEEP", "7"))
PORT = int(os.environ.get("LIVE_DB_PORT", "5434"))
ET = ZoneInfo("America/New_York")
MUST_HAVE = ("TABLE DATA public events", "TABLE DATA public config_overrides")
def main() -> int:
from dotenv import dotenv_values
u = urlparse(dotenv_values(ROOT / ".env")["DATABASE_URL"])
OUT.mkdir(parents=True, exist_ok=True)
stamp = datetime.now(ET).strftime("%Y-%m-%d_%H%M")
dump = OUT / f"live_{stamp}.dump"
env = dict(os.environ, PGPASSWORD=u.password or "")
status = dict(at=datetime.now(ET).isoformat(timespec="seconds"), file=str(dump), ok=False)
t0 = time.time()
try:
subprocess.run([str(PG_BIN / "pg_dump.exe"), "-h", u.hostname, "-p", str(PORT), "-U", u.username,
"-d", u.path[1:], "-Fc", "-Z", "6", "-f", str(dump)],
env=env, check=True, capture_output=True, text=True, timeout=3600,
creationflags=NO_WINDOW)
listing = subprocess.run([str(PG_BIN / "pg_restore.exe"), "--list", str(dump)],
check=True, capture_output=True, text=True, timeout=600,
creationflags=NO_WINDOW).stdout
missing = [m for m in MUST_HAVE if m not in listing]
if missing:
raise RuntimeError(f"dump is missing {missing}")
status.update(ok=True, size_mb=round(dump.stat().st_size / 1e6, 1), seconds=round(time.time() - t0),
entries=listing.count("\n"))
except subprocess.CalledProcessError as e:
status["error"] = (e.stderr or str(e))[-400:]
except Exception as e: # noqa: BLE001
status["error"] = str(e)[-400:]
if not status["ok"] and dump.exists():
dump.unlink() # never keep a broken dump around as if it were good
dumps = sorted(OUT.glob("live_*.dump"))
for old in dumps[:-KEEP]:
old.unlink()
status["kept"] = [p.name for p in sorted(OUT.glob("live_*.dump"))]
(OUT / "status.json").write_text(json.dumps(status, indent=1), encoding="utf-8")
print(json.dumps(status, indent=1))
return 0 if status["ok"] else 1
if __name__ == "__main__":
sys.exit(main())