How to Back up Cloud Firestore Periodically

Object

To back up Cloud Firestore periodically. You can back up Firebase Realtime Database easily from the console, but you cannot do that with Cloud Firestore. In this article, I will explain how to back up Cloud Firestore with exportDocuments API.

Step1 – Get private key of service account from GCP console

Go to Google Cloud Platform Console. IAM & admin → Service account. Click “create service account” and make the account. Do not forget to check “furnish a new private key”.

For project role, choose “Cloud Datastore Import Export Admin” and “Storage Object Admin”.

Step2 – Make a bucket in the storage of GCP

Google Cloud Platform Console → Storage → Browser

Step3 – Schedule (cron) job with Cloud Functions

Following the link below, make a Cloud Function.
https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html

This is the example of the function which is triggered every 24 hours.

cron.yaml

cron:
- description: make a backup for firestore everyday at 00:00
  url: /publish/firebase-backup
  schedule: every 24 hours

functions/index.js

exports.backupFirestore = functions.pubsub.topic('firebase-backup').onPublish((message, context) => {
  return true
})

Step4 – Use exportDocuments API in the function

Put the secret key you got in step1 in the function folder with the name of the project id. Edit the function you made on the step3 like below. You can use environmental variable instead of putting the secret key directly to the function folder.

functions/index.js

const {google} = require('googleapis')
const rp = require('request-promise')
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()

exports.backupFirestore = functions.pubsub.topic('firebase-backup').onPublish((message, context) => {
  const projectId = 'PROJECT_ID'
  const getAccessToken = new Promise(function (resolve, reject) {
    const scopes = ['https://www.googleapis.com/auth/datastore', 'https://www.googleapis.com/auth/cloud-platform']
    const key = require(`./${projectId}.json`)
    const jwtClient = new google.auth.JWT(
      key.client_email,
      undefined,
      key.private_key,
      scopes,
      undefined
    )
    const authorization = new Promise(function (resolve, reject) {
      return jwtClient.authorize().then((value) => {
        return resolve(value)
      })
    })
    return authorization.then(function (value) {
      return resolve(value.access_token)
    })
  })
  return getAccessToken.then(function (accessToken) {
    const url = `https://firestore.googleapis.com/v1beta1/projects/${projectId}/databases/(default):exportDocuments`
    return rp.post(url, {
      headers: {
        Authorization: `Bearer ${accessToken}`
      },
      json: true,
      body: {
        outputUriPrefix: `gs://${projectId}-backups`
      }
    })
  })
})

Deploy and check if it works!

You might have to change the permission of the bucket so that it accepts the access from YOUR_SERVICE_ID@appspot.gserviceaccount.com

Step5 – Configure lifecycle of backup

If you want to delete the old backups, you can do that by making lifecycle rule to the bucket in the storage.

2 thoughts on “How to Back up Cloud Firestore Periodically”

Leave a Comment