diff --git a/scripts/cutover-db.sh b/scripts/cutover-db.sh new file mode 100755 index 0000000..c24d25a --- /dev/null +++ b/scripts/cutover-db.sh @@ -0,0 +1,45 @@ +#!/bin/bash +set -euo pipefail + +cd "$(dirname "$0")/.." +REPO_ROOT="$(pwd)" +SERVER_DIR="$REPO_ROOT/server" + +if [ -z "${GNEXUS_BOOK_DATABASE_URL:-}" ]; then + echo "GNEXUS_BOOK_DATABASE_URL is required" >&2 + exit 2 +fi +export GNEXUS_BOOK_DATABASE_URL + +REPLACE_FLAG="${1:-}" +if [ -n "$REPLACE_FLAG" ] && [ "$REPLACE_FLAG" != "--replace" ]; then + echo "Usage: $0 [--replace]" >&2 + echo " --replace clears the target database before importing" >&2 + exit 2 +fi + +REPORT="${GNEXUS_BOOK_MIGRATION_REPORT:-$HOME/backup/gnexus-book/migration-report-$(date +%Y-%m-%d_%H%M%S).json}" + +cd "$SERVER_DIR" +.venv/bin/alembic upgrade head + +ARGS=(.venv/bin/python -m app.storage.cli migrate --manifest "$REPORT") +if [ "$REPLACE_FLAG" = "--replace" ]; then + ARGS+=(--replace) +fi + +"${ARGS[@]}" + +"$REPO_ROOT/scripts/backup-db.sh" + +echo +echo "Migration verified. Report: $REPORT" +echo "Database dumps are on the configured off-host backup target." +echo +echo "To activate database mode, set on the service:" +echo " GNEXUS_BOOK_STORAGE_BACKEND=db" +echo " GNEXUS_BOOK_DATABASE_URL=$GNEXUS_BOOK_DATABASE_URL" +echo "and restart gnexus-book-backend and gnexus-book-mcp." +echo +echo "Rollback: set GNEXUS_BOOK_STORAGE_BACKEND=file and restart." +echo "The Git checkout is untouched by this script." diff --git a/scripts/rollback-cutover.sh b/scripts/rollback-cutover.sh new file mode 100755 index 0000000..8cf1c2e --- /dev/null +++ b/scripts/rollback-cutover.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -euo pipefail + +cd "$(dirname "$0")/.." +SERVER_DIR="$(pwd)/server" + +cd "$SERVER_DIR" +GNEXUS_BOOK_STORAGE_BACKEND=file .venv/bin/python - <<'PY' +from app.config import Settings +from app.docs_repository import DocsRepository +from app.validation import validate_repository + +settings = Settings(storage_backend="file") +report = validate_repository(settings) +documents = DocsRepository(settings).list_docs() +print(f"file validation: {report['status']} ({report['issue_count']} issues)") +print(f"documents readable: {len(documents)}") +raise SystemExit(0 if report["status"] == "ok" else 1) +PY + +echo +echo "The file backend is readable and validated." +echo "Set GNEXUS_BOOK_STORAGE_BACKEND=file on the service and restart" +echo "gnexus-book-backend and gnexus-book-mcp to roll back." +echo "If the database itself must be restored, use scripts/restore-db.sh" +echo "with the pre-cutover dump and GNEXUS_BOOK_RESTORE_YES=1." diff --git a/server/.env.example b/server/.env.example index 48db63e..e440692 100644 --- a/server/.env.example +++ b/server/.env.example @@ -1,4 +1,5 @@ -# Storage backend: file (default) or db +# Storage backend: file (default) or db. +# The systemd units load this file via EnvironmentFile; switch to db after cutover. GNEXUS_BOOK_STORAGE_BACKEND=file # PostgreSQL URL used when the backend is db GNEXUS_BOOK_DATABASE_URL=postgresql+psycopg://gmikcon@127.0.0.1:5433/gnexus_book diff --git a/systemd/gnexus-book-backend.service b/systemd/gnexus-book-backend.service index 4b09556..a31a154 100644 --- a/systemd/gnexus-book-backend.service +++ b/systemd/gnexus-book-backend.service @@ -5,6 +5,7 @@ [Service] Type=simple WorkingDirectory=/home/ubuntu/gnexus-book/server +EnvironmentFile=-/home/ubuntu/gnexus-book/server/.env ExecStart=/home/ubuntu/gnexus-book/server/.venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 Restart=on-failure RestartSec=5 diff --git a/systemd/gnexus-book-mcp.service b/systemd/gnexus-book-mcp.service index c947ca4..8c2e8ba 100644 --- a/systemd/gnexus-book-mcp.service +++ b/systemd/gnexus-book-mcp.service @@ -5,6 +5,7 @@ [Service] Type=simple WorkingDirectory=/home/ubuntu/gnexus-book/server +EnvironmentFile=-/home/ubuntu/gnexus-book/server/.env Environment="GNEXUS_BOOK_REPO_ROOT=/home/ubuntu/gnexus-book" Environment="GNEXUS_BOOK_MCP_TRANSPORT=streamable-http" Environment="GNEXUS_BOOK_MCP_HOST=0.0.0.0"