DEV Community

Cover image for How to programmatically backup your Firestore database with simple steps
Marcelo Costa
Marcelo Costa

Posted on

How to programmatically backup your Firestore database with simple steps

Why this post? Recently, Google Cloud announced in preview a way to automatically setup and schedule your Firestore backups. Prior to the announcement, the recommended approach required multiple serverless components, such as Cloud Functions and Cloud Scheduler.

At the time this post was written, there was no public documentation around how to use Google Cloud APIs to run the aforementioned feature, but using gcloud:
Image description

How to do it programmatically with Python

Many users are not aware, but sometimes the newest API operations or available features are not immediately available on Google SDKs, but you have something they call discovery API client:

In summary, the Google API Discovery service simplifies the process of working with Google APIs by providing structured and standardized documentation, which under the hood is utilized by their client libraries:
Image description

Basically, it's a document that tells machines how to interact with their APIs, which sometimes can be helpful as documentation. I recommend always using each of Google's SDK services and relying on the discovery client if the operation is unavailable in the SDK or if you want to get more details on what is available for that service with its models.

Then how to use it?

First, start by installing the google-api-python-client PyPI package.

Image description

Next, after looking at the discovery JSON that you can get in this link, and finding what is the right service and operation you need to call, you build the service object:

Image description

Then, by inspecting what the gcloud command was doing, I got to the service I needed:

Image description

The full code sample is here; I hope it helps!

import googleapiclient.discovery

# change to your project and db ids
project_id = "MY_PROJECT_ID"
database_id = "MY_FIRSTORE_DB_ID"

api_service_name = "firestore"
api_version = "v1"
discovery_url = f"https://{api_service_name}.googleapis.com/$discovery/rest?version={api_version}"
service = googleapiclient.discovery.build(
    api_service_name, api_version, discoveryServiceUrl=discovery_url
)
created_backup = (
    service.projects()
    .databases()
    .backupSchedules()
    .create(
        parent=f"projects/{project_id}/databases/{database_id}",
        body={
            "retention": "604800s",
            "dailyRecurrence": {},
        },
    )
    .execute()
)
Enter fullscreen mode Exit fullscreen mode

I chose 604800s, equivalent to 7 days, and dailyRecurrence which doesn't require any payload attributes for daily backups. If you are looking to schedule it weekly, you may change dailyRecurrence to something like this:

"weeklyRecurrence": {
  # day of week enum
  "day": "MONDAY"
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)