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:

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:

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.

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:

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

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()
)
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()
)
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"
}
"weeklyRecurrence": {
  # day of week enum
  "day": "MONDAY"
}
"weeklyRecurrence": { # day of week enum "day": "MONDAY" }

Enter fullscreen mode Exit fullscreen mode

原文链接:How to programmatically backup your Firestore database with simple steps

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
The worst sort of indolence is being given a choice, yet taking no initiative to change.
我们人生中最大的懒惰,就是当我们明知自己拥有作出选择的能力,却不去主动改变而是放任它的生活态度
评论 抢沙发

请登录后发表评论

    暂无评论内容