Spaces:
Sleeping
Sleeping
Delete restore.sh
Browse files- restore.sh +0 -91
restore.sh
DELETED
|
@@ -1,91 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env bash
|
| 2 |
-
###############################################################################
|
| 3 |
-
# restore.sh
|
| 4 |
-
#
|
| 5 |
-
# Restores a backup bundle created by backup.sh.
|
| 6 |
-
#
|
| 7 |
-
# Usage:
|
| 8 |
-
# ./restore.sh /data/backups/n8n-backup-20260115-030000.tar.gz
|
| 9 |
-
#
|
| 10 |
-
# WARNING: Restoring the database DROPS conflicting objects in the target
|
| 11 |
-
# Supabase database (via pg_restore --clean --if-exists). Double-check
|
| 12 |
-
# DB_POSTGRESDB_* / SUPABASE_DB_URL point at the database you intend to
|
| 13 |
-
# overwrite before running this.
|
| 14 |
-
###############################################################################
|
| 15 |
-
set -euo pipefail
|
| 16 |
-
|
| 17 |
-
log() {
|
| 18 |
-
echo "[restore] $(date -u +'%Y-%m-%dT%H:%M:%SZ') $*"
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
ARCHIVE="${1:-}"
|
| 22 |
-
if [ -z "$ARCHIVE" ]; then
|
| 23 |
-
echo "Usage: $0 <path-to-backup-archive.tar.gz>" >&2
|
| 24 |
-
exit 1
|
| 25 |
-
fi
|
| 26 |
-
if [ ! -f "$ARCHIVE" ]; then
|
| 27 |
-
echo "ERROR: archive not found: $ARCHIVE" >&2
|
| 28 |
-
exit 1
|
| 29 |
-
fi
|
| 30 |
-
|
| 31 |
-
WORKDIR="$(mktemp -d)"
|
| 32 |
-
trap 'rm -rf "$WORKDIR"' EXIT
|
| 33 |
-
|
| 34 |
-
log "Extracting $ARCHIVE ..."
|
| 35 |
-
tar -xzf "$ARCHIVE" -C "$WORKDIR"
|
| 36 |
-
|
| 37 |
-
BUNDLE_DIR="$(find "$WORKDIR" -mindepth 1 -maxdepth 1 -type d | head -n1)"
|
| 38 |
-
if [ -z "$BUNDLE_DIR" ] || [ ! -f "${BUNDLE_DIR}/manifest.json" ]; then
|
| 39 |
-
echo "ERROR: archive does not look like a valid backup bundle (manifest.json missing)." >&2
|
| 40 |
-
exit 1
|
| 41 |
-
fi
|
| 42 |
-
log "Bundle contents:"
|
| 43 |
-
cat "${BUNDLE_DIR}/manifest.json"
|
| 44 |
-
|
| 45 |
-
read -r -p "This will OVERWRITE the target database and existing workflows/credentials. Continue? [y/N] " confirm
|
| 46 |
-
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
|
| 47 |
-
log "Aborted by user."
|
| 48 |
-
exit 0
|
| 49 |
-
fi
|
| 50 |
-
|
| 51 |
-
# --- 1. Database restore --------------------------------------------------------
|
| 52 |
-
if [ -n "${SUPABASE_DB_URL:-}" ]; then
|
| 53 |
-
DB_URL="$SUPABASE_DB_URL"
|
| 54 |
-
elif [ -n "${DB_POSTGRESDB_HOST:-}" ]; then
|
| 55 |
-
DB_URL="postgresql://${DB_POSTGRESDB_USER}:${DB_POSTGRESDB_PASSWORD}@${DB_POSTGRESDB_HOST}:${DB_POSTGRESDB_PORT:-5432}/${DB_POSTGRESDB_DATABASE}?sslmode=require"
|
| 56 |
-
else
|
| 57 |
-
echo "ERROR: Neither SUPABASE_DB_URL nor DB_POSTGRESDB_* variables are set." >&2
|
| 58 |
-
exit 1
|
| 59 |
-
fi
|
| 60 |
-
|
| 61 |
-
if [ -f "${BUNDLE_DIR}/database.dump" ]; then
|
| 62 |
-
log "Restoring database from ${BUNDLE_DIR}/database.dump ..."
|
| 63 |
-
pg_restore --clean --if-exists --no-owner --no-privileges \
|
| 64 |
-
--dbname="$DB_URL" "${BUNDLE_DIR}/database.dump"
|
| 65 |
-
log "Database restore complete."
|
| 66 |
-
else
|
| 67 |
-
log "No database.dump found in bundle; skipping database restore."
|
| 68 |
-
fi
|
| 69 |
-
|
| 70 |
-
# --- 2. Stop n8n before importing workflows/credentials, if running -------------
|
| 71 |
-
log "NOTE: n8n should be stopped (or you should expect to restart it) before workflow/credential import takes effect cleanly."
|
| 72 |
-
|
| 73 |
-
# --- 3. Workflow import -----------------------------------------------------------
|
| 74 |
-
if [ -d "${BUNDLE_DIR}/workflows" ] && command -v n8n >/dev/null 2>&1; then
|
| 75 |
-
log "Importing workflows..."
|
| 76 |
-
n8n import:workflow --separate --input="${BUNDLE_DIR}/workflows/" || \
|
| 77 |
-
log "WARNING: workflow import reported an error."
|
| 78 |
-
else
|
| 79 |
-
log "Skipping workflow import (no workflows dir or n8n CLI unavailable)."
|
| 80 |
-
fi
|
| 81 |
-
|
| 82 |
-
# --- 4. Credential import (requires the SAME N8N_ENCRYPTION_KEY used at backup time) -
|
| 83 |
-
if [ -d "${BUNDLE_DIR}/credentials" ] && command -v n8n >/dev/null 2>&1; then
|
| 84 |
-
log "Importing credentials (requires matching N8N_ENCRYPTION_KEY)..."
|
| 85 |
-
n8n import:credentials --separate --input="${BUNDLE_DIR}/credentials/" || \
|
| 86 |
-
log "WARNING: credential import reported an error. This usually means N8N_ENCRYPTION_KEY does not match the key used when the backup was created."
|
| 87 |
-
else
|
| 88 |
-
log "Skipping credential import (no credentials dir or n8n CLI unavailable)."
|
| 89 |
-
fi
|
| 90 |
-
|
| 91 |
-
log "Restore complete. Restart the n8n process/container to pick up imported data."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|