Skip to content

Backup and restore

A Spongram server keeps its state in two databases and a few secrets. This page lists what to back up, gives the procedures for Postgres and Neo4j, explains how to restore, and describes the per-brain export and import, which is a logical backup with different guarantees. It ends with purge and deletion.

What to back up

Item Where it lives Why it matters
Postgres /data/spongram/postgres (Compose, Swarm) or your managed database Instances and their group_id, hashed brain keys, the ingestion queue, the inference configuration with the encrypted provider key, personas, skills, project aliases, wiki pages, usage. With the Cloud control plane: organisations, accounts, access rights, licences, revocations and withdrawal records
Neo4j /data/spongram/neo4j or the volume of the Neo4j StatefulSet The graph of every brain: episodes (the text of each memory), entities, facts, summaries, the code map
SPONGRAM_ENCRYPTION_KEY Your secret store Decrypts the provider key and, where used, the per-request inference keys kept in the queue. If the variable was never set, the key is derived from SPONGRAM_ADMIN_TOKEN: back that up instead, and do not change it
The other secrets and the configuration .env, stack variables, Helm values and Kubernetes Secrets SPONGRAM_ADMIN_TOKEN, SPONGRAM_INTERNAL_AUTH_TOKEN, database passwords, public URL, feature flags
Graphiti receipt spool /data/spongram/execution-receipts or the receipts volume Cloud control plane only: durable receipts of extraction jobs
Organisation marketplace catalogues Your own repository Mounted read-only; restore them from their source

Brain keys cannot be recovered from a backup in clear: only their SHA-256 hashes are stored. A restored Postgres keeps every existing key valid, because clients still hold the clear value.

The two databases reference each other through group_id. Back them up together, at a moment when ingestion is idle: components.pending_episodes_worker.queue_depth in /healthz is 0 and Graphiti has finished the extractions it was given. A depth of 0 only says that every memory was forwarded; extraction continues for a short while after that. A full product backup is both databases plus the encryption key. An export of brains alone is not.

Postgres

Dump the database with the standard tools. With the Compose file's default names:

docker compose exec -T postgres pg_dump -U spongram -d spongram -Fc > spongram-pg-$(date +%Y%m%dT%H%M%S).dump

On Swarm, run the same pg_dump inside the task of spongram_postgresspg. On a managed database, use its snapshot or point-in-time recovery feature. Schedule it; the dump is online and does not interrupt the service.

Restore into an empty database:

docker compose stop spongram graphiti-mcp
docker compose exec -T postgres pg_restore -U spongram -d spongram --clean --if-exists < spongram-pg-<timestamp>.dump
docker compose start graphiti-mcp spongram

The dump contains the migration journal. Start the same release that produced it, or a newer one, which then applies only the migrations that are missing (Upgrades and migrations).

Neo4j

Neo4j Community has no online backup: a consistent dump requires the database to be stopped. While it is stopped, /readyz answers 503, searches fail, and new memories stay in the ingestion queue until the graph is back.

On Compose: scripts/backup_neo4j.sh

The script finds the neo4j container of a Compose project by its labels, then:

  1. stops the database inside the container;
  2. runs neo4j-admin database dump neo4j into a temporary directory of the data volume;
  3. copies the result to $BACKUP_DIR/neo4j-<timestamp>.dump;
  4. starts the database and waits until it answers;
  5. deletes dumps older than RETAIN_DAYS.
Variable Default Meaning
BACKUP_DIR ./backups/neo4j Host directory that receives the dumps
RETAIN_DAYS 14 Age after which dumps are deleted. 0 keeps everything
COMPOSE_PROJECT spongram Compose project name used to find the container
BACKUP_DIR=/var/backups/spongram ./scripts/backup_neo4j.sh
# crontab: every day at 03:00
0 3 * * * BACKUP_DIR=/var/backups/spongram /opt/spongram/scripts/backup_neo4j.sh >> /var/log/spongram-backup.log 2>&1

Run it once by hand and restore the result on a test stack before you rely on the schedule.

On Swarm or Kubernetes: archive the stopped volume

The script only knows Compose. Elsewhere, stop the Neo4j service, archive its data directory, and start it again:

docker service scale spongram_neo4jspg=0
tar -C /data/spongram -czf /var/backups/spongram/neo4j-$(date +%Y%m%dT%H%M%S).tgz neo4j
docker service scale spongram_neo4jspg=1

Run the tar on the node that holds the directory. While Neo4j is down the healthcheck of spongram fails and Swarm restarts that task until the graph is back: treat the backup as a short maintenance window. On Kubernetes, scale the spongram-neo4j StatefulSet to zero and snapshot its volume with the tools of your storage provider.

Restore

A restore replaces the whole database: every brain returns to the state of the backup.

Order

Restore Postgres first, then Neo4j, then start the services. Postgres holds the mapping from instances to group_id; a graph restored without it contains namespaces that no instance points to.

Neo4j from a dump (Compose)

docker compose stop spongram graphiti-mcp neo4j

NEO4J=$(docker ps -aq -f "label=com.docker.compose.project=spongram" \
                      -f "label=com.docker.compose.service=neo4j")
docker cp /var/backups/spongram/neo4j-<timestamp>.dump "$NEO4J:/data/neo4j.dump"

docker start "$NEO4J"
docker exec "$NEO4J" neo4j stop || true
docker exec "$NEO4J" neo4j-admin database load neo4j --from-path=/data --overwrite-destination=true
docker exec "$NEO4J" rm /data/neo4j.dump
docker exec "$NEO4J" neo4j start

docker compose start graphiti-mcp spongram

