Automate Daily Cloud Firestore Backups with GitHub Actions (Updated for 2023)

Managing and backing up your data is crucial for any application, especially when using a service like Google Firestore. In this post, we’ll show you how to automate Firestore backups using GitHub Actions. This guide is an update to a previous tutorial on fireship.io and is up-to-date as of 2023.

Prerequisites

  • A Firebase project with Firestore enabled
  • A GitHub repository for your project

Step 1: Create a Google Cloud Storage Bucket

  1. Go to the Google Cloud Console
  2. Navigate to your Firebase project
  3. Go to Storage > Browser
  4. Click “Create Bucket”
  5. Choose a unique name for your bucket and follow the prompts to create the bucket

Step 2: Create a Google Cloud Service Account

  1. Go to the Google Cloud Console
  2. Navigate to your Firebase project
  3. Go to IAM & Admin > Service accounts
  4. Click “Create Service Account”
  5. Fill in the required details and click “Create”
  6. Grant the service account the “Cloud Datastore Import Export Admin” and “Storage Object Admin” roles
  7. Click “Continue” and then “Done”

Step 3: Generate a Service Account Key

  1. In the GCP “Service accounts” page, click on the three “Actions” dots beside the service account that you just created
  2. Click “Manage Keys” on the popover
  3. Click “Add Key” then “Create a new key” and select “JSON”
  4. Save the generated JSON file securely

Step 4: Add the Service Account Key to Your GitHub Repository as a Secret

  1. Open your GitHub repository and go to Settings > Secrets and variables > Actions
  2. Click “New repository secret”
  3. Name the secret “GCP_FIRESTORE_BACKUPS_SERVICE_ACCOUNT_KEY”
  4. “Compress” your JSON into a single line string by removing line breaks and spaces as recommended here
  5. Copy the single line JSON string and paste it into the “Value” field
  6. Click “Add secret”

Step 5: Create a GitHub Actions Workflow

  1. In your GitHub repository, create a new file in the .github/workflows directory named backup-firestore.yml
  2. Paste the following contents into the file:
name: Backup Firestore Daily

on:
  schedule:
    - cron:  '0 0 * * *'
  workflow_dispatch:

env:
  PROJECT_ID: your-project-id
  BUCKET: gs://your-storage-bucket
  
jobs:
  backup:
    runs-on: ubuntu-latest
    steps:
    - id: 'auth'
      name: 'Authenticate to Google Cloud'
      uses: 'google-github-actions/auth@v1'
      with:
        credentials_json: ${{ secrets.GCP_FIRESTORE_BACKUPS_SERVICE_ACCOUNT_KEY }}
    - run: gcloud info
    - run: gcloud config set project $PROJECT_ID
    - run: gcloud firestore export $BUCKET
  1. Replace your-project-id with your Firebase project ID and your-storage-bucket with your Google Cloud Storage bucket URL
  2. Commit and push the changes to your GitHub repository

Step 6: Test the Workflow

  1. In your GitHub repository, go to the “Actions” tab
  2. Select the “Backup Firestore Daily” workflow from the list
  3. Click “Run workflow” to start the backup process (it shouldn’t matter which branch you choose)

Once the workflow runs successfully, you should see your Firestore data backed up in the specified Google Cloud Storage bucket. The workflow is scheduled to run daily, as specified by the cron expression in the on.schedule section of the workflow configuration.

Daniel Ellis

I'm a full-stack TypeScript developer and a passionate geek