Skip to content

Instantly share code, notes, and snippets.

@oguzdelioglu
Created July 12, 2025 00:04
Show Gist options
  • Save oguzdelioglu/b3b17d1fc2e9119039710321c97c4572 to your computer and use it in GitHub Desktop.
Save oguzdelioglu/b3b17d1fc2e9119039710321c97c4572 to your computer and use it in GitHub Desktop.
Supabase Self Hosting Backup
#!/bin/bash
# Exit immediately if a command exits with a non-zero status, and print commands.
set -e
set -o pipefail
set -x
# ==============================================================================
# CORE VARIABLES
# ==============================================================================
# The directory where your Supabase project's docker-compose.yml is located
COMPOSE_DIR="/home/supabase/docker"
# The base directory where backups will be stored
BACKUP_BASE_DIR="/var/backups/supabase"
# Number of days to keep backups
RETENTION_DAYS=7
# ==============================================================================
# SCRIPT START
# ==============================================================================
echo "Starting full data backup for Supabase..."
# Create a unique, timestamped directory for each backup
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
BACKUP_DIR="${BACKUP_BASE_DIR}/${TIMESTAMP}"
mkdir -p "$BACKUP_DIR"
echo "Backups will be saved to: ${BACKUP_DIR}"
# Change to the docker-compose directory to run commands in the correct context
cd "${COMPOSE_DIR}"
# --- 1. Full PostgreSQL Cluster Backup (pg_dumpall) ---
# This command backs up all databases, roles, users, and schemas.
echo "[1/2] Performing a full PostgreSQL cluster backup (pg_dumpall)..."
CLUSTER_BACKUP_FILE="${BACKUP_DIR}/postgres_cluster_dump.sql.gz"
# Run pg_dumpall as the 'postgres' user inside the 'db' service.
# This user is a superuser and does not require a password.
docker compose exec -T db pg_dumpall -U postgres --clean --if-exists | gzip > "$CLUSTER_BACKUP_FILE"
echo "PostgreSQL cluster backup created successfully."
# --- 2. Supabase Storage Files Backup (Volume) ---
# This command backs up all user-uploaded files (images, videos, etc.).
echo "[2/2] Backing up Supabase Storage files (volume)..."
STORAGE_BACKUP_FILE="${BACKUP_DIR}/storage_volume_backup.tar.gz"
STORAGE_PATH_IN_CONTAINER="/var/lib/storage"
# Archive the /var/lib/storage directory inside the 'storage' service.
docker compose exec -T storage tar -cz -C "$STORAGE_PATH_IN_CONTAINER" . > "$STORAGE_BACKUP_FILE"
echo "Storage files backed up successfully."
# --- Prune Old Backups ---
echo -e "\nPruning old backups..."
find "$BACKUP_BASE_DIR" -type d -mtime +$RETENTION_DAYS -exec rm -rf {} \;
echo "Pruning complete."
# Disable debug mode
set +x
echo -e "\n\033[0;32mFull data backup completed successfully!\033[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment