A comprehensive collection of bash scripts for managing Supabase projects across development, staging, and production environments. This toolkit provides essential tools for database management, edge function deployment, and project migration workflows.
Complete database backup, restore, and migration management with smart role bootstrap functionality.
Download and manage Supabase Edge Functions from remote projects with automatic file organization, path fixing, and secret template generation.
# Clone production database to local development
./supabase_backup.sh clone-local --db-url <REMOTE_DB_URL>
# Backup a database
./supabase_backup.sh backup --db-url <DB_URL>
# Restore to local database
./supabase_backup.sh restore --local# Download all edge functions from a project
./pull_edge_functions.sh --project-ref <PROJECT_REF>
# Download specific functions
./pull_edge_functions.sh --project-ref <PROJECT_REF> --names auth,webhook
# Download with secret templates
./pull_edge_functions.sh --project-ref <PROJECT_REF> --export-secrets
# Fix existing function organization
./pull_edge_functions.sh --fix-existing- Supabase CLI: Installation Guide
- PostgreSQL Client (psql): For direct database operations
- libpq: PostgreSQL client library for database connectivity
- Perl: Included by default on macOS, required for advanced text processing
- Bash: Unix-like shell environment
- jq (optional): For enhanced JSON parsing in edge function tool
supabase/
βββ backups/ # Database backup files (auto-created)
β βββ roles.sql # Database roles
β βββ schema.sql # Database schema
β βββ data.sql # Database data
βββ migrations/ # Generated migrations (auto-created)
β βββ 00000000000000_roles_bootstrap.sql # Auto-generated role bootstrap
βββ functions/ # Edge functions (auto-created)
β βββ _shared/ # Shared utilities and types
β βββ _graphql/ # GraphQL schema and resolvers
β βββ function1/
β β βββ index.ts
β β βββ .env.example
β βββ function2/
βββ seed.sql # Generated seed file (auto-created)
The supabase_backup.sh script provides comprehensive database operations:
- Complete Database Operations: Backup, restore, baseline, and seed generation
- Smart Role Management: Automatic detection and bootstrap of custom database roles
- Local & Cloud Support: Works with both local development and production databases
- macOS Safe: Optimized for macOS environments with proper dependency handling
- Idempotent Operations: Safe to run multiple times without conflicts
π Full Database Tool Documentation
The pull_edge_functions.sh script manages Supabase Edge Functions:
- Function Download: Pull edge functions from remote Supabase projects
- Secret Management: Generate
.env.exampletemplates from project secrets - Flexible Output: Specify custom output directories
- Selective Download: Download specific functions by name
- Overwrite Protection: Safe overwrite options with confirmation
π Full Edge Function Tool Documentation
- Role Bootstrap: Automatic detection and creation of custom roles
- Local-Safe Mode: Filters managed schemas for local development
- Idempotent Operations: Safe repeated execution
- Transaction Safety: All operations use proper transaction handling
- Robust Parsing: Handles both JSON and table output formats
- Secret Templates: Automatically generates
.env.examplefiles - Function Discovery: Lists all available functions in a project
- Flexible Deployment: Supports custom output directories
- Path Organization: Automatically fixes absolute paths and organizes files into
_sharedand_graphqldirectories - Import Fixing: Updates TypeScript import statements to use relative paths
- Existing Function Fixes: Use
--fix-existingto reorganize already downloaded functions
# 1. Clone production database to local
./supabase_backup.sh clone-local --db-url $PROD_DB_URL --yes
# 2. Download edge functions with automatic organization
./pull_edge_functions.sh --project-ref $PROJECT_REF --export-secrets
# 3. Start local development
supabase start# 1. Backup current production database
./supabase_backup.sh backup --db-url $PROD_DB_URL
# 2. Deploy edge functions (with organized structure)
supabase functions deploy --project-ref $PROJECT_REF
# 3. Apply database migrations
supabase db push --project-ref $PROJECT_REF# 1. Restore production backup to staging
./supabase_backup.sh restore --target-db-url $STAGING_DB_URL
# 2. Deploy edge functions to staging
supabase functions deploy --project-ref $STAGING_PROJECT_REF-
Missing Dependencies
# Install Supabase CLI npm install -g supabase # Install PostgreSQL client and libpq (macOS) brew install postgresql # Install jq for enhanced JSON parsing (optional) brew install jq
-
Permission Issues
# Make scripts executable chmod +x supabase_backup.sh pull_edge_functions.sh -
Database Connection Issues
- Verify database URL format:
postgresql://user:pass@host:port/db - Check network connectivity
- Ensure database is accessible
- Verify database URL format:
For troubleshooting, you can run scripts with bash debugging:
# Database tool debugging
bash -x ./supabase_backup.sh <command> [options]
# Edge function tool debugging
bash -x ./pull_edge_functions.sh --project-ref <REF> --debug- Database URLs: Never commit database URLs with credentials to version control
- Seed Files: Add
supabase/seed.sqlto.gitignoreif it contains sensitive data - Backup Files: Consider encrypting backup files for sensitive data
- Secret Templates:
.env.examplefiles contain only keys, not values - Role Permissions: Review custom roles and their permissions before bootstrap
The edge function tool automatically organizes downloaded functions into a clean structure:
supabase/functions/
βββ _shared/ # Shared utilities, types, and common code
β βββ types.ts # Common TypeScript types
β βββ utils.ts # Shared utility functions
β βββ constants.ts # Shared constants
βββ _graphql/ # GraphQL schema and resolvers
β βββ schema.ts # GraphQL schema definitions
β βββ resolvers.ts # GraphQL resolvers
βββ auth/ # Authentication functions
β βββ index.ts
β βββ .env.example
βββ webhook/ # Webhook handlers
β βββ index.ts
β βββ .env.example
βββ api/ # API endpoints
βββ index.ts
βββ .env.example
The tool automatically:
- Fixes Absolute Paths: Converts
file:/absolute/pathreferences to relative paths - Organizes Shared Code: Moves common utilities to
_shareddirectory - Groups GraphQL Code: Organizes GraphQL-related files in
_graphqldirectory - Updates Imports: Fixes TypeScript import statements to use relative paths
- Maintains Structure: Preserves function-specific code in individual directories
If you have existing functions with path issues:
# Fix organization in existing functions
./pull_edge_functions.sh --fix-existing
# Fix with custom output directory
./pull_edge_functions.sh --fix-existing --outdir ./my-functionsThis toolkit is designed to be portable and maintainable. When contributing:
- Test on both macOS and Linux environments
- Ensure all operations remain idempotent
- Add appropriate error handling
- Update documentation for new features
- Follow the existing code style and patterns
This project is open source and available under the MIT License.
Note: These tools are designed for development and staging environments. For production backups, consider using Supabase's built-in backup features or database-specific backup solutions.