The file must be named neo4j.dump inside the container: neo4j-admin database load neo4j looks for <database>.dump in --from-path.

Neo4j from a volume archive

Stop the Neo4j service, move the current data directory aside, extract the archive in its place, start the service. Restore into a new directory or volume when you can, and keep the old one until the checks below pass.

Checks

curl -fsS http://localhost:8091/readyz
curl -sS -H "Authorization: Bearer <admin-token>" \
  http://localhost:8091/admin/api/spongram/instances

In the Neo4j Browser, count nodes per brain:

MATCH (n) WHERE n.group_id IS NOT NULL
RETURN n.group_id AS group_id, count(*) AS nodes
ORDER BY nodes DESC LIMIT 20;

Each group_id should belong to an instance listed by the admin API. Then run a search from a client with an existing key.

When the two backups are not from the same moment

  • Graph older than Postgres. The ingestion queue in Postgres still knows the memories accepted after the graph backup. Its reconciler re-submits named memories accepted within the last 30 days whose episode is missing from the graph, so those are extracted again without intervention. Older ones are not, and neither are memories that were deleted on purpose.
  • Postgres older than the graph. Instances created after the Postgres backup are gone, and their data remains in Neo4j as orphan namespaces. Remove one with POST /admin/api/spongram/orphans/{group_id}/purge; the route answers 409 if an instance still uses that group_id.
  • Deletions made after the backup come back. A restore returns memories, keys and, with the Cloud control plane, access rights to their state at backup time. Before reopening the service, replay the deletions and revocations made since, or you make withdrawn data and revoked keys usable again.

Per-brain export and import

The admin UI offers an export and an import card on each instance, backed by two routes:

Route Effect
GET /admin/api/spongram/instances/{id}/brain/export Returns a JSON file spongram-brain-<name>-<date>.json
POST /admin/api/spongram/instances/{id}/brain/import Queues the episodes of such a file for ingestion into the instance

The spongram-brain/v1 format

{
  "format": "spongram-brain/v1",
  "exported_at": "<timestamp>",
  "group_id": "sb_…",
  "embedding_dim": 1024,
  "counts": {"episodes": 0, "entities": 0, "edges": 0},
  "episodes": [
    {"uuid": "…", "name": "…", "episode_body": "…", "source": "text",
     "source_description": "project=… client=…", "created_at": "…", "valid_at": "…"}
  ],
  "entities": [],
  "edges": []
}

When the corresponding features are on, the file also carries personas, skills and project_aliases. A file without those keys imports normally.

What an import does

Episodes are the source of truth. The import validates the file, then queues each episode as if a client had called add_memory; Graphiti extracts entities and facts again with the current models. The entities and edges arrays are there for inspection and are not imported.

Consequences to know before using it as a backup:

  • It costs inference: every episode is extracted again, at the pace of the ingestion queue.
  • Only name, episode_body, source and source_description are replayed. Imported episodes get new identifiers and the date of the import; entities and facts are rebuilt, so their identifiers and summaries can differ from the original graph.
  • An episode without a project= tag is filed under project=global.
  • Imported personas are always inactive: activation remains an action of the agent.
  • The import does not deduplicate. Importing the same file twice into a brain that was not purged in between writes every episode twice.
Check Answer
format is not spongram-brain/v1, or the file has no episode (a file that carries only personas, skills or aliases is accepted when those features are on) 400
embedding_dim differs from the instance's 400: the dimension is immutable; import into an instance of the same dimension
More than 50 000 episodes 400
Body larger than 100 MB, or no Content-Length header 413
The instance is not active 409
Accepted 200 with {"enqueued": <count>, …}

Use export and import to move a brain to another deployment, to keep a readable copy of one brain, or to rebuild a graph after changing the embedder. Use database backups for disaster recovery.

To recover one brain from a full backup without rolling back the others, restore the two databases into a separate, isolated stack, export the brain there, and import it into the production instance after purging it.

Purge and deletion

Operation Route Effect
Purge a brain POST /admin/api/spongram/instances/{id}/purge Deletes every node and relationship carrying the brain's group_id, resets the instance counters, deletes its wiki pages. The instance and its key remain valid
Delete an instance DELETE /admin/api/spongram/instances/{id} Deletes the instance, its key and its rows in Postgres, and purges its graph. The SQL deletion is committed only once the graph purge succeeded: a failure (500) leaves everything in place and the request can be repeated
Purge an orphan namespace POST /admin/api/spongram/orphans/{group_id}/purge Deletes graph data whose instance no longer exists

The purge response reports deleted_nodes, deleted_rels and deleted_wiki_pages. With the Cloud control plane, purge and deletion are recorded as a durable request and answer with "state": "complete" or "state": "purge_pending" (deletion: 204 or 202); a pending purge is completed in the background.

A purge does not empty the ingestion queue

Without the Cloud control plane, the admin purge route removes the graph and leaves the ingestion queue untouched. The queue's reconciler treats an accepted memory that is missing from the graph as lost and submits it again, for memories accepted within the last 30 days. After a purge, recent memories can therefore be extracted again. The purge route has no option to prevent it. When a brain must stay empty, use one of these instead:

  • the owner of the brain calls the MCP tool clear_graph from a client, with the brain key: it erases the graph and retires every queue row of the brain;
  • delete the instance, which removes its queue rows with it, and create a new one;
  • for a handful of memories, retire their queue rows one by one with POST /admin/api/spongram/instances/{id}/ingestion/{row_id}/retire and a JSON body {"reason": "…"}.

With the Cloud control plane, a purge retires the queued work itself.

There is no undo for a purge or a deletion other than a restore. Export the brain first if there is any chance you will need it.