How to automatically backup Firestore database daily

本秂侑毒 提交于 2020-03-18 04:15:12

问题


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.



来源:https://stackoverflow.com/questions/50345411/how-to-automatically-backup-firestore-database-daily

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!