How to Use the Firestore Export API Before it is Released
Tags: firestore, firebase, backup, tool
Published: 2018-07-20 23:48:46

---
title: How to Use the Firestore Export API Before it is Released
published: true
description: Google silently added export and import routes to its Cloud Firestore API. A utility now exists to use this API while Google builds their own tooling.
tags: firestore,firebase,backup,tool
canonical_url: https://christianalexander.com/2018/07/18/firestore-backup-restore/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/auneej4djf68ze9biw7i.png
---

## TL;DR

Check out [FirestoreRestore](https://github.com/christianalexander/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](https://developers.google.com/apis-explorer/#s/firestore/v1beta1/) and found two new methods listed under Firestore `v1beta1`: `exportDocuments` and `importDocuments`.

![APIs Explorer Screenshot](https://thepracticaldev.s3.amazonaws.com/i/mypdp1zzzof9nv0cl0l0.png)

## 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](https://console.firebase.google.com) 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](https://thepracticaldev.s3.amazonaws.com/i/cxrb8v1lyzh7zrzpv4ty.png)

In the same project, I opened the [Google Cloud Storage Console](https://console.cloud.google.com/storage/) and created a bucket in which to store my backups.

From the [Google APIs Explorer's `exportDocuments` page](https://developers.google.com/apis-explorer/#s/firestore/v1beta1/firestore.projects.databases.exportDocuments), I entered the Firebase project name, as well as a path to the storage bucket, and hit **Execute**.

![Execute!](https://thepracticaldev.s3.amazonaws.com/i/oy92jzhev0sspj14dtoq.png)

The following is the API's response:

```json
{
 "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](https://thepracticaldev.s3.amazonaws.com/i/icikz8lm449v1ytzhy3d.png)

## Restore

Now, to test the restore functionality. I deleted the contents of the Firestore database, and [ran `importDocuments`](https://developers.google.com/apis-explorer/#s/firestore/v1beta1/firestore.projects.databases.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](https://github.com/christianalexander/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](https://console.cloud.google.com/iam-admin/serviceaccounts/project) 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](https://github.com/ChristianAlexander/FirestoreRestore#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!