DEV Community

Cover image for Backing up Firebase project with Github Actions
Dennis Alund for Oddbit

Posted on • Originally published at dennis.alund.me

Backing up Firebase project with Github Actions

Backups are really useless most of the time... until something goes wrong and you really need one.

This article will help provide some easy to apply recipes to back up your Firebase project, in a single Github Action workflow.

To implement this workflow you will be needing

  1. Firebase token for CI/CD deployment.
  2. Service account that can access your Firebase project and storage buckets.

We will be making a snapshot backup that is being replaced for each run and another accumulative one that is adding every new
backup into an historical archive.

Make sure that your GCP project has a "backups bucket". It should be listed in your Google Cloud Platform Console, but not in the Firebase console. Replace example in the URL below to check and confirm.

https://console.cloud.google.com/storage/browser?project=example

If your project ID is example, then you should have a bucket called example-backups.

The bucket is automatically created if you have already configured Firebase Database automated backups before. You can configure to switch them on right away to get two-for-one: both having the bucket generated and benefits of automated backups.

We will be using this bucket to store our own backups too.

/.github/workflows/backup-snapshot.yml

The schedule for your snapshot backups is up to you on how frequent you find it necessary. In this example we're making a nightly snapshot of the project.

name: Snapshot backup
env:
BACKUP_BUCKET: "gs://example-prod-backups"
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
on:
schedule:
# At some schedule when your app is least active
- cron: "0 3 * * *"
jobs:
backup-firestore:
runs-on: ubuntu-latest
steps:
- name: Setup gcloud environment
uses: GoogleCloudPlatform/github-actions@0.1.2
with:
project_id: example-prod
service_account_key: ${{ secrets.GCLOUD_AUTH_KEY_PROD }}
- name: Select project
run: gcloud config set project example-prod
- name: Remove previous nightly backup
run: gsutil rm -rf ${{ env.BACKUP_BUCKET }}/firestore/snapshot
- name: Export Firestore to Cloud Storage bucket
run: gcloud firestore export ${{ env.BACKUP_BUCKET }}/firestore/snapshot --async
backup-rtdb:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup gcloud environment
uses: GoogleCloudPlatform/github-actions@0.1.2
with:
project_id: example-prod
service_account_key: ${{ secrets.GCLOUD_AUTH_KEY_PROD }}
- name: Select project
run: gcloud config set project example-prod
- name: Select Firebase project
uses: w9jds/firebase-action@v1.3.0
with:
args: use production
- name: Download RTDB data
uses: w9jds/firebase-action@v1.3.0
with:
args: database:get --output snapshot.json /
- name: Remove previous nightly backup
run: gsutil rm -f ${{ env.BACKUP_BUCKET }}/rtdb/snapshot.json
- name: Upload to Cloud Storage Bucket
run: gsutil cp snapshot.json ${{ env.BACKUP_BUCKET }}/rtdb

/.github/workflows/backup-archive.yml

The schedule for your archive backups is also up to your own needs, depending on how frequent you find it necessary. In this example we're making a monthly backup archive of the project.

name: Backup archive
env:
BACKUP_BUCKET: "gs://example-prod-backups"
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
on:
schedule:
# First of every month at some time when your app is least active
- cron: "0 3 1 * *"
jobs:
backup-firestore:
runs-on: ubuntu-latest
steps:
- name: Setup gcloud environment
uses: GoogleCloudPlatform/github-actions@0.1.2
with:
project_id: example-prod
service_account_key: ${{ secrets.GCLOUD_AUTH_KEY_PROD }}
- name: Select project
run: gcloud config set project example-prod
- name: Export Firestore
run: gcloud firestore export ${{ env.BACKUP_BUCKET }}/firestore/$(date '+%Y-%m-%d') --async
backup-rtdb:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup gcloud environment
uses: GoogleCloudPlatform/github-actions@0.1.2
with:
project_id: example-prod
service_account_key: ${{ secrets.GCLOUD_AUTH_KEY_PROD }}
- name: Select project
run: gcloud config set project example-prod
- name: Select Firebase project
uses: w9jds/firebase-action@v1.3.0
with:
args: use production
- name: Generate backup file name
run: |
echo "::set-env name=BACKUP_FILE::$(date '+%Y-%m-%d').json"
- name: Download RTDB data
uses: w9jds/firebase-action@v1.3.0
with:
args: database:get --output ${{ env.BACKUP_FILE }} /
- name: Upload to Cloud Storage Bucket
run: gsutil cp ${{ env.BACKUP_FILE }} ${{ env.BACKUP_BUCKET }}/rtdb

Finally

Sleep well knowing that your project is backed up on a regular basis.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay