Skip to content

Showing how to use Cloud Scheduler and Cloud Functions to configure a schedule for creating Cloud Spanner backups.

License

Notifications You must be signed in to change notification settings

cloudspannerecosystem/scheduled-backups

Repository files navigation

Scheduled Backups

This example shows how to use Cloud Scheduler and Cloud Function to configure a schedule for creating Cloud Spanner backups.

Run SpannerCreateBackup locally

Start a terminal for running a local server:

go run cmd/local_func_server/main.go

Start another terminal for calling the function:

DATA=$(printf '{"database":"projects/[PROJECT_ID]/instances/[INSTANCE_ID]/databases/[DATABASE_ID]", "expire": "6h"}'|base64|tr -d '\n') && curl --data '{"data":"'$DATA'"}' localhost:8080

Run SpannerCreateBackup in Cloud

Create a pub/sub topic:

gcloud pubsub topics create cloud-spanner-scheduled-backups

Deploy the SpannerCreateBackup function that subscribes to the above topic:

gcloud functions deploy SpannerCreateBackup --trigger-topic cloud-spanner-scheduled-backups --runtime go113

Call the SpannerCreateBackup function from the command-line:

DATA=$(printf '{"database":"projects/[PROJECT_ID]/instances/[INSTANCE_ID]/databases/[DATABASE_ID]", "expire": "6h"}'|base64|tr -d '\n') && gcloud functions call SpannerCreateBackup --data '{"data":"'$DATA'"}'

Deploy scheduled jobs to Cloud Scheduler

Note: To use Cloud Scheduler, we must create an App Engine app. Cloud Scheduler must be deployed in the same region with App Engine. To specify a region, add a location flag to the go run cmd/scheduler/main.go ... command (default is set to us-central1).

Make a copy of schedule-template.yaml, name it as schedule.config.yaml and replace PROJECT_ID, INSTANCE_ID, DATABASE_ID with your configurations.

Deploy scheduled jobs for creating backups:

go run cmd/scheduler/main.go -config schedule.config.yaml

Email notification of backup failures

To get email notification, we need to do the following three steps.

Set up email notification channel

We can follow this guide to add our email address as a notification channel.

Add logs-based metrics

We can add logs-based metrics via GCP Console, API, gcloud, etc. Here, for convenience, we use Deployment Manager to create custom metrics.

gcloud deployment-manager deployments create schedule-backup-metrics-deployment --config metrics.yaml

After this, we should see three user-defined metrics under Logs-based Metrics in Cloud Logging.

Create alerting policies

We need to create alerting policies that define when we should send an alert notification.

Cloud Monitoring API is still under alpha, so we would recommend using GCP console to create the alerting policies.

The easiest way is to go to Logs-based Metrics under Cloud Logging and for each user-defined metric, there is an option Create alert from metric. From there, we can choose Aggregrator, such as sum or mean, for the target metric, and define what the condition of triggering an alert is, e.g., any time series violates that the value is above 0 for 1 minute.

At last, we need to add notification channels, e.g., email, to alerting policies.

About

Showing how to use Cloud Scheduler and Cloud Functions to configure a schedule for creating Cloud Spanner backups.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages