How to automatically backup Firestore database dai

2020-07-06 02:39发布

问题:

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

回答1:

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



回答2:

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 much a 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/



回答3:

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.