Spaces:
Running
Running
Delete backup.sh
Browse files
backup.sh
DELETED
|
@@ -1,95 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env bash
|
| 2 |
-
###############################################################################
|
| 3 |
-
# backup.sh
|
| 4 |
-
#
|
| 5 |
-
# Creates a timestamped backup bundle containing:
|
| 6 |
-
# 1. A full Supabase Postgres dump (pg_dump, custom format).
|
| 7 |
-
# 2. An export of every n8n workflow (n8n export:workflow --all).
|
| 8 |
-
# 3. An export of every n8n credential, STILL ENCRYPTED with
|
| 9 |
-
# N8N_ENCRYPTION_KEY (n8n export:credentials --all).
|
| 10 |
-
#
|
| 11 |
-
# Run manually: ./backup.sh
|
| 12 |
-
# Run inside the container (e.g. via `docker exec`):
|
| 13 |
-
# docker exec <container> /app/backup.sh
|
| 14 |
-
# Or schedule it (see docs/BACKUP.md for a cron / HF Space "scheduled job"
|
| 15 |
-
# example, since HF Spaces itself has no built-in cron).
|
| 16 |
-
#
|
| 17 |
-
# IMPORTANT: The credential export is only as safe as N8N_ENCRYPTION_KEY.
|
| 18 |
-
# Store backups somewhere access-controlled, and store the encryption key
|
| 19 |
-
# separately from the backups themselves.
|
| 20 |
-
###############################################################################
|
| 21 |
-
set -euo pipefail
|
| 22 |
-
|
| 23 |
-
BACKUP_DIR="${BACKUP_DIR:-/data/backups}"
|
| 24 |
-
RETENTION_DAYS="${BACKUP_RETENTION_DAYS:-14}"
|
| 25 |
-
TIMESTAMP="$(date -u +'%Y%m%d-%H%M%S')"
|
| 26 |
-
BUNDLE_DIR="${BACKUP_DIR}/${TIMESTAMP}"
|
| 27 |
-
|
| 28 |
-
log() {
|
| 29 |
-
echo "[backup] $(date -u +'%Y-%m-%dT%H:%M:%SZ') $*"
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
mkdir -p "$BUNDLE_DIR"
|
| 33 |
-
log "Writing backup bundle to $BUNDLE_DIR"
|
| 34 |
-
|
| 35 |
-
# --- 1. Database dump ---------------------------------------------------------
|
| 36 |
-
if [ -n "${SUPABASE_DB_URL:-}" ]; then
|
| 37 |
-
DB_URL="$SUPABASE_DB_URL"
|
| 38 |
-
elif [ -n "${DB_POSTGRESDB_HOST:-}" ]; then
|
| 39 |
-
DB_URL="postgresql://${DB_POSTGRESDB_USER}:${DB_POSTGRESDB_PASSWORD}@${DB_POSTGRESDB_HOST}:${DB_POSTGRESDB_PORT:-5432}/${DB_POSTGRESDB_DATABASE}?sslmode=require"
|
| 40 |
-
else
|
| 41 |
-
log "ERROR: Neither SUPABASE_DB_URL nor DB_POSTGRESDB_* variables are set. Cannot back up database."
|
| 42 |
-
exit 1
|
| 43 |
-
fi
|
| 44 |
-
|
| 45 |
-
log "Dumping Supabase Postgres database (pg_dump, custom format)..."
|
| 46 |
-
if pg_dump --format=custom --no-owner --no-privileges --dbname="$DB_URL" \
|
| 47 |
-
--file="${BUNDLE_DIR}/database.dump"; then
|
| 48 |
-
log "Database dump complete: ${BUNDLE_DIR}/database.dump"
|
| 49 |
-
else
|
| 50 |
-
log "ERROR: pg_dump failed."
|
| 51 |
-
exit 1
|
| 52 |
-
fi
|
| 53 |
-
|
| 54 |
-
# --- 2. Workflow export --------------------------------------------------------
|
| 55 |
-
log "Exporting n8n workflows..."
|
| 56 |
-
mkdir -p "${BUNDLE_DIR}/workflows"
|
| 57 |
-
if command -v n8n >/dev/null 2>&1; then
|
| 58 |
-
n8n export:workflow --all --output="${BUNDLE_DIR}/workflows/" --separate || \
|
| 59 |
-
log "WARNING: workflow export reported an error (n8n may not be running)."
|
| 60 |
-
else
|
| 61 |
-
log "WARNING: 'n8n' CLI not found in this shell; skipping workflow export. Run this script inside the container."
|
| 62 |
-
fi
|
| 63 |
-
|
| 64 |
-
# --- 3. Credential export (still encrypted) -------------------------------------
|
| 65 |
-
log "Exporting n8n credentials (remain encrypted with N8N_ENCRYPTION_KEY)..."
|
| 66 |
-
mkdir -p "${BUNDLE_DIR}/credentials"
|
| 67 |
-
if command -v n8n >/dev/null 2>&1; then
|
| 68 |
-
n8n export:credentials --all --output="${BUNDLE_DIR}/credentials/" --separate || \
|
| 69 |
-
log "WARNING: credential export reported an error (n8n may not be running)."
|
| 70 |
-
else
|
| 71 |
-
log "WARNING: 'n8n' CLI not found in this shell; skipping credential export."
|
| 72 |
-
fi
|
| 73 |
-
|
| 74 |
-
# --- 4. Manifest -----------------------------------------------------------------
|
| 75 |
-
cat > "${BUNDLE_DIR}/manifest.json" <<EOF
|
| 76 |
-
{
|
| 77 |
-
"timestamp_utc": "${TIMESTAMP}",
|
| 78 |
-
"database_dump": "database.dump",
|
| 79 |
-
"workflows_dir": "workflows/",
|
| 80 |
-
"credentials_dir": "credentials/",
|
| 81 |
-
"note": "Credentials are encrypted with N8N_ENCRYPTION_KEY. Keep the key separately."
|
| 82 |
-
}
|
| 83 |
-
EOF
|
| 84 |
-
|
| 85 |
-
# --- 5. Compress -------------------------------------------------------------------
|
| 86 |
-
log "Compressing bundle..."
|
| 87 |
-
tar -czf "${BACKUP_DIR}/n8n-backup-${TIMESTAMP}.tar.gz" -C "$BACKUP_DIR" "$TIMESTAMP"
|
| 88 |
-
rm -rf "$BUNDLE_DIR"
|
| 89 |
-
log "Backup archive created: ${BACKUP_DIR}/n8n-backup-${TIMESTAMP}.tar.gz"
|
| 90 |
-
|
| 91 |
-
# --- 6. Retention / pruning ---------------------------------------------------------
|
| 92 |
-
log "Pruning backups older than ${RETENTION_DAYS} days..."
|
| 93 |
-
find "$BACKUP_DIR" -maxdepth 1 -name 'n8n-backup-*.tar.gz' -mtime "+${RETENTION_DAYS}" -print -delete || true
|
| 94 |
-
|
| 95 |
-
log "Backup complete."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|