Peter Fields
Peter Fields

Reputation: 389

How to automatically backup Firestore database daily

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

Upvotes: 25

Views: 15065

Answers (5)

Alexandru Pavaloi
Alexandru Pavaloi

Reputation: 29

A new feature in Firebase might be of help with backups up to 7 days: Point-in-time recovery.

https://cloud.google.com/firestore/docs/pitr

Upvotes: 0

Venryx
Venryx

Reputation: 17999

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.

EDIT: I now use the helper-package mentioned above (simple-firestore-backup), as the npm-package helps keep the implementation consistent between projects, and I like it over the app-engine solution because cloud-functions have more flexibility.

Note however that the readme is currently "missing" a few steps (depending on what you consider "self-evident" or not). If you want a more comprehensive set of instructions, augment the readme with the additional steps I explain here.

Upvotes: 5

eMAD
eMAD

Reputation: 503

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/

Upvotes: 16

Pentium10
Pentium10

Reputation: 207912

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

Upvotes: 8

uksz
uksz

Reputation: 18699

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

Upvotes: 12

Related Questions