This document explains how to use the YAML import/export functionality to backup and restore your Korgraph database.
Korgraph provides comprehensive YAML-based import/export functionality that allows you to:
Export all entities (companies, domains, contexts, systems, etc.) excluding layout positions:
# Using curl
curl http://localhost:3000/api/export/yaml > korgraph-export.yaml
# Using wget
wget http://localhost:3000/api/export/yaml -O korgraph-export.yamlWhat's included:
What's excluded:
Export all diagram layout positions separately:
# Using curl
curl http://localhost:3000/api/export/yaml-layouts > korgraph-layouts.yaml
# Using wget
wget http://localhost:3000/api/export/yaml-layouts -O korgraph-layouts.yamlWhat's included:
Import entities from a YAML file:
curl -X POST \
http://localhost:3000/api/import/yaml \
-H "Content-Type: application/x-yaml" \
--data-binary @korgraph-export.yamlBehavior:
Response Example:
{
"success": true,
"message": "Database import completed",
"stats": {
"total": 150,
"created": 120,
"updated": 30,
"failed": 0,
"errors": []
}
}Import layout positions from a YAML file:
curl -X POST \
http://localhost:3000/api/import/yaml-layouts \
-H "Content-Type: application/x-yaml" \
--data-binary @korgraph-layouts.yamlBehavior:
Create a complete backup of your database:
#!/bin/bash
DATE=$(date +%Y-%m-%d)
BACKUP_DIR="./backups/$DATE"
mkdir -p "$BACKUP_DIR"
# Export entities
curl http://localhost:3000/api/export/yaml > "$BACKUP_DIR/korgraph-export.yaml"
# Export layouts
curl http://localhost:3000/api/export/yaml-layouts > "$BACKUP_DIR/korgraph-layouts.yaml"
echo "Backup created in $BACKUP_DIR"Restore from a backup:
#!/bin/bash
BACKUP_DIR="./backups/2025-10-18"
# Import entities
curl -X POST \
http://localhost:3000/api/import/yaml \
-H "Content-Type: application/x-yaml" \
--data-binary "@$BACKUP_DIR/korgraph-export.yaml"
# Import layouts
curl -X POST \
http://localhost:3000/api/import/yaml-layouts \
-H "Content-Type: application/x-yaml" \
--data-binary "@$BACKUP_DIR/korgraph-layouts.yaml"
echo "Restore completed"Initialize a fresh database with predefined data:
#!/bin/bash
# 1. Stop the server
npm stop
# 2. Delete existing databases (optional - only if you want to start completely fresh)
rm -rf data/db/korgraph
rm -rf data/db/korgraph_layouts
# 3. Start the server (will create empty databases)
npm start
# Wait for server to start
sleep 3
# 4. Import your YAML files
curl -X POST \
http://localhost:3000/api/import/yaml \
-H "Content-Type: application/x-yaml" \
--data-binary @./my-architecture.yaml
curl -X POST \
http://localhost:3000/api/import/yaml-layouts \
-H "Content-Type: application/x-yaml" \
--data-binary @./my-layouts.yaml
echo "Fresh database initialized!"# Korgraph Database Export
# Generated: 2025-10-18T10:30:00.000Z
# Version: 1.0
# Note: Layout positions are exported separately to korgraph-layouts.yaml
export:
date: "2025-10-18T10:30:00.000Z"
version: "1.0"
elementCount: 150
data:
company:
- id: "company-001"
rev: "1-abc123"
name: "Example Corporation"
description: "Main company entity"
elementType: "company"
domain:
- id: "domain-001"
rev: "1-def456"
name: "Sales Domain"
description: "Handles all sales processes"
elementType: "domain"
companyId: "company-001"
# ... more element types# Korgraph Layout Positions Export
# Generated: 2025-10-18T10:30:00.000Z
# Version: 1.0
export:
date: "2025-10-18T10:30:00.000Z"
version: "1.0"
layoutCount: 25
layouts:
- diagramId: "company-001"
rev: "1-xyz789"
elements:
- elementId: "domain-001"
x: 100
y: 200
- elementId: "domain-002"
x: 400
y: 200
- diagramId: "domain-001"
rev: "1-abc123"
elements:
- elementId: "subdomain-001"
x: 50
y: 100
- elementId: "subdomain-002"
x: 250
y: 100When migrating between environments:
Export from source:
curl http://source-server:3000/api/export/yaml > export.yaml
curl http://source-server:3000/api/export/yaml-layouts > layouts.yamlTransfer files to target environment
Import to target:
curl -X POST http://target-server:3000/api/import/yaml \
-H "Content-Type: application/x-yaml" --data-binary @export.yaml
curl -X POST http://target-server:3000/api/import/yaml-layouts \
-H "Content-Type: application/x-yaml" --data-binary @layouts.yamlIf import fails for some elements, the API returns details:
{
"success": true,
"message": "Database import completed",
"stats": {
"total": 150,
"created": 145,
"updated": 0,
"failed": 5,
"errors": [
{
"id": "invalid-element-1",
"type": "company",
"error": "Missing required field: name"
}
]
}
}The import process automatically handles conflicts:
_rev fields are not imported (CouchDB assigns new revisions)Check server logs for detailed error messages:
npm start | grep "YAML Import"Ensure your YAML follows the exact format shown above. Common issues:
Ensure CouchDB is running and accessible:
curl http://localhost:5984Potential improvements: