Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Automatic backup firestore

Hello. I would like to know how I can back up my firestore database automatically with some frequency, for example every hour.

0 1 1,433
1 REPLY 1

To schedule automatic backups of your Firestore database, Its recommended to use a combination of Cloud Functions and Cloud Scheduler. Here are the steps you can follow:

  1. Before you begin:

    • Enable billing for your Google Cloud project. Only Google Cloud projects with billing enabled can use the export and import feature. Note that Firebase projects must be on the Blaze plan to use the managed export and import feature. Enabling billing for the Google Cloud automatically upgrades your Firebase project to the Blaze plan.
    • Export operations require a destination Cloud Storage bucket. Create a Cloud Storage bucket in a location near your Cloud Firestore database location. You cannot use a Requester Pays bucket for export operations​
  2. Create a Cloud Function:

    • You can create a Cloud Function using either the Firebase CLI or the GCP Console.
    • For the Firebase CLI, you would initialize the CLI for Cloud Functions in a new directory, replace the code in the functions/index.js file with the provided export script (modifying it as needed), and then deploy the scheduled function​1​.
    • For the GCP Console, you would open the Cloud Functions page, create a new function with Cloud Pub/Sub as the trigger, and enter the provided export script (again, modifying it as needed) in the Inline editor​
  3. Create a Cloud Scheduler job:

    • You would open the Cloud Scheduler page in the GCP Console, create a new job with the desired frequency (e.g., every 24 hours), select Pub/Sub as the target and enter the name of the pub/sub topic you defined when creating the Cloud Function. In the Payload field, you would enter start export (the job requires a payload defined, but the Cloud Function does not actually use this value)​
  4. Configure access permissions:

    • The Cloud Function needs permission to start export operations and to write to your GCS bucket. It uses your project's default service account to authenticate and authorize its export operations. This service account requires permission to start an export operation and to write to your Cloud Storage bucket. To grant these permissions, assign the Cloud Datastore Import Export Admin role (this Datastore role also grants permissions for Cloud Firestore) and either the Owner or Storage Admin role on the bucket to the default service account. You can use the gcloud and gsutil command-line tools to assign these roles​

For more details please see:

https://firebase.google.com/docs/firestore/solutions/schedule-export