Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 71 additions & 12 deletions restore.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/bash

################################################################################
# Supabase Disaster Recovery - Restore Script
# Supabase Disaster Recovery - Restore Script (v2.0)
################################################################################
# This script restores a complete Supabase database backup including:
# - Extensions
# - Schema (tables, indexes, constraints, relations)
# - Full data
# - Full data (with limitations - see below)
# - SQL Functions
# - Triggers
# - RLS Policies
Expand All @@ -16,6 +16,21 @@
# ./restore.sh
#
# IMPORTANT: This should be run on a NEW/EMPTY Supabase project
#
# KNOWN LIMITATIONS:
# - auth.users data cannot be restored (Supabase managed schema)
# - Tables with FK to auth.users may have partial data restoration
# - Users will need to re-register or be imported via Supabase Admin API
# - Some permission errors are expected and filtered out
#
# WHAT GETS RESTORED:
# ✅ Complete database schema (all tables, indexes, constraints)
# ✅ All RLS policies
# ✅ All custom functions and triggers
# ✅ Data in tables without auth.users dependencies
# ⚠️ Partial data in user-dependent tables
#
# For more information, see backup_with_auth_export.sh
################################################################################

set -e # Exit on error
Expand Down Expand Up @@ -193,19 +208,63 @@ restore_data() {
exit 1
fi

# Use pg_restore with data-only mode to avoid conflicts with already-created schema
pg_restore "$SUPABASE_DB_URL" \
log_info "Temporarily disabling foreign key constraints..."

# Disable all foreign key constraints temporarily
psql "$SUPABASE_DB_URL" > /dev/null 2>&1 <<EOF
DO \$\$
DECLARE
r RECORD;
BEGIN
-- Disable all triggers (including FK triggers) on public schema tables
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
BEGIN
EXECUTE 'ALTER TABLE public.' || quote_ident(r.tablename) || ' DISABLE TRIGGER ALL';
EXCEPTION WHEN OTHERS THEN
-- Ignore errors for tables we can't modify
NULL;
END;
END LOOP;
END \$\$;
EOF

log_info "Restoring data..."

# Use pg_restore with data-only mode
# Suppress errors but capture exit code
pg_restore -d "$SUPABASE_DB_URL" \
--data-only \
--no-owner \
--no-privileges \
--disable-triggers \
"$BACKUP_DIR/complete_backup.dump" 2>&1 | grep -v "WARNING" || true

if [ ${PIPESTATUS[0]} -eq 0 ]; then
log_info "✓ Data restored successfully"
else
log_warn "Data restore completed with some warnings (this is often normal)"
fi
--schema=public \
"$BACKUP_DIR/complete_backup.dump" 2>&1 | \
grep -v "WARNING" | \
grep -v "permission denied.*system trigger" | \
grep -v "must be owner" | \
grep -v "permission denied for table" | \
grep -v "permission denied for sequence" || true

log_info "Re-enabling foreign key constraints..."

# Re-enable all triggers
psql "$SUPABASE_DB_URL" > /dev/null 2>&1 <<EOF
DO \$\$
DECLARE
r RECORD;
BEGIN
-- Re-enable all triggers on public schema tables
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
BEGIN
EXECUTE 'ALTER TABLE public.' || quote_ident(r.tablename) || ' ENABLE TRIGGER ALL';
EXCEPTION WHEN OTHERS THEN
-- Ignore errors for tables we can't modify
NULL;
END;
END LOOP;
END \$\$;
EOF

log_info "✓ Data restore completed"
}

restore_functions() {
Expand Down
2 changes: 1 addition & 1 deletion supabase_snapshot/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4199,5 +4199,5 @@ CREATE EVENT TRIGGER pgrst_drop_watch ON sql_drop
-- PostgreSQL database dump complete
--

\unrestrict OVoBGub51LjFy6kEY7dRtaMWfQ2BIRckmmYLXufmKBCuNpLz3XLKfT2ZMgLMnQe
\unrestrict NW9brVcVm5JHg1chomVHbWU0SVK0H6qV5acMD85ELkMWTybxSfA6OX34ZPbdCHb

4 changes: 2 additions & 2 deletions supabase_snapshot/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- PostgreSQL database dump
--

\restrict wHHu30W2rUIDAs5qZNz98lcGqa5W0GOwTdb5mYhijwndmJlyoh65CKjVXRgNPx8
\restrict DmgijBsGU6JA0th1NLoPa3DnHdMAaPY17WtkePAMxEaxxk4aSgrm23Vknzi2eUQ

-- Dumped from database version 17.4
-- Dumped by pg_dump version 17.7 (Homebrew)
Expand Down Expand Up @@ -11151,5 +11151,5 @@ CREATE EVENT TRIGGER pgrst_drop_watch ON sql_drop
-- PostgreSQL database dump complete
--

\unrestrict wHHu30W2rUIDAs5qZNz98lcGqa5W0GOwTdb5mYhijwndmJlyoh65CKjVXRgNPx8
\unrestrict DmgijBsGU6JA0th1NLoPa3DnHdMAaPY17WtkePAMxEaxxk4aSgrm23Vknzi2eUQ

Loading