Firestore Backup and Restore


TL;DR

Check out FirestoreRestore. It uses a brand new API to backup and restore your Firestore database!

Background

There have been many requests for Google to add backup and restore functionality to their Cloud Firestore offering. Until this week, the only method for backing up a Firestore database has been to run utilities that traverse the tree as an admin client, saving each document’s JSON representation on disk.

Discovery

This morning, I opened the Google APIs Explorer and found two new methods listed under Firestore v1beta1: exportDocuments and importDocuments.

APIs Explorer Screenshot

Exploration

As the API documentation states, firestore.projects.databases.exportDocuments is able to export Firestore data to Google Cloud Storage.

To test this out, I opened up the Firebase Console and created a new project. In order for there to be data to back up, I created a user document from the console.

Creating a document

In the same project, I opened the Google Cloud Storage Console and created a bucket in which to store my backups.

From the Google APIs Explorer’s exportDocuments page, I entered the Firebase project name, as well as a path to the storage bucket, and hit Execute.

Execute!

The following is the API’s response:

{
 "name": "projects/firestore-restore/databases/(default)/operations/<REDACTED>",
 "metadata": {
  "@type": "type.googleapis.com/google.firestore.admin.v1beta1.ExportDocumentsMetadata",
  "startTime": "2018-07-18T23:20:26.535130Z",
  "operationState": "PROCESSING",
  "outputUriPrefix": "gs://firestore-restore/backups/2018-07-18"
 }
}

A few moments later, I opened the bucket. Sure enough, the data was present!

Backup in the bucket

Restore

Now, to test the restore functionality. I deleted the contents of the Firestore database, and ran importDocuments with the same parameters as the export request, and sure enough, the data came back!

Is the Backup Complete?

Unfortunately, there is currently no “documented” way to check the status of a Firestore export in progress. Luckily, with a little URL guessing, I was able to find the endpoint for the operation resource. It was there all along; the name field in the export response is the status path!

A Temporary Tool: FirestoreRestore

As I don’t know when Google will officially support and build tooling for backing up and restoring Cloud Firestore databases, I developed a simple command line utility to interact with the service.

It requires the creation of a service account with the “Cloud Datastore Import Export Admin” role. With a JSON key for the service account, this tool is able to perform a backup and restore.

Check out the README for more info 😀

Speculation

Google will likely launch backup and restore functionality within the Firebase UI. It’ll probably expose the per-collection backup capability that is present in the API. It’ll probably be much easier to use than some command line program you found on the internet.

Until then, enjoy the CLI!