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>
110 lines
3.4 KiB
Python
110 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Restore shorts_channels from stash DB using column-name alignment"""
|
|
import paramiko
|
|
|
|
HOST = "107.173.30.245"
|
|
USER = "root"
|
|
PASSWORD = "Xiao4669805"
|
|
DB_PATH = "/opt/douyinWEB/backend/data/monitor.db"
|
|
|
|
RESTORE_PY = r"""
|
|
import sqlite3
|
|
|
|
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)
|
|
|
|
def get_cols(conn, table):
|
|
return [r[1] for r in conn.execute(f'PRAGMA table_info({table})').fetchall()]
|
|
|
|
def restore_table(table):
|
|
src_cols = get_cols(src, table)
|
|
dst_cols = get_cols(dst, table)
|
|
# use intersection in dst order
|
|
common = [c for c in dst_cols if c in src_cols]
|
|
if not common:
|
|
print(f'{table}: no common columns, skip')
|
|
return 0
|
|
ph = ','.join(['?'] * len(common))
|
|
col_list = ','.join(common)
|
|
rows = src.execute(f'SELECT {col_list} FROM {table}').fetchall()
|
|
dst.execute(f'DELETE FROM {table}')
|
|
if rows:
|
|
dst.executemany(f'INSERT OR REPLACE INTO {table} ({col_list}) VALUES ({ph})', [tuple(r) for r in rows])
|
|
print(f'{table}: restored {len(rows)} rows (cols: {common})')
|
|
return len(rows)
|
|
|
|
restore_table('shorts_channels')
|
|
restore_table('channel_groups')
|
|
|
|
# shorts_jobs: merge (INSERT OR IGNORE)
|
|
try:
|
|
src_cols = get_cols(src, 'shorts_jobs')
|
|
dst_cols = get_cols(dst, 'shorts_jobs')
|
|
common = [c for c in dst_cols if c in src_cols]
|
|
if common:
|
|
ph = ','.join(['?'] * len(common))
|
|
col_list = ','.join(common)
|
|
rows = src.execute(f'SELECT {col_list} FROM shorts_jobs').fetchall()
|
|
n = 0
|
|
for r in rows:
|
|
try:
|
|
dst.execute(f'INSERT OR IGNORE INTO shorts_jobs ({col_list}) VALUES ({ph})', tuple(r))
|
|
n += 1
|
|
except Exception:
|
|
pass
|
|
print(f'shorts_jobs: merged {n}/{len(rows)} rows')
|
|
except Exception as e:
|
|
print(f'shorts_jobs skip: {e}')
|
|
|
|
dst.commit()
|
|
src.close()
|
|
dst.close()
|
|
print('DONE')
|
|
"""
|
|
|
|
|
|
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()
|
|
|
|
|
|
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)
|
|
|
|
sftp = c.open_sftp()
|
|
with sftp.open("/tmp/_do_restore2.py", "w") as f:
|
|
f.write(RESTORE_PY)
|
|
sftp.close()
|
|
|
|
out, err = ssh_run(c, "python3 /tmp/_do_restore2.py", timeout=60)
|
|
print(out)
|
|
if err:
|
|
print("ERR:", err[:400])
|
|
|
|
print("\n=== Verify ===")
|
|
for tbl in ["shorts_channels", "channel_groups", "shorts_jobs"]:
|
|
out, _ = ssh_run(c, f"python3 -c \"import sqlite3; c=sqlite3.connect('{DB_PATH}'); print(c.execute('select count(*) from {tbl}').fetchone()[0])\"")
|
|
print(f" {tbl}: {out}")
|
|
|
|
# Show channel names
|
|
out, _ = ssh_run(c, f"python3 -c \"import sqlite3; c=sqlite3.connect('{DB_PATH}'); c.row_factory=sqlite3.Row; [print(dict(r)) for r in c.execute('SELECT * FROM shorts_channels').fetchall()]\"")
|
|
print("\n=== shorts_channels ===")
|
|
print(out)
|
|
|
|
print("\n=== Restart API ===")
|
|
out, _ = ssh_run(c, "systemctl restart douyinweb-api; sleep 5; systemctl is-active douyinweb-api", timeout=30)
|
|
print(out)
|
|
|
|
c.close()
|
|
print("DONE")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|