# -*- coding: utf-8 -*- """Restore shorts_channels (and jobs) from stash@{1} on server""" import paramiko HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" DB_PATH = "/opt/douyinWEB/backend/data/monitor.db" def ssh_run(c, cmd, timeout=120): _stdin, stdout, stderr = c.exec_command(cmd, timeout=timeout) return stdout.read().decode("utf-8", "replace").strip(), stderr.read().decode("utf-8", "replace").strip() RESTORE_PY = r""" import sqlite3, sys src_path = '/tmp/monitor_stash1.db' dst_path = '/opt/douyinWEB/backend/data/monitor.db' src = sqlite3.connect(src_path) src.row_factory = sqlite3.Row dst = sqlite3.connect(dst_path) # --- shorts_channels --- cols_info = src.execute('PRAGMA table_info(shorts_channels)').fetchall() col_names = [r[1] for r in cols_info] ph = ','.join(['?'] * len(col_names)) rows = src.execute('SELECT * FROM shorts_channels').fetchall() dst.execute('DELETE FROM shorts_channels') if rows: dst.executemany(f'INSERT OR REPLACE INTO shorts_channels VALUES ({ph})', [tuple(r) for r in rows]) print(f'shorts_channels restored: {len(rows)}') # --- channel_groups (if any) --- try: cg_cols = src.execute('PRAGMA table_info(channel_groups)').fetchall() cg_col_names = [r[1] for r in cg_cols] cg_ph = ','.join(['?'] * len(cg_col_names)) cg_rows = src.execute('SELECT * FROM channel_groups').fetchall() if cg_rows: dst.execute('DELETE FROM channel_groups') dst.executemany(f'INSERT OR REPLACE INTO channel_groups VALUES ({cg_ph})', [tuple(r) for r in cg_rows]) print(f'channel_groups restored: {len(cg_rows)}') except Exception as e: print(f'channel_groups skip: {e}') # --- shorts_jobs (if any, preserve existing) --- try: sj_cols = src.execute('PRAGMA table_info(shorts_jobs)').fetchall() sj_col_names = [r[1] for r in sj_cols] sj_ph = ','.join(['?'] * len(sj_col_names)) sj_rows = src.execute('SELECT * FROM shorts_jobs').fetchall() if sj_rows: for r in sj_rows: try: dst.execute(f'INSERT OR IGNORE INTO shorts_jobs VALUES ({sj_ph})', tuple(r)) except Exception: pass print(f'shorts_jobs merged: {len(sj_rows)}') except Exception as e: print(f'shorts_jobs skip: {e}') dst.commit() src.close() dst.close() print('DONE') """ def main(): 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) print("=== Extract stash@{1} monitor.db ===") out, err = ssh_run( c, "cd /opt/douyinWEB && git show 'stash@{1}':backend/data/monitor.db > /tmp/monitor_stash1.db 2>&1; echo $?", timeout=60, ) print(out) if err: print("err:", err[:200]) # Verify it has shorts_channels out, err = ssh_run( c, "python3 -c \"import sqlite3; conn=sqlite3.connect('/tmp/monitor_stash1.db'); print('tables:', [r[0] for r in conn.execute(\\\"SELECT name FROM sqlite_master WHERE type='table'\\\").fetchall()])\"", ) print("Tables in stash DB:", out) # Write restore script to server print("\n=== Writing restore script ===") sftp = c.open_sftp() with sftp.open("/tmp/_do_restore.py", "w") as f: f.write(RESTORE_PY) sftp.close() print("=== Running restore ===") out, err = ssh_run(c, "python3 /tmp/_do_restore.py", timeout=60) print(out) if err: print("err:", err[:300]) # Verify print("\n=== Verify counts ===") for tbl in ["shorts_channels", "channel_groups", "shorts_jobs"]: out, err = ssh_run( c, f"python3 -c \"import sqlite3; conn=sqlite3.connect('{DB_PATH}'); print(conn.execute('select count(*) from {tbl}').fetchone()[0])\"", ) print(f" {tbl}: {out}") # Also show shorts_channels data out, err = ssh_run( c, f"python3 -c \"import sqlite3; conn=sqlite3.connect('{DB_PATH}'); rows=conn.execute('SELECT id, channel_id, name, enabled FROM shorts_channels').fetchall(); [print(dict(zip(['id','channel_id','name','enabled'], r))) for r in rows]\"", ) print("\n=== shorts_channels rows ===") print(out) # Restart API print("\n=== Restart API ===") out, err = ssh_run(c, "systemctl restart douyinweb-api --no-block; sleep 5; systemctl is-active douyinweb-api", timeout=30) print(out) c.close() print("\nALL DONE") if __name__ == "__main__": main()