How to Back up Cloud Firestore Periodically


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.

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


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


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.


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

exports.backupFirestore = functions.pubsub.topic('firebase-backup').onPublish((message, context) => {
  const projectId = 'PROJECT_ID'
  const getAccessToken = new Promise(function (resolve, reject) {
    const scopes = ['', '']
    const key = require(`./${projectId}.json`)
    const jwtClient = new google.auth.JWT(
    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 = `${projectId}/databases/(default):exportDocuments`
    return, {
      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

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