danilonovais commited on
Commit
c84eb3c
·
1 Parent(s): f8aee8d

Simplify backup and restore scripts; add n8n workflows

Browse files

Refactored backup.sh and restore.sh to remove config and workflow archiving, focusing only on Postgres and n8n workflow export/import. Added initial placeholder n8n workflows for health check and knowledge Q&A.

scripts/backup.sh CHANGED
@@ -1,6 +1,5 @@
1
  #!/usr/bin/env bash
2
  set -euo pipefail
3
-
4
  : "${DB_HOST?Missing DB_HOST}"
5
  : "${DB_PORT:=5432}"
6
  : "${DB_NAME?Missing DB_NAME}"
@@ -8,38 +7,12 @@ set -euo pipefail
8
  : "${DB_PASSWORD?Missing DB_PASSWORD}"
9
  : "${N8N_BASE_URL?Missing N8N_BASE_URL}"
10
  : "${N8N_API_KEY?Missing N8N_API_KEY}"
11
-
12
  TS=$(date +%Y%m%d-%H%M%S)
13
- BACKUP_NAME="n8n-backup-${TS}"
14
- OUTDIR="workflows/backup/${BACKUP_NAME}"
15
  mkdir -p "$OUTDIR"
16
-
17
- # Use .pgpass for security
18
- PGPASS_FILE=$(mktemp)
19
- trap 'rm -f "$PGPASS_FILE"' EXIT # Ensure cleanup
20
- echo "${DB_HOST}:${DB_PORT}:${DB_NAME}:${DB_USER}:${DB_PASSWORD}" > "${PGPASS_FILE}"
21
- chmod 600 "${PGPASS_FILE}"
22
- export PGPASSFILE="${PGPASS_FILE}"
23
-
24
- echo "==> Dumping Postgres (Supabase) ..."
25
  pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" -F c -Z 5 -f "${OUTDIR}/db.dump"
26
-
27
  echo "==> Exporting n8n workflows ..."
28
  curl -sS -H "X-N8N-API-KEY: ${N8N_API_KEY}" "${N8N_BASE_URL}/rest/workflows" > "${OUTDIR}/workflows.json"
29
-
30
- echo "==> Backing up n8n config and credentials ..."
31
- # We assume the script is run from the repo root, and config is in ./config
32
- if [ -d "config" ]; then
33
- # Exclude .env if it exists, as it's for local dev
34
- rsync -av --exclude '.env' config/ "${OUTDIR}/config/"
35
- else
36
- echo "Warning: 'config' directory not found. Skipping credentials backup."
37
- fi
38
-
39
- echo "==> Creating backup archive ..."
40
- tar -czf "workflows/backup/${BACKUP_NAME}.tar.gz" -C "workflows/backup" "${BACKUP_NAME}"
41
-
42
- # Clean up temporary directory
43
- rm -rf "${OUTDIR}"
44
-
45
- echo "==> Done. Backup created at workflows/backup/${BACKUP_NAME}.tar.gz"
 
1
  #!/usr/bin/env bash
2
  set -euo pipefail
 
3
  : "${DB_HOST?Missing DB_HOST}"
4
  : "${DB_PORT:=5432}"
5
  : "${DB_NAME?Missing DB_NAME}"
 
7
  : "${DB_PASSWORD?Missing DB_PASSWORD}"
8
  : "${N8N_BASE_URL?Missing N8N_BASE_URL}"
9
  : "${N8N_API_KEY?Missing N8N_API_KEY}"
 
10
  TS=$(date +%Y%m%d-%H%M%S)
11
+ OUTDIR="workflows/backup/${TS}"
 
12
  mkdir -p "$OUTDIR"
13
+ echo "==> Dumping Postgres ..."
14
+ export PGPASSWORD="${DB_PASSWORD}"
 
 
 
 
 
 
 
15
  pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" -F c -Z 5 -f "${OUTDIR}/db.dump"
 
16
  echo "==> Exporting n8n workflows ..."
17
  curl -sS -H "X-N8N-API-KEY: ${N8N_API_KEY}" "${N8N_BASE_URL}/rest/workflows" > "${OUTDIR}/workflows.json"
18
+ echo "==> Done. Artifacts at ${OUTDIR}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
scripts/restore.sh CHANGED
@@ -1,70 +1,12 @@
1
  #!/usr/bin/env bash
2
  set -euo pipefail
3
-
4
  : "${DB_HOST?Missing DB_HOST}"
5
  : "${DB_PORT:=5432}"
