18
votes

I would like to backup a Firestore database on a daily basis.

My data structure is like this:

usersCollection
  uid
     defaultCurrency: 'USD'
     name: 'something'
     dreamsCollection
                     name
                     image

I have looked at firestore-export-import and node-firestore-backup to do the backup and export the data to a JSON file.

My questions are:

  1. If I create a cloud function how do I run the cloud function daily?
  2. What is the best way to do the backup?
    • Should I store the data in firebase storage and how to do this?
    • Should I save the JSON to a separate service etc?

Any help, no matter how small would be appreciated.

Thanks

4

4 Answers

9
votes

You can now export and import the data to the specified bucket.

More info here: https://firebase.google.com/docs/firestore/manage-data/export-import

Here is an info on how to do it automatically:

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

9
votes

I honestly don't like the sample solution provided on the firebase page. It's unnecessarily convoluted especially the part where you need to run things on app engine ¯\_(ツ)_/¯

There is a much simpler way to do this using the Cloud Scheduler and google-auth-library, to directly store the backups in the Firebase Cloud Storage. I have written an in depth article with the seamlessly working code that I'm using in production. Have a look.

https://blog.emad.in/automate-firestore-db-backups/

4
votes

I've blogged about creating a fully managed, serverless automatically triggered Workflow that triggers the Firestore export/backup API, and it places into a Cloud Storage bucket, this way you ensure proper backup for disaster recovery.

As it’s serverless no maintenance of SDK tools, no updates to libraries are involved, and even a non-developer can set it up.

Full article is here: Firestore Backups the easy way with Cloud Workflows

enter image description here

2
votes

Approach 1 (App Engine)

I've been using this solution for months, and it works well: https://github.com/firebase/snippets-node/tree/master/firestore/solution-scheduled-backups

This sample demonstrates using AppEngine cron jobs to run nightly backups of data in Cloud Firestore.

Approach 2 (Cloud Scheduler)

That said, the approach described by @eMAD looks good as well; it's based on the same gcloud command, but the Cloud Scheduler it uses may be preferred over the App Engine scheduling. (for example, it lets you specify an exact time of day, instead of it just being the time at which you first set up the schedule)

There's also this helper package for setting up Cloud-Scheduler-based automated backups.