import base64 import json import os import re import subprocess PW = open("/root/.pg-superuser-pass").read().strip() OUT = "/tmp/n8n_import" os.makedirs(OUT, exist_ok=True) DBS = ["n8n_big", "n8n_restore", "n8n_tmp"] SQL = ("copy (select replace(encode(convert_to(json_build_object(" "'name',name,'nodes',nodes,'connections',connections,'settings',settings" ")::text,'UTF8'),'base64'), chr(10), '') from workflow_entity) to stdout;") def dump(db): env = dict(os.environ, PGPASSWORD=PW) r = subprocess.run(["psql", "-h", "192.168.1.151", "-U", "postgres", "-d", db, "-c", SQL], capture_output=True, text=True, timeout=180, env=env) if r.returncode != 0: print(" ошибка", db, r.stderr[:150]) return [ln.strip() for ln in r.stdout.splitlines() if len(ln.strip()) > 40] seen = set() total = 0 for db in DBS: rows = dump(db) print(f" {db}: {len(rows)}") for line in rows: try: row = json.loads(base64.b64decode(line).decode("utf-8")) except Exception as e: print(" decode:", str(e)[:80]) continue name = (row.get("name") or "workflow").strip() if name in seen: continue seen.add(name) def parse(v): if isinstance(v, str): try: return json.loads(v) except Exception: return {} return v or {} wf = {"name": name, "nodes": parse(row.get("nodes")) or [], "connections": parse(row.get("connections")) or {}, "settings": parse(row.get("settings")) or {}, "staticData": None, "pinData": {}, "active": False} slug = re.sub(r"[^a-zA-Z0-9]+", "_", name)[:40].strip("_").lower() or f"wf{total}" with open(f"{OUT}/{slug}.json", "w", encoding="utf-8") as f: json.dump(wf, f, ensure_ascii=False) total += 1 print(f" {name} ({len(wf['nodes'])} нод)") print("итого уникальных:", total)