""" Search ALL git commits and stashes on server for channel_groups data. Then restore the best one found. """ import paramiko HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" DB_PATH = "/opt/douyinWEB/backend/data/monitor.db" FIND_PY = r""" import sqlite3, subprocess, os, tempfile repo = '/opt/douyinWEB' db_rel = 'backend/data/monitor.db' def count_cg(db_file): try: conn = sqlite3.connect(db_file) r = conn.execute("SELECT count(*) FROM channel_groups").fetchone() conn.close() return r[0] if r else 0 except: return 0 def get_cg_rows(db_file): try: conn = sqlite3.connect(db_file) conn.row_factory = sqlite3.Row rows = conn.execute("SELECT * FROM channel_groups").fetchall() conn.close() return [dict(r) for r in rows] except: return [] best = {'count': 0, 'source': '', 'rows': []} # Check stashes result = subprocess.run(['git', '-C', repo, 'stash', 'list'], capture_output=True, text=True) stashes = [l.split(':')[0] for l in result.stdout.strip().splitlines() if l.strip()] print(f"Stashes: {stashes}") for s in stashes: with tempfile.NamedTemporaryFile(suffix='.db', delete=False) as f: tmp = f.name ret = subprocess.run( f"cd {repo} && git show '{s}':{db_rel} > {tmp} 2>/dev/null", shell=True, capture_output=True ) c = count_cg(tmp) print(f" {s}: channel_groups={c}") if c > best['count']: best = {'count': c, 'source': s, 'rows': get_cg_rows(tmp)} os.unlink(tmp) # Check all git commits result = subprocess.run(['git', '-C', repo, 'log', '--all', '--format=%H %s', '--', db_rel], capture_output=True, text=True) commits = result.stdout.strip().splitlines() print(f"\nCommits touching monitor.db: {len(commits)}") for line in commits: sha = line.split()[0] with tempfile.NamedTemporaryFile(suffix='.db', delete=False) as f: tmp = f.name ret = subprocess.run( f"cd {repo} && git show '{sha}':{db_rel} > {tmp} 2>/dev/null", shell=True, capture_output=True ) c = count_cg(tmp) print(f" {sha[:8]}: channel_groups={c}") if c > best['count']: best = {'count': c, 'source': sha, 'rows': get_cg_rows(tmp)} os.unlink(tmp) print(f"\nBest source: {best['source']} with {best['count']} channel_groups") if best['rows']: for r in best['rows']: print(f" {r.get('name')} | {r.get('platform')} | {r.get('channel_url','')[:50]}") else: print(" No channel_groups found anywhere in git history!") print(" -> channel_groups data is permanently lost from git.") print(" -> User needs to re-add YouTube channels manually.") """ 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) sftp = c.open_sftp() with sftp.open("/tmp/_find_cg.py", "w") as f: f.write(FIND_PY) sftp.close() _s, out, err = c.exec_command("python3 /tmp/_find_cg.py 2>&1", timeout=120) print(out.read().decode("utf-8", "replace").strip()) c.close()