# -*- coding: utf-8 -*- """Backfill PB sessions/events/blacklist on dylive — no switch changes.""" from __future__ import annotations import sys import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") REMOTE = r''' import asyncio, os, sys os.chdir("/opt/douyinWEB/backend") # load systemd env from api import subprocess pid=subprocess.getoutput("systemctl show douyinweb-api -p MainPID --value").strip() for x in open(f"/proc/{pid}/environ","rb").read().decode().split("\0"): if "=" in x: k,v=x.split("=",1) if k.startswith("DY_"): os.environ[k]=v sys.path.insert(0,".") from app import db, pb_sync async def main(): await pb_sync.ensure_collections() # more sessions (offset via repeated list — list_sessions is recent-first; push up to 5000) rows = (db.list_sessions(limit=5000, offset=0).get("sessions") or []) ok=fail=0 for s in rows: try: await pb_sync.sync_session(s); ok+=1 except Exception: fail+=1 print("sessions", ok, fail, "attempted", len(rows)) # blacklist if helper exists bl_ok=bl_fail=0 try: items = db.list_blacklist(limit=5000, offset=0) if hasattr(db, "list_blacklist") else None if items is None: # raw import sqlite3 con=sqlite3.connect("data/monitor.db"); con.row_factory=sqlite3.Row items=[dict(r) for r in con.execute("select * from blacklist").fetchall()] for b in items or []: try: fn=getattr(pb_sync, "sync_blacklist", None) or getattr(pb_sync, "sync_blacklist_entry", None) if fn: await fn(b); bl_ok+=1 else: # best-effort create in dy_blacklist pass except Exception: bl_fail+=1 print("blacklist", bl_ok, bl_fail, "n", len(items or [])) except Exception as e: print("blacklist_skip", e) # recent events only (cap) — historical full dump not required for ops health ev_ok=ev_fail=0 events = db.list_events(limit=2000, offset=0) if hasattr(db, "list_events") else [] # list_events signature may differ if isinstance(events, dict): events = events.get("events") or events.get("items") or [] for e in events or []: try: await pb_sync.sync_event(e); ev_ok+=1 except Exception: ev_fail+=1 print("events", ev_ok, ev_fail, "attempted", len(events or [])) h = await pb_sync.sync_health() print("health", {k:h.get(k) for k in ("ok","in_sync","max_abs_delta")}) print("drift", h.get("drift")) asyncio.run(main()) ''' c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=45, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() with sftp.file("/tmp/_pb_backfill.py", "w") as f: f.write(REMOTE.replace("\r\n", "\n")) sftp.close() _, o, e = c.exec_command("/opt/douyinWEB/backend/.venv/bin/python -u /tmp/_pb_backfill.py", timeout=600) print((o.read() + e.read()).decode("utf-8", "replace")) c.close()