Skip to content

Recovery

This procedure restores Nyravio from a backup created with the Backup procedure. It includes comprehensive preflight verification before making any destructive changes.

  • The complete backup directory with all four files (backup.sql, backup-metadata.json, env.backup, uploads.tar)
  • The original NYRAVIO_ADMIN_ENCRYPTION_KEY in the backup’s env.backup
  • The exact Docker image recorded in backup metadata (available locally)
  • Required tools: docker, jq, curl, sha256sum

The restore procedure supports restoring into a compatible Nyravio deployment:

  • Same Compose project
  • Same PostgreSQL major version
  • Same database credentials
  • Matching resolved volume identities

Cross-version PostgreSQL restores, restores with changed database passwords, mismatched volume configurations, or restores into unrelated deployments are not supported.

Component Atomicity
Database restore Atomic — --single-transaction rolls back the entire operation on any failure
Uploads restore Not atomic — if extraction fails, re-run from the uploads step before starting the application

The procedure has two phases: preflight verification (read-only, no changes) and destructive operations (only after all preflight passes).

Terminal window
set -euo pipefail
# ── Point BACKUP_DIR at your backup ──
BACKUP_DIR="/path/to/nyravio-backup-XXXXXXXXXX"
# ── Preflight: all verification completes before any changes ──
# Required tools
for cmd in docker jq curl sha256sum; do
command -v "$cmd" > /dev/null 2>&1 || { echo "Error: $cmd not found"; exit 1; }
done
# Pin the supported Compose file
COMPOSE_YAML="$(pwd)/compose.yaml"
[ -f "$COMPOSE_YAML" ] || {
echo "Error: compose.yaml not found in $(pwd)"; exit 1
}
# Reject shell overrides
[ -z "${COMPOSE_FILE:-}" ] || {
echo "Error: COMPOSE_FILE is set — unset it"; exit 1
}
[ -z "${COMPOSE_ENV_FILES:-}" ] || {
echo "Error: COMPOSE_ENV_FILES is set — unset it"; exit 1
}
[ -z "${COMPOSE_PROJECT_NAME:-}" ] || {
echo "Error: COMPOSE_PROJECT_NAME is set — unset it"; exit 1
}
# Verify backup artifacts exist
for f in backup.sql backup-metadata.json env.backup uploads.tar; do
[ -f "$BACKUP_DIR/$f" ] || { echo "Error: missing: $BACKUP_DIR/$f"; exit 1; }
done
# Secure temporary files (cleaned up on exit)
CFG_CURRENT=$(mktemp) CFG_RESTORE=$(mktemp) CRED_TMP=$(mktemp)
chmod 600 "$CFG_CURRENT" "$CFG_RESTORE" "$CRED_TMP"
trap 'rm -f "$CFG_CURRENT" "$CFG_RESTORE" "$CRED_TMP"' EXIT
# Verify checksums
EXPECTED=$(jq -re '.dump_sha256' "$BACKUP_DIR/backup-metadata.json")
ACTUAL=$(sha256sum "$BACKUP_DIR/backup.sql" | awk '{print $1}')
[ "$ACTUAL" = "$EXPECTED" ] || {
echo "Error: backup.sql checksum mismatch"; exit 1
}
EXPECTED=$(jq -re '.uploads_sha256' "$BACKUP_DIR/backup-metadata.json")
ACTUAL=$(sha256sum "$BACKUP_DIR/uploads.tar" | awk '{print $1}')
[ "$ACTUAL" = "$EXPECTED" ] || {
echo "Error: uploads.tar checksum mismatch"; exit 1
}
EXPECTED=$(jq -re '.env_sha256' "$BACKUP_DIR/backup-metadata.json")
ACTUAL=$(sha256sum "$BACKUP_DIR/env.backup" | awk '{print $1}')
[ "$ACTUAL" = "$EXPECTED" ] || {
echo "Error: env.backup checksum mismatch"; exit 1
}
# Verify application image identity
STORED_IMAGE_ID=$(jq -re '.image_id' "$BACKUP_DIR/backup-metadata.json")
STORED_IMAGE=$(jq -re '.nyravio_image' "$BACKUP_DIR/backup-metadata.json")
RESTORE_IMAGE="${NYRAVIO_IMAGE:-$STORED_IMAGE}"
ACTUAL_IMAGE_ID=$(docker image inspect --format='{{.Id}}' "$RESTORE_IMAGE" 2>/dev/null) || {
echo "Error: image not found locally: $RESTORE_IMAGE"; exit 1
}
[ "$ACTUAL_IMAGE_ID" = "$STORED_IMAGE_ID" ] || {
echo "Error: image ID mismatch for $RESTORE_IMAGE"
echo " Expected: $STORED_IMAGE_ID"
echo " Actual: $ACTUAL_IMAGE_ID"; exit 1
}
export NYRAVIO_IMAGE="$RESTORE_IMAGE"
# Verify Compose configuration compatibility
docker compose --file "$COMPOSE_YAML" config --format json > "$CFG_CURRENT" 2>/dev/null
docker compose --file "$COMPOSE_YAML" --env-file "$BACKUP_DIR/env.backup" \
config --format json > "$CFG_RESTORE" 2>/dev/null
COMPOSE_PROJECT=$(jq -re '.name' "$CFG_CURRENT")
DC=(docker compose --file "$COMPOSE_YAML" --project-name "$COMPOSE_PROJECT" \
--project-directory "$(pwd)")
# Verify project identity, database settings, volume identities
RESTORE_PROJECT=$(jq -re '.name' "$CFG_RESTORE")
[ "$COMPOSE_PROJECT" = "$RESTORE_PROJECT" ] || {
echo "Error: Compose project mismatch"; exit 1
}
# Verify database user and name match
CURRENT_DB_USER=$(jq -re '.services.db.environment.POSTGRES_USER' "$CFG_CURRENT")
RESTORE_DB_USER=$(jq -re '.services.db.environment.POSTGRES_USER' "$CFG_RESTORE")
[ "$CURRENT_DB_USER" = "$RESTORE_DB_USER" ] || {
echo "Error: database user mismatch"; exit 1
}
# Verify PostgreSQL major version
DB_CONTAINER=$("${DC[@]}" ps -q db)
STORED_PG=$(jq -re '.pg_version' "$BACKUP_DIR/backup-metadata.json")
CURRENT_PG=$(docker exec "$DB_CONTAINER" psql -X -U "$CURRENT_DB_USER" \
-d "$(jq -re '.services.db.environment.POSTGRES_DB' "$CFG_CURRENT")" \
-tAc "SHOW server_version;")
[ "${STORED_PG%%.*}" = "${CURRENT_PG%%.*}" ] || {
echo "Error: PostgreSQL major version mismatch (backup: $STORED_PG, target: $CURRENT_PG)"; exit 1
}
# TCP credential verification
RESTORE_DB_PASSWORD=$(jq -re '.services.db.environment.POSTGRES_PASSWORD' "$CFG_RESTORE")
printf 'PGPASSWORD=%s\nPGCONNECT_TIMEOUT=10\n' "$RESTORE_DB_PASSWORD" > "$CRED_TMP"
docker exec --env-file "$CRED_TMP" "$DB_CONTAINER" \
psql -h db -p 5432 -U "$RESTORE_DB_USER" -d "$(jq -re '.services.db.environment.POSTGRES_DB' "$CFG_RESTORE")" \
-tAc "SELECT 1;" > /dev/null || {
echo "Error: TCP authentication failed with restored credentials"; exit 1
}
echo "Preflight passed"
# ── Destructive operations ──
"${DC[@]}" stop nyravio
# Save current .env
if [ -f .env ]; then
ENV_SAVE=$(mktemp "./.env.pre-restore-XXXXXXXXXX")
cp .env "$ENV_SAVE"
echo "Existing .env saved as $ENV_SAVE"
fi
cp "$BACKUP_DIR/env.backup" .env
chmod 600 .env
# Restore database (atomic)
docker exec -i "$DB_CONTAINER" psql -X --single-transaction -v ON_ERROR_STOP=1 \
-U "$RESTORE_DB_USER" -d "$(jq -re '.services.db.environment.POSTGRES_DB' "$CFG_RESTORE")" \
-c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;" \
-f - < "$BACKUP_DIR/backup.sql"
# Recreate application container
"${DC[@]}" create --no-build --pull never nyravio
# Restore uploads
NEW_CONTAINER=$("${DC[@]}" ps -q -a nyravio)
"${DC[@]}" run --rm --pull never --no-deps --user root --entrypoint sh nyravio -c \
"find /app/data/uploads -mindepth 1 -maxdepth 1 -exec rm -rf {} +"
docker cp - "$NEW_CONTAINER":/app/data/uploads/ < "$BACKUP_DIR/uploads.tar"
"${DC[@]}" run --rm --pull never --no-deps --user root --entrypoint sh nyravio -c \
"chown -R nyravio:nyravio /app/data/uploads"
# Start and verify
"${DC[@]}" start nyravio
PORT_MAPPING=$("${DC[@]}" port nyravio 8080)
HOST_PORT="${PORT_MAPPING##*:}"
echo "Waiting for readiness on port $HOST_PORT..."
SECONDS=0
TIMEOUT=120
while [ "$SECONDS" -lt "$TIMEOUT" ]; do
if curl -sf --connect-timeout 5 --max-time 10 \
"http://localhost:${HOST_PORT}/health/ready" > /dev/null 2>&1; then
echo "Restore complete — application healthy (${SECONDS}s)"
exit 0
fi
sleep 2
done
echo "Error: not ready within ${TIMEOUT}s"
exit 1

The restore procedure performs these verifications before any changes:

  1. Required tools available
  2. All backup artifacts present
  3. Database dump checksum matches
  4. Uploads archive checksum matches
  5. Environment backup checksum matches
  6. Application image exists locally with matching ID
  7. Compose project identity matches
  8. Database configuration (user, name, passwords) matches
  9. Volume identities match
  10. PostgreSQL major version matches
  11. TCP credential verification succeeds
  • Database rollback is not automatic. Flyway migrations are forward-only.
  • Image identity is verified. The image ID in metadata must match locally. Rebuilding from source does not guarantee the same ID.
  • Encrypted credentials require the original NYRAVIO_ADMIN_ENCRYPTION_KEY.
  • MCP master keys (NYRAVIO_MCP_MASTER_KEYS) must also be preserved if MCP credentials are stored.
  • Uploads restore is not atomic. Re-run on failure before starting the application.
  • Compatible deployment required. Cross-deployment restores are not supported.
  • Backup — create backups
  • Updates — rollback after a failed upgrade