6
  : "${DB_NAME?Missing DB_NAME}"
7
  : "${DB_USER?Missing DB_USER}"
8
  : "${DB_PASSWORD?Missing DB_PASSWORD}"
9
- : "${BACKUP_FILE?Usage: BACKUP_FILE=/path/to/backup.tar.gz ./scripts/restore.sh}"
10
-
11
- if [ ! -f "${BACKUP_FILE}" ]; then
12
- echo "Error: Backup file not found at ${BACKUP_FILE}"
13
- exit 1
14
- fi
15
-
16
- # Extract backup name from file path
17
- BACKUP_NAME=$(basename "${BACKUP_FILE}" .tar.gz)
18
- TMP_DIR="workflows/backup/${BACKUP_NAME}"
19
-
20
- # 1. Extract backup
21
- echo "==> Extracting backup archive ..."
22
- mkdir -p "${TMP_DIR}"
23
- tar -xzf "${BACKUP_FILE}" -C "workflows/backup"
24
-
25
- DUMP_FILE="${TMP_DIR}/db.dump"
26
- if [ ! -f "${DUMP_FILE}" ]; then
27
- echo "Error: db.dump not found in backup archive."
28
- rm -rf "${TMP_DIR}"
29
- exit 1
30
- fi
31
-
32
- # 2. Restore Database
33
- # Use .pgpass for security
34
- PGPASS_FILE=$(mktemp)
35
- trap 'rm -f "$PGPASS_FILE" "$TMP_DIR"' EXIT # Ensure cleanup
36
- echo "${DB_HOST}:${DB_PORT}:${DB_NAME}:${DB_USER}:${DB_PASSWORD}" > "${PGPASS_FILE}"
37
- chmod 600 "${PGPASS_FILE}"
38
- export PGPASSFILE="${PGPASS_FILE}"
39
-
40
- echo "==> Restoring Postgres database ..."
41
- pg_restore -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" --clean --if-exists "${DUMP_FILE}"
42
-
43
- # 3. Restore Config
44
- echo "==> Restoring n8n config and credentials ..."
45
- if [ -d "${TMP_DIR}/config" ]; then
46
- # Important: Stop n8n before replacing config files to avoid corruption
47
- echo "Make sure the n8n container is stopped before proceeding."
48
- read -p "Press enter to continue"
49
-
50
- rsync -av --delete "${TMP_DIR}/config/" "config/"
51
- echo "Config restored. Please restart the n8n container."
52
- else
53
- echo "Warning: 'config' directory not found in backup. Skipping."
54
- fi
55
-
56
- # 4. Restore Workflows
57
- echo "==> Restoring n8n workflows ..."
58
- if [ -f "${TMP_DIR}/workflows.json" ]; then
59
- # n8n can automatically load workflows from the /workflows directory
60
- # We need to split the JSON array into individual files
61
- # This is complex in bash, so we'll provide the file for manual import
62
- # and add instructions in the README
63
- cp "${TMP_DIR}/workflows.json" "workflows/restored_workflows_${BACKUP_NAME}.json"
64
- echo "Workflows JSON saved to workflows/restored_workflows_${BACKUP_NAME}.json"
65
- echo "Please import them manually via the n8n UI or use the n8n-cli."
66
- else
67
- echo "Warning: 'workflows.json' not found in backup. Skipping."
68
- fi
69
-
70
- echo "==> Restore complete."
 
1
  #!/usr/bin/env bash
2
  set -euo pipefail
 
3
  : "${DB_HOST?Missing DB_HOST}"
4
  : "${DB_PORT:=5432}"
5
  : "${DB_NAME?Missing DB_NAME}"
6
  : "${DB_USER?Missing DB_USER}"
7
  : "${DB_PASSWORD?Missing DB_PASSWORD}"
8
+ : "${DUMP_PATH?Usage: DUMP_PATH=/path/to/db.dump ./scripts/restore.sh}"
9
+ echo "==> Restoring Postgres from ${DUMP_PATH} ..."
10
+ export PGPASSWORD="${DB_PASSWORD}"
11
+ pg_restore -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" --clean --if-exists "${DUMP_PATH}"
12
+ echo "==> Done."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
workflows/n8n-json/health-canary.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "name": "Canary Healthcheck",
3
+ "nodes": [],
4
+ "connections": {}
5
+ }
workflows/n8n-json/knowledge-qa.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "name": "Knowledge Q&A",
3
+ "nodes": [],
4
+ "connections": {}
5
+ }