How to programmatically backup your Firestore database with simple steps
Tags: firestore, googlecloud, database, python
Published: 2024-04-28 19:40:33

**Why this post**? Recently, Google Cloud announced in preview a way to [automatically setup and schedule](https://cloud.google.com/firestore/docs/backups) your Firestore backups. Prior to the announcement, the [recommended approach](https://cloud.google.com/firestore/docs/solutions/schedule-export) 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](https://cloud.google.com/firestore/docs/backups#create_a_daily_backup_schedule):
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8lcjvpocoxuvfny3npj.png)

# 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](https://developers.google.com/apis-explorer):

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8cbm2h3v8hec2lf7xcy6.png)

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](https://pypi.org/project/google-api-python-client/) PyPI package.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kie8ibne0qvkne3ck07b.png)

Next, after looking at the discovery JSON that you can get in this [link](https://developers.google.com/apis-explorer/), and finding what is the right service and operation you need to call, you build the service object:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lcdj25uc59o4pr2h0lqg.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuts4o45tnvrebn8mqit.png)

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()
)
```
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"
}
```