25

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

Peter Fields
  • 389
  • 1
  • 3
  • 9

5 Answers5

14

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/

eMAD
  • 473
  • 4
  • 6
  • Awesome! Maybe a silly follow-up question, how would one restore the firestore snapshot from the backup ? – Sebastien FERRAND Mar 23 '22 at 17:32
  • 1
    @SebastienFERRAND I came across your question while trying to find an answer to the same question for myself (how to restore a Firestore backup). The Firebase documentation encourages you to load it in BigQuery, but that will not work if you are exporting all of your collections. Instead you can use the Firebase Import/Screen screen in the GCP console to re-import the data. You can either overwrite your existing Firestore, or you can create a new Firebase project and load it there for viewing. – most200 May 12 '22 at 21:36
12

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

uksz
  • 18,239
  • 30
  • 94
  • 161
  • 3
    IMHO it's not a good solution. It should be a simple Cloud Function to deploy, not needing to use App Engine. – Jeremy Belolo Aug 30 '19 at 07:21
  • Also, this solution doesn't work. The permissions for Firestore are never approved for AppEngine to connect.. – Oliver Dixon Apr 20 '20 at 19:45
  • 1
    I don't understand the above comments. Just following : https://firebase.google.com/docs/firestore/solutions/schedule-export does exactly what I want. I used the console path, pasted the code, created a bucket where to store to, setup the job, gave the rights and it works. Added a retention ploicy and a rule to clean up, done. – Vincent Gerris May 26 '20 at 13:02
8

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

Pentium10
  • 204,586
  • 122
  • 423
  • 502
4

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.

Venryx
  • 15,624
  • 10
  • 70
  • 96
0

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