douyinWEB/scripts/_emergency_check_prod_db.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

88 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
"""Read-only emergency check of production DB / services. No writes."""
import paramiko
import sys
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST = "107.173.30.245"
USER = "root"
PASSWORD = "Xiao4669805"
REPO = "/opt/douyinWEB"
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False)
def run(cmd: str, timeout: int = 90) -> str:
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace").strip()
print("=== services ===")
print(run("systemctl is-active douyinweb-api douyinweb-web"))
print(run("systemctl show douyinweb-api -p ActiveState -p SubState -p MainPID -p Environment -p ExecStart --no-pager"))
print("\n=== find db files ===")
print(run(f"find {REPO} -name '*.db' 2>/dev/null | head -50"))
print(run(f"ls -lah {REPO}/data 2>/dev/null | head -40"))
print(run(f"ls -lah {REPO}/backend/data 2>/dev/null | head -40"))
print("\n=== env / unit ===")
print(run(f"ls -la {REPO}/backend/.env {REPO}/.env 2>/dev/null"))
print(run("grep -nE 'DB|SQLITE|DATA|database' /opt/douyinWEB/backend/.env /opt/douyinWEB/.env 2>/dev/null | head -40"))
print(run("systemctl cat douyinweb-api 2>/dev/null | sed -n '1,80p'"))
print("\n=== row counts if db found ===")
print(run(r"""
python3 - <<'PY'
import os, sqlite3, glob
cands=[]
for p in [
'/opt/douyinWEB/data/app.db','/opt/douyinWEB/data/monitor.db','/opt/douyinWEB/backend/data/app.db',
'/opt/douyinWEB/backend/app.db','/opt/douyinWEB/data/douyin.db'
]:
if os.path.isfile(p): cands.append(p)
cands += glob.glob('/opt/douyinWEB/**/*.db', recursive=True)[:40]
seen=set()
for p in cands:
if p in seen: continue
seen.add(p)
try:
sz=os.path.getsize(p)
conn=sqlite3.connect(f'file:{p}?mode=ro', uri=True)
tables=[r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()]
interesting=[t for t in tables if any(x in t for x in ('channel','room','account','job','setting'))]
print(f'FILE {p} size={sz}')
for t in interesting[:25]:
try:
n=conn.execute(f'SELECT COUNT(*) FROM [{t}]').fetchone()[0]
print(f' {t}: {n}')
except Exception as e:
print(f' {t}: ERR {e}')
conn.close()
except Exception as e:
print(f'FILE {p} ERR {e}')
PY
"""))
print("\n=== backups ===")
print(run("ls -lah /opt/douyinWEB/data/backups 2>/dev/null | head -30"))
print(run("ls -lah /opt/douyinWEB/backups 2>/dev/null | head -30"))
print(run("find /opt/douyinWEB -name '*.db*' -mtime -7 2>/dev/null | head -40"))
print(run("ls -lah /root/*.db* /tmp/*.db* 2>/dev/null | head -20"))
print("\n=== api health / channels sample ===")
print(run("curl -sS -m 8 http://127.0.0.1:8000/api/health 2>&1 | head -c 800"))
print()
print(run("curl -sS -m 8 http://127.0.0.1:8000/api/shorts/channels 2>&1 | head -c 500"))
print()
print(run("curl -sS -m 8 'http://127.0.0.1:8000/api/rooms?limit=3' 2>&1 | head -c 500"))
print("\n=== recent journal ===")
print(run("journalctl -u douyinweb-api -n 40 --no-pager -o cat 2>/dev/null | tail -40"))
c.close()
print("\nDONE")