#!/usr/bin/env python3 """Dump one PocketBase collection's schema to a JSON file. A safety net to run before any script that PATCHes a collection's field list. Reads only — it never writes to PocketBase. Credentials come from .env.local (or the environment): python3 scripts/pb-backup-collection.py SITES [outdir] """ import json import sys from datetime import datetime from pb_env import http, superuser_token COLLECTION = sys.argv[1] if len(sys.argv) > 1 else "SITES" OUTDIR = sys.argv[2] if len(sys.argv) > 2 else "/tmp" TOKEN = superuser_token() code, coll = http("GET", f"/api/collections/{COLLECTION}", token=TOKEN) if code != 200: sys.exit(f"could not read {COLLECTION}: {code} {coll}") stamp = datetime.now().strftime("%Y%m%d-%H%M%S") path = f"{OUTDIR}/{COLLECTION.lower()}-collection-{stamp}.json" with open(path, "w") as f: json.dump(coll, f, indent=2) print(f" fields: {', '.join(f['name'] for f in coll.get('fields', []))}") print(f" ✓ backup written to {path}")