Skip to content

PostgreSQL Backup and Upload to Supabase Storage #274

PostgreSQL Backup and Upload to Supabase Storage

PostgreSQL Backup and Upload to Supabase Storage #274

Workflow file for this run

name: PostgreSQL Backup and Upload to Supabase Storage
on:
push:
branches:
- main
schedule:
- cron: "0 0 * * *" # Cron job that runs every day at midnight (UTC)
jobs:
backup:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up PostgreSQL Dump
uses: tj-actions/pg-dump@v3.0.1
with:
postgresql_version: "16" # Use PostgreSQL version 16
database_url: ${{ secrets.DATABASE_URL }} # Set this secret in GitHub
path: "./backups/backup.sql"
options: "-O"
- name: Generate Timestamp for Backup Filename
id: timestamp
run: echo "timestamp=$(date +'%Y-%m-%d_%H-%M-%S')" >> $GITHUB_ENV
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "18" # Specify Node.js version
- name: Install dependencies
run: npm install --legacy-peer-deps
- name: Install Supabase Client
run: npm install @supabase/supabase-js --legacy-peer-deps
- name: Upload Backup to Supabase Storage
run: |
BACKUP_FILE="./backups/backup.sql"
node -e "
const { createClient } = require('@supabase/supabase-js');
const fs = require('fs');
const path = require('path');
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseServiceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
const supabase = createClient(supabaseUrl, supabaseServiceRoleKey);
const filePath = path.resolve('$BACKUP_FILE');
const file = fs.readFileSync(filePath);
const fileName = 'backup-${{ env.timestamp }}.sql';
supabase
.storage
.from('backups')
.upload(fileName, file, { contentType: 'application/sql', upsert: true })
.then(({ error }) => {
if (error) throw error;
console.log('File uploaded successfully!');
})
.catch(err => {
console.error('Error uploading file:', err);
process.exit(1);
});
"
env:
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
SUPABASE_SERVICE_ROLE_KEY: ${{ secrets.SUPABASE_SERVICE_ROLE_KEY }}