Create and manage backup schedules
This page describes Spanner backup schedule operations and explains how to set up a backup schedule for your database.
Before you begin
-
To get the permissions that you need to create and manage backup schedules, ask your administrator to grant you the following IAM roles on the instance:
-
Create, view, update, and delete backup schedules:
Cloud Spanner Backup Admin (
roles/spanner.backupAdmin) -
Create and view backup schedules:
Cloud Spanner Backup Writer (
roles/spanner.backupWriter)
-
Create, view, update, and delete backup schedules:
Cloud Spanner Backup Admin (
Create a backup schedule
Console
- In the Google Cloud console, go to the Spanner Instances page.
Go to the Spanner Instances page
Click the instance containing the database.
Click the database.
In the navigation menu, click the Backup/Restore.
Click Create backup schedule.
Fill out the form, then click Create.
gcloud
Before using any of the command data below, make the following replacements:
- SCHEDULE_ID: the backup schedule ID.
- PROJECT_ID: the project ID.
- INSTANCE_ID: the ID of the instance where you want to create the backup schedule.
- DATABASE_ID: the ID of the database where you want to create the backup schedule.
-
RETENTION_DURATION: the retention duration of
the backups created by the schedule. For example, if you want the retention duration to be one
day, you can use
86400s. -
CRONTAB_EXPRESSION:
the crontab expression for the backup schedule frequency. For example, if you want the
backup schedule frequency to be every 12 hours, you can use
0 12 * * *. -
BACKUP_TYPE:
whether it's a full backup schedule or an incremental backup schedule. Possible values are
full-backuporincremental-backup. -
ENCRYPTION_TYPE:
the encryption type of backups created by the backup schedule.
Valid values are
USE_DATABASE_ENCRYPTION,GOOGLE_DEFAULT_ENCRYPTION, orCUSTOMER_MANAGED_ENCRYPTION. If you useCUSTOMER_MANAGED_ENCRYPTION, you must specify akmsKeyName. If your backup type isincremental-backup, the encryption type must beGOOGLE_DEFAULT_ENCRYPTION.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloudspannerbackup-schedulescreateSCHEDULE_ID\ --project=PROJECT_ID\ --instance=INSTANCE_ID\ --database=DATABASE_ID\ --retention-duration=RETENTION_DURATION\ --cron="CRONTAB_EXPRESSION"\ --backup-type=BACKUP_TYPE\ --encryption-type=ENCRYPTION_TYPE
Windows (PowerShell)
gcloudspannerbackup-schedulescreateSCHEDULE_ID` --project=PROJECT_ID` --instance=INSTANCE_ID` --database=DATABASE_ID` --retention-duration=RETENTION_DURATION` --cron="CRONTAB_EXPRESSION"` --backup-type=BACKUP_TYPE` --encryption-type=ENCRYPTION_TYPE
Windows (cmd.exe)
gcloudspannerbackup-schedulescreateSCHEDULE_ID^ --project=PROJECT_ID^ --instance=INSTANCE_ID^ --database=DATABASE_ID^ --retention-duration=RETENTION_DURATION^ --cron="CRONTAB_EXPRESSION"^ --backup-type=BACKUP_TYPE^ --encryption-type=ENCRYPTION_TYPE
REST v1
Before using any of the request data, make the following replacements:
- PROJECT_ID: the project ID.
- INSTANCE_ID: the ID of the instance where you want to create the backup schedule.
- DATABASE_ID: the ID of the database where you want to create the backup schedule.
- SCHEDULE_ID: the backup schedule ID.
-
BACKUP_TYPE:
whether it's a full backup schedule or an incremental backup schedule.
Possible values are
fullBackupSpecorincrementalBackupSpec. -
ENCRYPTION_TYPE: the encryption type of backups
created by the backup schedule. Possible values are
USE_DATABASE_ENCRYPTION,GOOGLE_DEFAULT_ENCRYPTION, orCUSTOMER_MANAGED_ENCRYPTION. If you useCUSTOMER_MANAGED_ENCRYPTION, you must specify akmsKeyName. If your backup type isincremental-backup, the encryption type must beGOOGLE_DEFAULT_ENCRYPTION. - RETENTION_DURATION: the retention duration of the backups created by the schedule.
HTTP method and URL:
POST https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules?backup_schedule_id=SCHEDULE_ID
Request JSON body:
{
"retentionDuration": "RETENTION_DURATION",
"spec": {
"cronSpec": {
"text": "0 2 * * *"
}
},
"encryptionConfig": {
"encryptionType": "ENCRYPTION_TYPE"
},
"BACKUP_TYPE": {}
}
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Save the request body in a file named request.json,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules?backup_schedule_id=SCHEDULE_ID"
PowerShell (Windows)
Save the request body in a file named request.json,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules?backup_schedule_id=SCHEDULE_ID" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{
"name": "projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID",
"retentionDuration": "86400s",
"encryptionConfig": {
"encryptionType": "USE_DATABASE_ENCRYPTION"
},
"spec": {
"cronSpec": {
"text": "0 2 * * *",
"timeZone": "UTC",
"creationWindow": "14400s"
}
},
"BACKUP_TYPE": {},
"updateTime": "2024-05-22T11:13:51.835590Z"
}
Client libraries
C#
To create a full backup schedule, see the following sample code:
usingGoogle.Cloud.Spanner.Admin.Database.V1 ;
usingGoogle.Cloud.Spanner.Common.V1 ;
usingGoogle.Protobuf.WellKnownTypes ;
usingSystem;
publicclassCreateFullBackupScheduleSample
{
publicBackupScheduleCreateFullBackupSchedule(stringprojectId,stringinstanceId,stringdatabaseId,stringscheduleId)
{
DatabaseAdminClient client=DatabaseAdminClient .Create ();
BackupSchedule response=client.CreateBackupSchedule (
newCreateBackupScheduleRequest
{
ParentAsDatabaseName=DatabaseName .FromProjectInstanceDatabase (projectId,instanceId,databaseId),
BackupScheduleId=scheduleId,
BackupSchedule=newBackupSchedule
{
Spec=newBackupScheduleSpec
{
CronSpec=newCrontabSpec
{
Text="30 12 * * *",
}
},
RetentionDuration=newDuration
{
Seconds=86400,
},
EncryptionConfig=newCreateBackupEncryptionConfig
{
EncryptionType=CreateBackupEncryptionConfig .Types .EncryptionType .UseDatabaseEncryption ,
},
FullBackupSpec=newFullBackupSpec {},
}
});
Console.WriteLine($"Created full backup schedule: {response}");
returnresponse;
}
}usingGoogle.Cloud.Spanner.Admin.Database.V1 ;
usingGoogle.Cloud.Spanner.Common.V1 ;
usingGoogle.Protobuf.WellKnownTypes ;
usingSystem;
publicclassCreateIncrementalBackupScheduleSample
{
publicBackupScheduleCreateIncrementalBackupSchedule(stringprojectId,stringinstanceId,stringdatabaseId,stringscheduleId)
{
DatabaseAdminClient client=DatabaseAdminClient .Create ();
BackupSchedule response=client.CreateBackupSchedule (
newCreateBackupScheduleRequest
{
ParentAsDatabaseName=DatabaseName .FromProjectInstanceDatabase (projectId,instanceId,databaseId),
BackupScheduleId=scheduleId,
BackupSchedule=newBackupSchedule
{
Spec=newBackupScheduleSpec
{
CronSpec=newCrontabSpec
{
Text="30 12 * * *",
}
},
RetentionDuration=newDuration
{
Seconds=86400,
},
EncryptionConfig=newCreateBackupEncryptionConfig
{
EncryptionType=CreateBackupEncryptionConfig .Types .EncryptionType .GoogleDefaultEncryption ,
},
IncrementalBackupSpec=newIncrementalBackupSpec {},
}
});
Console.WriteLine($"Created incremental backup schedule: {response}");
returnresponse;
}
}C++
To create a full backup schedule, see the following sample code:
voidCreateFullBackupSchedule(
google::cloud::spanner_admin::DatabaseAdminClientclient,
std::stringconst&project_id,std::stringconst&instance_id,
std::stringconst&database_id,std::stringconst&backup_schedule_id){
google::spanner::admin::database::v1::BackupSchedulebackup_schedule;
*backup_schedule.mutable_full_backup_spec()={};
backup_schedule.mutable_spec()->mutable_cron_spec()->set_text("30 12 * * *");
backup_schedule.mutable_retention_duration()->set_seconds(3600*24);
google::spanner::admin::database::v1::CreateBackupEncryptionConfig
encryption_config;
encryption_config.set_encryption_type(
google::spanner::admin::database::v1::CreateBackupEncryptionConfig::
USE_DATABASE_ENCRYPTION);
*backup_schedule.mutable_encryption_config()=std::move(encryption_config);
google::cloud::spanner::Databasedb(project_id,instance_id,database_id);
google::spanner::admin::database::v1::CreateBackupScheduleRequestrequest;
request.set_parent(db.FullName());
request.set_backup_schedule_id(backup_schedule_id);
*request.mutable_backup_schedule()=std::move(backup_schedule);
autocreated_backup_schedule=client.CreateBackupSchedule(request);
if(!created_backup_schedule){
throwstd::move(created_backup_schedule).status();
}
std::cout << "Backup schedule " << created_backup_schedule->name()
<< " created at "
<< *google::cloud::spanner::MakeTimestamp(
created_backup_schedule->update_time());
}voidCreateIncrementalBackupSchedule(
google::cloud::spanner_admin::DatabaseAdminClientclient,
std::stringconst&project_id,std::stringconst&instance_id,
std::stringconst&database_id,std::stringconst&backup_schedule_id){
google::spanner::admin::database::v1::BackupSchedulebackup_schedule;
*backup_schedule.mutable_incremental_backup_spec()={};
backup_schedule.mutable_spec()->mutable_cron_spec()->set_text("30 12 * * *");
backup_schedule.mutable_retention_duration()->set_seconds(3600*24);
google::spanner::admin::database::v1::CreateBackupEncryptionConfig
encryption_config;
encryption_config.set_encryption_type(
google::spanner::admin::database::v1::CreateBackupEncryptionConfig::
GOOGLE_DEFAULT_ENCRYPTION);
*backup_schedule.mutable_encryption_config()=std::move(encryption_config);
google::cloud::spanner::Databasedb(project_id,instance_id,database_id);
google::spanner::admin::database::v1::CreateBackupScheduleRequestrequest;
request.set_parent(db.FullName());
request.set_backup_schedule_id(backup_schedule_id);
*request.mutable_backup_schedule()=std::move(backup_schedule);
autocreated_backup_schedule=client.CreateBackupSchedule(request);
if(!created_backup_schedule){
throwstd::move(created_backup_schedule).status();
}
std::cout << "Incremental backup schedule " << created_backup_schedule->name()
<< " created at "
<< *google::cloud::spanner::MakeTimestamp(
created_backup_schedule->update_time());
}Go
To create a full backup schedule, see the following sample code:
import(
"context"
"fmt"
"io"
"time"
database"cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"google.golang.org/protobuf/types/known/durationpb"
)
funccreateFullBackupSchedule(wio.Writer,dbNamestring,scheduleIdstring)error{
ctx:=context.Background()
client,err:=database.NewDatabaseAdminClient(ctx)
iferr!=nil{
returnerr
}
deferclient.Close ()
// Create a schedule to create full backups daily at 12:30 AM, using the
// database's encryption config, and retained for 24 hours.
req:=databasepb.CreateBackupScheduleRequest{
Parent:dbName,
BackupScheduleId:scheduleId,
BackupSchedule:&databasepb.BackupSchedule{
Spec:&databasepb.BackupScheduleSpec{
ScheduleSpec:&databasepb.BackupScheduleSpec_CronSpec{
CronSpec:&databasepb.CrontabSpec{
Text:"30 12 * * *",
},
},
},
RetentionDuration:durationpb.New(24*time.Hour),
EncryptionConfig:&databasepb.CreateBackupEncryptionConfig{
EncryptionType:databasepb.CreateBackupEncryptionConfig_USE_DATABASE_ENCRYPTION ,
},
BackupTypeSpec:&databasepb.BackupSchedule_FullBackupSpec{},
},
}
res,err:=client.CreateBackupSchedule(ctx,&req)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"Created full backup schedule: %s",res)
returnnil
}
import(
"context"
"fmt"
"io"
"time"
database"cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"google.golang.org/protobuf/types/known/durationpb"
)
funccreateIncrementalBackupSchedule(wio.Writer,dbNamestring,scheduleIdstring)error{
ctx:=context.Background()
client,err:=database.NewDatabaseAdminClient(ctx)
iferr!=nil{
returnerr
}
deferclient.Close ()
// Create a schedule to create incremental backups daily at 12:30 AM, using
// Google-managed encryption, and retained for 24 hours.
req:=databasepb.CreateBackupScheduleRequest{
Parent:dbName,
BackupScheduleId:scheduleId,
BackupSchedule:&databasepb.BackupSchedule{
Spec:&databasepb.BackupScheduleSpec{
ScheduleSpec:&databasepb.BackupScheduleSpec_CronSpec{
CronSpec:&databasepb.CrontabSpec{
Text:"30 12 * * *",
},
},
},
RetentionDuration:durationpb.New(24*time.Hour),
EncryptionConfig:&databasepb.CreateBackupEncryptionConfig{
EncryptionType:databasepb.CreateBackupEncryptionConfig_GOOGLE_DEFAULT_ENCRYPTION ,
},
BackupTypeSpec:&databasepb.BackupSchedule_IncrementalBackupSpec{},
},
}
res,err:=client.CreateBackupSchedule(ctx,&req)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"Created incremental backup schedule: %s",res)
returnnil
}
Java
To create a full backup schedule, see the following sample code:
importcom.google.cloud.spanner.admin.database.v1.DatabaseAdminClient ;
importcom.google.protobuf.Duration ;
importcom.google.spanner.admin.database.v1.BackupSchedule ;
importcom.google.spanner.admin.database.v1.BackupScheduleSpec ;
importcom.google.spanner.admin.database.v1.CreateBackupEncryptionConfig ;
importcom.google.spanner.admin.database.v1.CreateBackupScheduleRequest ;
importcom.google.spanner.admin.database.v1.CrontabSpec ;
importcom.google.spanner.admin.database.v1.DatabaseName ;
importcom.google.spanner.admin.database.v1.FullBackupSpec ;
importjava.io.IOException;
class CreateFullBackupScheduleSample{
staticvoidcreateFullBackupSchedule()throwsIOException{
// TODO(developer): Replace these variables before running the sample.
StringprojectId="my-project";
StringinstanceId="my-instance";
StringdatabaseId="my-database";
StringbackupScheduleId="my-backup-schedule";
createFullBackupSchedule(projectId,instanceId,databaseId,backupScheduleId);
}
staticvoidcreateFullBackupSchedule(
StringprojectId,StringinstanceId,StringdatabaseId,StringbackupScheduleId)
throwsIOException{
finalCreateBackupEncryptionConfig encryptionConfig=
CreateBackupEncryptionConfig .newBuilder()
.setEncryptionType(CreateBackupEncryptionConfig .EncryptionType.USE_DATABASE_ENCRYPTION)
.build();
finalBackupSchedule backupSchedule=
BackupSchedule .newBuilder()
.setFullBackupSpec (FullBackupSpec .newBuilder().build())
.setRetentionDuration (Duration .newBuilder().setSeconds(3600*24).build())
.setSpec (
BackupScheduleSpec .newBuilder()
.setCronSpec (CrontabSpec .newBuilder().setText ("30 12 * * *").build())
.build())
.setEncryptionConfig(encryptionConfig)
.build();
try(DatabaseAdminClient databaseAdminClient=DatabaseAdminClient .create()){
DatabaseName databaseName=DatabaseName .of(projectId,instanceId,databaseId);
finalBackupSchedule createdBackupSchedule=
databaseAdminClient.createBackupSchedule(
CreateBackupScheduleRequest .newBuilder()
.setParent(databaseName.toString ())
.setBackupScheduleId (backupScheduleId)
.setBackupSchedule(backupSchedule)
.build());
System.out.println(
String.format(
"Created backup schedule: %s\n%s",
createdBackupSchedule.getName (),createdBackupSchedule.toString()));
}
}
}
importcom.google.cloud.spanner.admin.database.v1.DatabaseAdminClient ;
importcom.google.protobuf.Duration ;
importcom.google.spanner.admin.database.v1.BackupSchedule ;
importcom.google.spanner.admin.database.v1.BackupScheduleSpec ;
importcom.google.spanner.admin.database.v1.CreateBackupEncryptionConfig ;
importcom.google.spanner.admin.database.v1.CreateBackupScheduleRequest ;
importcom.google.spanner.admin.database.v1.CrontabSpec ;
importcom.google.spanner.admin.database.v1.DatabaseName ;
importcom.google.spanner.admin.database.v1.IncrementalBackupSpec ;
importjava.io.IOException;
class CreateIncrementalBackupScheduleSample{
staticvoidcreateIncrementalBackupSchedule()throwsIOException{
// TODO(developer): Replace these variables before running the sample.
StringprojectId="my-project";
StringinstanceId="my-instance";
StringdatabaseId="my-database";
StringbackupScheduleId="my-backup-schedule";
createIncrementalBackupSchedule(projectId,instanceId,databaseId,backupScheduleId);
}
staticvoidcreateIncrementalBackupSchedule(
StringprojectId,StringinstanceId,StringdatabaseId,StringbackupScheduleId)
throwsIOException{
finalCreateBackupEncryptionConfig encryptionConfig=
CreateBackupEncryptionConfig .newBuilder()
.setEncryptionType(
CreateBackupEncryptionConfig .EncryptionType.GOOGLE_DEFAULT_ENCRYPTION)
.build();
finalBackupSchedule backupSchedule=
BackupSchedule .newBuilder()
.setIncrementalBackupSpec (IncrementalBackupSpec .newBuilder().build())
.setRetentionDuration (Duration .newBuilder().setSeconds(3600*24).build())
.setSpec (
BackupScheduleSpec .newBuilder()
.setCronSpec (CrontabSpec .newBuilder().setText ("30 12 * * *").build())
.build())
.setEncryptionConfig(encryptionConfig)
.build();
try(DatabaseAdminClient databaseAdminClient=DatabaseAdminClient .create()){
DatabaseName databaseName=DatabaseName .of(projectId,instanceId,databaseId);
finalBackupSchedule createdBackupSchedule=
databaseAdminClient.createBackupSchedule(
CreateBackupScheduleRequest .newBuilder()
.setParent(databaseName.toString ())
.setBackupScheduleId (backupScheduleId)
.setBackupSchedule(backupSchedule)
.build());
System.out.println(
String.format(
"Created incremental backup schedule: %s\n%s",
createdBackupSchedule.getName (),createdBackupSchedule.toString()));
}
}
}Node.js
To create a full backup schedule, see the following sample code:
// Import the Google Cloud client library for Spanner.
const{Spanner}=require('@google-cloud/spanner');
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';
// const scheduleId = 'my-schedule-id';
// Create a Spanner database admin client.
constspanner=newSpanner ({projectId});
constclient=spanner.getDatabaseAdminClient ();
try{
// Create a schedule to create full backups daily at 12:30 AM, using the
// database's encryption config, and retained for 24 hours.
const[response]=awaitclient .createBackupSchedule({
parent:client .databasePath(projectId,instanceId,databaseId),
backupScheduleId:scheduleId,
backupSchedule:{
spec:{
cronSpec:{
text:'30 12 * * *',
},
},
retentionDuration:{
seconds:86400,
},
encryptionConfig:{
encryptionType:'USE_DATABASE_ENCRYPTION',
},
fullBackupSpec:{},
},
});
console.log('Created full backup schedule:',response);
}catch(err){
console.error('ERROR:',err);
}// Import the Google Cloud client library for Spanner.
const{Spanner}=require('@google-cloud/spanner');
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';
// const scheduleId = 'my-schedule-id';
// Create a Spanner database admin client.
constspanner=newSpanner ({projectId});
constclient=spanner.getDatabaseAdminClient ();
try{
// Create a schedule to create incremental backups daily at 12:30 AM,
// using Google-managed encryption, and retained for 24 hours.
const[response]=awaitclient .createBackupSchedule({
parent:client .databasePath(projectId,instanceId,databaseId),
backupScheduleId:scheduleId,
backupSchedule:{
spec:{
cronSpec:{
text:'30 12 * * *',
},
},
retentionDuration:{
seconds:86400,
},
encryptionConfig:{
encryptionType:'GOOGLE_DEFAULT_ENCRYPTION',
},
incrementalBackupSpec:{},
},
});
console.log('Created incremental backup schedule:',response);
}catch(err){
console.error('ERROR:',err);
}PHP
To create a full backup schedule, see the following sample code:
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupScheduleRequest;
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig;
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig\EncryptionType;
use Google\Cloud\Spanner\Admin\Database\V1\BackupSchedule;
use Google\Cloud\Spanner\Admin\Database\V1\FullBackupSpec;
use Google\Cloud\Spanner\Admin\Database\V1\BackupScheduleSpec;
use Google\Cloud\Spanner\Admin\Database\V1\CrontabSpec;
use Google\Protobuf\Duration;
/**
* Create a backup schedule.
* Example:
* ```
* create_backup_schedule($projectId, $instanceId, $databaseId, $backupScheduleId);
* ```
*
* @param string $projectId The Google Cloud project ID.
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
* @param string $backupScheduleId The ID of the backup schedule to be created.
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
*/
function create_backup_schedule(
string $projectId,
string $instanceId,
string $databaseId,
string $backupScheduleId,
): void {
$databaseAdminClient = new DatabaseAdminClient();
$databaseFullName = DatabaseAdminClient::databaseName($projectId, $instanceId, $databaseId);
printf('%s', $databaseFullName);
$encryptionConfig = (new CreateBackupEncryptionConfig())
->setEncryptionType(EncryptionType::USE_DATABASE_ENCRYPTION);
$backupSchedule = new BackupSchedule([
'full_backup_spec' => new FullBackupSpec(),
'retention_duration' => (new Duration())
->setSeconds(24 * 60 * 60),
'spec' => new BackupScheduleSpec([
'cron_spec' => new CrontabSpec([
'text' => '30 12 * * *'
]),
]),
'encryption_config' => $encryptionConfig,
]);
$request = new CreateBackupScheduleRequest([
'parent' => $databaseFullName,
'backup_schedule_id' => $backupScheduleId,
'backup_schedule' => $backupSchedule,
]);
$created_backup_schedule = $databaseAdminClient->createBackupSchedule($request);
printf('Created backup scehedule %s' . PHP_EOL, $created_backup_schedule->getName());
}Python
To create a full backup schedule, see the following sample code:
defcreate_full_backup_schedule(
instance_id: str,
database_id: str,
schedule_id: str,
) -> None:
fromdatetimeimport timedelta
fromgoogle.cloudimport spanner
fromgoogle.cloud.spanner_admin_database_v1.typesimport (
backup_schedule as backup_schedule_pb,
)
fromgoogle.cloud.spanner_admin_database_v1.typesimport (
CreateBackupEncryptionConfig ,
FullBackupSpec ,
)
client = spanner .Client ()
database_admin_api = client .database_admin_api
request = backup_schedule_pb.CreateBackupScheduleRequest (
parent=database_admin_api .database_path(
client .project, instance_id, database_id
),
backup_schedule_id=schedule_id,
backup_schedule=backup_schedule_pb.BackupSchedule (
spec=backup_schedule_pb.BackupScheduleSpec (
cron_spec=backup_schedule_pb.CrontabSpec (
text="30 12 * * *",
),
),
retention_duration=timedelta(hours=24),
encryption_config=CreateBackupEncryptionConfig(
encryption_type=CreateBackupEncryptionConfig .EncryptionType.USE_DATABASE_ENCRYPTION,
),
full_backup_spec=FullBackupSpec(),
),
)
response = database_admin_api .create_backup_schedule(request)
print(f"Created full backup schedule: {response}")
defcreate_incremental_backup_schedule(
instance_id: str,
database_id: str,
schedule_id: str,
) -> None:
fromdatetimeimport timedelta
fromgoogle.cloudimport spanner
fromgoogle.cloud.spanner_admin_database_v1.typesimport (
backup_schedule as backup_schedule_pb,
)
fromgoogle.cloud.spanner_admin_database_v1.typesimport (
CreateBackupEncryptionConfig ,
IncrementalBackupSpec ,
)
client = spanner .Client ()
database_admin_api = client .database_admin_api
request = backup_schedule_pb.CreateBackupScheduleRequest (
parent=database_admin_api .database_path(
client .project, instance_id, database_id
),
backup_schedule_id=schedule_id,
backup_schedule=backup_schedule_pb.BackupSchedule (
spec=backup_schedule_pb.BackupScheduleSpec (
cron_spec=backup_schedule_pb.CrontabSpec (
text="30 12 * * *",
),
),
retention_duration=timedelta(hours=24),
encryption_config=CreateBackupEncryptionConfig(
encryption_type=CreateBackupEncryptionConfig .EncryptionType.GOOGLE_DEFAULT_ENCRYPTION,
),
incremental_backup_spec=IncrementalBackupSpec(),
),
)
response = database_admin_api .create_backup_schedule(request)
print(f"Created incremental backup schedule: {response}")
Get a backup schedule
Console
- In the Google Cloud console, go to the Spanner Instances page.
Go to the Spanner Instances page
Click the instance containing the database.
Click the database.
In the navigation menu, click the Backup/Restore.
Click the Schedules tab to view all the backup schedules and their information.
gcloud
Before using any of the command data below, make the following replacements:
- PROJECT_ID: the project ID.
- INSTANCE_ID: the instance ID.
- DATABASE_ID: the database ID.
- SCHEDULE_ID: the backup schedule ID.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloudspannerbackup-schedulesdescribeSCHEDULE_ID\ --project=PROJECT_ID\ --instance=INSTANCE_ID\ --database=DATABASE_ID
Windows (PowerShell)
gcloudspannerbackup-schedulesdescribeSCHEDULE_ID` --project=PROJECT_ID` --instance=INSTANCE_ID` --database=DATABASE_ID
Windows (cmd.exe)
gcloudspannerbackup-schedulesdescribeSCHEDULE_ID^ --project=PROJECT_ID^ --instance=INSTANCE_ID^ --database=DATABASE_ID
You should receive a response similar to the following:
{
encryptionConfig:
encryptionType: USE_DATABASE_ENCRYPTION
incrementalBackupSpec: {}
name: projects/my-project/instances/my-instance/databases/my-database/backupSchedules/my-schedule
retentionDuration: 2592000s
spec:
cronSpec:
creationWindow: 14400s
text: 0 */4 * * *
timeZone: UTC
updateTime: '2024-09-13T10:24:18.754839Z'
}
REST v1
Before using any of the request data, make the following replacements:
- PROJECT_ID: the project ID.
- INSTANCE_ID: the instance ID.
- DATABASE_ID: the database ID.
- SCHEDULE_ID: the backup schedule ID.
HTTP method and URL:
GET https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID"
PowerShell (Windows)
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{
"name": "projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID",
"retentionDuration": "100000s",
"encryptionConfig": {
"encryptionType": "USE_DATABASE_ENCRYPTION"
},
"spec": {
"cronSpec": {
"text": "0 2 * * *",
"timeZone": "UTC",
"creationWindow": "14400s"
}
},
"fullBackupSpec": {},
"updateTime": "2024-05-22T11:13:51.835590Z"
}
Client libraries
C#
usingGoogle.Cloud.Spanner.Admin.Database.V1 ;
usingSystem;
publicclassGetBackupScheduleSample
{
publicBackupScheduleGetBackupSchedule(stringprojectId,stringinstanceId,stringdatabaseId,stringscheduleId)
{
DatabaseAdminClient client=DatabaseAdminClient .Create ();
BackupSchedule response=client.GetBackupSchedule (BackupScheduleName .FromProjectInstanceDatabaseSchedule (projectId,instanceId,databaseId,scheduleId));
Console.WriteLine($"Backup schedule: {response}");
returnresponse;
}
}C++
voidGetBackupSchedule(google::cloud::spanner_admin::DatabaseAdminClientclient,
std::stringconst&project_id,
std::stringconst&instance_id,
std::stringconst&database_id,
std::stringconst&backup_schedule_id){
google::cloud::spanner::Databasedb(project_id,instance_id,database_id);
std::stringbackup_schedule_name=
db.FullName()+"/backupSchedules/"+backup_schedule_id;
google::spanner::admin::database::v1::GetBackupScheduleRequestrequest;
request.set_name(backup_schedule_name);
autobackup_schedule=client.GetBackupSchedule(request);
if(!backup_schedule)throwstd::move(backup_schedule).status();
std::cout << "Retrieved backup schedule:\n" << backup_schedule->DebugString();
}Go
import(
"context"
"fmt"
"io"
database"cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
)
funcgetBackupSchedule(wio.Writer,dbNamestring,scheduleIdstring)error{
ctx:=context.Background()
client,err:=database.NewDatabaseAdminClient(ctx)
iferr!=nil{
returnerr
}
deferclient.Close ()
req:=databasepb.GetBackupScheduleRequest{
Name:fmt.Sprintf("%s/backupSchedules/%s",dbName,scheduleId),
}
res,err:=client.GetBackupSchedule(ctx,&req)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"Backup schedule: %s",res)
returnnil
}
Java
importcom.google.cloud.spanner.admin.database.v1.DatabaseAdminClient ;
importcom.google.spanner.admin.database.v1.BackupSchedule ;
importcom.google.spanner.admin.database.v1.BackupScheduleName ;
importcom.google.spanner.admin.database.v1.GetBackupScheduleRequest ;
importjava.io.IOException;
class GetBackupScheduleSample{
staticvoidgetBackupSchedule()throwsIOException{
// TODO(developer): Replace these variables before running the sample.
StringprojectId="my-project";
StringinstanceId="my-instance";
StringdatabaseId="my-database";
StringbackupScheduleId="my-backup-schedule";
getBackupSchedule(projectId,instanceId,databaseId,backupScheduleId);
}
staticvoidgetBackupSchedule(
StringprojectId,StringinstanceId,StringdatabaseId,StringbackupScheduleId)
throwsIOException{
try(DatabaseAdminClient databaseAdminClient=DatabaseAdminClient .create()){
BackupScheduleName backupScheduleName=
BackupScheduleName .of(projectId,instanceId,databaseId,backupScheduleId);
finalBackupSchedule backupSchedule=
databaseAdminClient.getBackupSchedule(
GetBackupScheduleRequest .newBuilder().setName(backupScheduleName.toString ()).build());
System.out.println(
String.format(
"Backup schedule: %s\n%s",backupSchedule.getName (),backupSchedule.toString()));
}
}
}Node.js
// Import the Google Cloud client library for Spanner.
const{Spanner}=require('@google-cloud/spanner');
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';
// const scheduleId = 'my-schedule-id';
// Create a Spanner database admin client.
constspanner=newSpanner ({projectId});
constclient=spanner.getDatabaseAdminClient ();
try{
// Get the backup schedule.
const[response]=awaitclient .getBackupSchedule({
name:client .backupSchedulePath(
projectId,
instanceId,
databaseId,
scheduleId,
),
});
console.log('Backup schedule:',response);
}catch(err){
console.error('ERROR:',err);
}PHP
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Database\V1\GetBackupScheduleRequest;
/**
* Get a backup schedule.
* Example:
* ```
* get_backup_schedule($projectId, $instanceId, $databaseId, $backupScheduleId);
* ```
*
* @param string $projectId The Google Cloud project ID.
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
* @param string $backupScheduleId The ID of the backup schedule to be created.
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
*/
function get_backup_schedule(
string $projectId,
string $instanceId,
string $databaseId,
string $backupScheduleId,
): void {
$databaseAdminClient = new DatabaseAdminClient();
$backupScheduleName = sprintf(
'projects/%s/instances/%s/databases/%s/backupSchedules/%s',
$projectId,
$instanceId,
$databaseId,
$backupScheduleId
);
$request = new GetBackupScheduleRequest([
'name' => $backupScheduleName,
]);
$backup_schedule = $databaseAdminClient->getBackupSchedule($request);
printf('Fetched backup scehedule %s' . PHP_EOL, $backup_schedule->getName());
}Python
defget_backup_schedule(
instance_id: str,
database_id: str,
schedule_id: str,
) -> None:
fromgoogle.cloudimport spanner
fromgoogle.cloud.spanner_admin_database_v1.typesimport (
backup_schedule as backup_schedule_pb,
)
client = spanner .Client ()
database_admin_api = client .database_admin_api
request = backup_schedule_pb.GetBackupScheduleRequest (
name=database_admin_api .backup_schedule_path(
client .project,
instance_id,
database_id,
schedule_id,
),
)
response = database_admin_api .get_backup_schedule(request)
print(f"Backup schedule: {response}")
List backup schedules
Console
- In the Google Cloud console, go to the Spanner Instances page.
Go to the Spanner Instances page
Click the instance containing the database.
Click the database.
In the navigation menu, click the Backup/Restore.
Click the Schedules tab to view all the backup schedules and their information.
gcloud
Before using any of the command data below, make the following replacements:
- PROJECT_ID: the project ID.
- INSTANCE_ID: the instance ID.
- DATABASE_ID: the database ID.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloudspannerbackup-scheduleslist\ --project=PROJECT_ID\ --instance=INSTANCE_ID\ --database=DATABASE_ID
Windows (PowerShell)
gcloudspannerbackup-scheduleslist` --project=PROJECT_ID` --instance=INSTANCE_ID` --database=DATABASE_ID
Windows (cmd.exe)
gcloudspannerbackup-scheduleslist^ --project=PROJECT_ID^ --instance=INSTANCE_ID^ --database=DATABASE_ID
You should receive a response similar to the following:
| Name | Backup type | Cron | Retention duration | Encryption type |
|---|---|---|---|---|
| my-full-schedule | FULL | 30 12 * * * | 2592000s | USE_DATABASE_ENCRYPTION |
| my-incr-schedule | INCREMENTAL | 0 */4 * * * | 2592000s | USE_DATABASE_ENCRYPTION |
REST v1
Before using any of the request data, make the following replacements:
- PROJECT_ID: the project ID.
- INSTANCE_ID: the instance ID.
- DATABASE_ID: the database ID.
HTTP method and URL:
GET https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules"
PowerShell (Windows)
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{
"backupSchedules": [
{
"name": "projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID",
"retentionDuration": "172800s",
"encryptionConfig": {
"encryptionType": "USE_DATABASE_ENCRYPTION"
},
"spec": {
"cronSpec": {
"text": "0 */12 * * *",
"timeZone": "UTC",
"creationWindow": "14400s"
}
},
"fullBackupSpec": {},
"updateTime": "2024-09-09T07:21:43.946180Z"
},
{
"name": "projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID",
"retentionDuration": "2592000s",
"encryptionConfig": {
"encryptionType": "CUSTOMER_MANAGED_ENCRYPTION",
"kmsKeyName": "projects/PROJECT_ID/locations/us-central1/keyRings/cmek-demo/cryptoKeys/test-key"
},
"spec": {
"cronSpec": {
"text": "30 12 * * *",
"timeZone": "UTC",
"creationWindow": "14400s"
}
},
"fullBackupSpec": {},
"updateTime": "2024-09-17T18:27:53.868741Z"
}
]
}
Client libraries
C#
usingGoogle.Cloud.Spanner.Admin.Database.V1 ;
usingGoogle.Cloud.Spanner.Common.V1 ;
usingSystem;
usingSystem.Collections.Generic;
publicclassListBackupSchedulesSample
{
publicIEnumerable<BackupSchedule>ListBackupSchedules(stringprojectId,stringinstanceId,stringdatabaseId)
{
DatabaseAdminClient client=DatabaseAdminClient .Create ();
varbackupSchedules=client.ListBackupSchedules (
newListBackupSchedulesRequest
{
ParentAsDatabaseName=DatabaseName .FromProjectInstanceDatabase (projectId,instanceId,databaseId),
});
foreach(BackupSchedule backupScheduleinbackupSchedules)
{
Console.WriteLine($"Backup schedule: {backupSchedule}");
}
returnbackupSchedules;
}
}C++
voidListBackupSchedules(
google::cloud::spanner_admin::DatabaseAdminClientclient,
std::stringconst&project_id,std::stringconst&instance_id,
std::stringconst&database_id){
google::cloud::spanner::Databasedb(project_id,instance_id,database_id);
google::spanner::admin::database::v1::ListBackupSchedulesRequestrequest;
request.set_parent(db.FullName());
intcount=0;
for(auto&backup_schedule:client.ListBackupSchedules(request)){
if(!backup_schedule)throwstd::move(backup_schedule).status();
std::cout << "Backup Schedule [" << ++count << "]:\n"
<< backup_schedule->DebugString();
}
if(count==0){
std::cout << "No backup schedules found for database " << db.FullName();
}
}Go
import(
"context"
"fmt"
"io"
database"cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"google.golang.org/api/iterator"
)
funclistBackupSchedules(wio.Writer,dbNamestring)error{
ctx:=context.Background()
client,err:=database.NewDatabaseAdminClient(ctx)
iferr!=nil{
returnerr
}
deferclient.Close ()
req:=databasepb.ListBackupSchedulesRequest{
Parent:dbName,
}
iter:=client.ListBackupSchedules(ctx,&req)
for{
backupSchedule,err:=iter.Next()
iferr==iterator.Done{
break
}
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"Backup schedule: %s\n",backupSchedule)
}
returnnil
}
Java
importcom.google.cloud.spanner.admin.database.v1.DatabaseAdminClient ;
importcom.google.spanner.admin.database.v1.BackupSchedule ;
importcom.google.spanner.admin.database.v1.DatabaseName ;
importjava.io.IOException;
class ListBackupSchedulesSample{
staticvoidlistBackupSchedules()throwsIOException{
// TODO(developer): Replace these variables before running the sample.
StringprojectId="my-project";
StringinstanceId="my-instance";
StringdatabaseId="my-database";
listBackupSchedules(projectId,instanceId,databaseId);
}
staticvoidlistBackupSchedules(StringprojectId,StringinstanceId,StringdatabaseId)
throwsIOException{
try(DatabaseAdminClient databaseAdminClient=DatabaseAdminClient .create()){
DatabaseName databaseName=DatabaseName .of(projectId,instanceId,databaseId);
System.out.println(
String.format("Backup schedules for database '%s'",databaseName.toString ()));
for(BackupSchedule backupSchedule:
databaseAdminClient.listBackupSchedules(databaseName).iterateAll()){
System.out.println(
String.format(
"Backup schedule: %s\n%s",backupSchedule.getName(),backupSchedule.toString()));
}
}
}
}Node.js
// Import the Google Cloud client library for Spanner.
const{Spanner}=require('@google-cloud/spanner');
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';
// Create a Spanner database admin client.
constspanner=newSpanner ({projectId});
constclient=spanner.getDatabaseAdminClient ();
try{
// List backup schedules of a database.
const[schedules]=awaitclient .listBackupSchedules({
parent:client .databasePath(projectId,instanceId,databaseId),
});
schedules.forEach(schedule=>{
console.log('Backup schedule:',schedule);
});
}catch(err){
console.error('ERROR:',err);
}PHP
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Database\V1\ListBackupSchedulesRequest;
/**
* Get list of all backup schedules for a given database.
* Example:
* ```
* list_backup_schedules($projectId, $instanceId, $databaseId, $backupScheduleId);
* ```
*
* @param string $projectId The Google Cloud project ID.
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
*/
function list_backup_schedules(
string $projectId,
string $instanceId,
string $databaseId,
): void {
$databaseAdminClient = new DatabaseAdminClient();
$databaseFullName = DatabaseAdminClient::databaseName($projectId, $instanceId, $databaseId);
$request = new ListBackupSchedulesRequest([
'parent' => $databaseFullName,
]);
$backup_schedules = $databaseAdminClient->listBackupSchedules($request);
printf('Backup schedules for database %s' . PHP_EOL, $databaseFullName);
foreach ($backup_schedules as $schedule) {
printf('Backup schedule: %s' . PHP_EOL, $schedule->getName());
}
}Python
deflist_backup_schedules(instance_id: str, database_id: str) -> None:
fromgoogle.cloudimport spanner
fromgoogle.cloud.spanner_admin_database_v1.typesimport (
backup_schedule as backup_schedule_pb,
)
client = spanner .Client ()
database_admin_api = client .database_admin_api
request = backup_schedule_pb.ListBackupSchedulesRequest (
parent=database_admin_api .database_path(
client .project,
instance_id,
database_id,
),
)
for backup_schedule in database_admin_api .list_backup_schedules(request):
print(f"Backup schedule: {backup_schedule}")
Update a backup schedule
Console
- In the Google Cloud console, go to the Spanner Instances page.
Go to the Spanner Instances page
Click the instance containing the database.
Click the database.
In the navigation menu, click the Backup/Restore.
Click the Schedules tab.
Select More actions for your backup schedule and click Update.
You can update the frequency and retention time of the backup schedule. If it's a full backup schedule, you can also update the encryption type.
Click Save.
gcloud
Before using any of the command data below, make the following replacements:
- SCHEDULE_ID: the backup schedule ID.
- PROJECT_ID: the project ID.
- INSTANCE_ID: the ID of the instance where you want to modify the backup schedule.
- DATABASE_ID: the ID of the database where you want to modify the backup schedule.
- RETENTION_DURATION: the retention duration of the backups created by the schedule.
-
CRONTAB_EXPRESSION:
the crontab expression for the backup schedule frequency. For example, if you want the
backup schedule frequency to be every 12 hours, you can use
0 12 * * *. -
ENCRYPTION_TYPE:
the encryption type of backups created by the backup schedule.
Valid values are
USE_DATABASE_ENCRYPTION,GOOGLE_DEFAULT_ENCRYPTION, orCUSTOMER_MANAGED_ENCRYPTION. If you useCUSTOMER_MANAGED_ENCRYPTION, you must specify akmsKeyName. If your backup type isincremental-backup, the encryption type must beGOOGLE_DEFAULT_ENCRYPTION.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloudspannerbackup-schedulesupdateSCHEDULE_ID\ --project=PROJECT_ID\ --instance=INSTANCE_ID\ --database=DATABASE_ID\ --retention-duration=RETENTION_DURATION\ --cron="CRONTAB_EXPRESSION"\ --encryption-type=ENCRYPTION_TYPE
Windows (PowerShell)
gcloudspannerbackup-schedulesupdateSCHEDULE_ID` --project=PROJECT_ID` --instance=INSTANCE_ID` --database=DATABASE_ID` --retention-duration=RETENTION_DURATION` --cron="CRONTAB_EXPRESSION"` --encryption-type=ENCRYPTION_TYPE
Windows (cmd.exe)
gcloudspannerbackup-schedulesupdateSCHEDULE_ID^ --project=PROJECT_ID^ --instance=INSTANCE_ID^ --database=DATABASE_ID^ --retention-duration=RETENTION_DURATION^ --cron="CRONTAB_EXPRESSION"^ --encryption-type=ENCRYPTION_TYPE
REST v1
Before using any of the request data, make the following replacements:
- PROJECT_ID: the project ID.
- INSTANCE_ID: the ID of the instance where you want to modify the backup schedule.
- DATABASE_ID: the ID of the database where you want to modify the backup schedule.
- SCHEDULE_ID: the backup schedule ID.
-
RETENTION_DURATION: the retention duration of
the backups created by the schedule. For example, if you want the retention duration to be one day,
specify
86400s. -
ENCRYPTION_TYPE:
the encryption type of backups created by the backup schedule.
Possible values are
USE_DATABASE_ENCRYPTION,GOOGLE_DEFAULT_ENCRYPTION, orCUSTOMER_MANAGED_ENCRYPTION. If you useCUSTOMER_MANAGED_ENCRYPTION, you must specify akmsKeyName. If your backup type isincremental-backup, the encryption type must beGOOGLE_DEFAULT_ENCRYPTION.
HTTP method and URL:
PATCH https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID?updateMask=retention_duration,encryption_config,spec.cron_spec.text
Request JSON body:
{
"retentionDuration": "RETENTION_DURATION",
"spec": {
"cronSpec": {
"text": "0 2 * * *"
}
"encryptionConfig": {
"encryptionType": "ENCRYPTION_TYPE"
},
},
}
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Save the request body in a file named request.json,
and execute the following command:
curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID?updateMask=retention_duration,encryption_config,spec.cron_spec.text"
PowerShell (Windows)
Save the request body in a file named request.json,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID?updateMask=retention_duration,encryption_config,spec.cron_spec.text" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{
"name": "projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID",
"retentionDuration": "86400s",
"encryptionConfig": {
"encryptionType": "USE_DATABASE_ENCRYPTION"
},
"spec": {
"cronSpec": {
"text": "0 2 * * *",
"timeZone": "UTC",
"creationWindow": "14400s"
}
},
"fullBackupSpec": {},
"updateTime": "2024-05-22T11:13:51.835590Z"
}
Client libraries
C#
usingGoogle.Cloud.Spanner.Admin.Database.V1 ;
usingGoogle.Protobuf.WellKnownTypes ;
usingSystem;
publicclassUpdateBackupScheduleSample
{
publicBackupScheduleUpdateBackupSchedule(stringprojectId,stringinstanceId,stringdatabaseId,stringscheduleId)
{
DatabaseAdminClient client=DatabaseAdminClient .Create ();
BackupSchedule response=client.UpdateBackupSchedule (
newUpdateBackupScheduleRequest
{
BackupSchedule=newBackupSchedule
{
BackupScheduleName=BackupScheduleName .FromProjectInstanceDatabaseSchedule (projectId,instanceId,databaseId,scheduleId),
Spec=newBackupScheduleSpec
{
CronSpec=newCrontabSpec
{
Text="45 15 * * *",
},
},
RetentionDuration=newDuration
{
Seconds=172800,
},
EncryptionConfig=newCreateBackupEncryptionConfig
{
EncryptionType=CreateBackupEncryptionConfig .Types .EncryptionType .UseDatabaseEncryption ,
},
},
UpdateMask=newFieldMask
{
Paths={"spec.cron_spec.text","retention_duration","encryption_config"},
},
});
Console.WriteLine($"Updated backup schedule: {response}");
returnresponse;
}
}C++
voidUpdateBackupSchedule(
google::cloud::spanner_admin::DatabaseAdminClientclient,
std::stringconst&project_id,std::stringconst&instance_id,
std::stringconst&database_id,std::stringconst&backup_schedule_id){
google::cloud::spanner::Databasedb(project_id,instance_id,database_id);
std::stringbackup_schedule_name=
db.FullName()+"/backupSchedules/"+backup_schedule_id;
google::spanner::admin::database::v1::BackupSchedulebackup_schedule;
backup_schedule.set_name(backup_schedule_name);
backup_schedule.mutable_spec()->mutable_cron_spec()->set_text("45 15 * * *");
backup_schedule.mutable_retention_duration()->set_seconds(3600*48);
google::spanner::admin::database::v1::CreateBackupEncryptionConfig
encryption_config;
encryption_config.set_encryption_type(
google::spanner::admin::database::v1::CreateBackupEncryptionConfig::
USE_DATABASE_ENCRYPTION);
*backup_schedule.mutable_encryption_config()=std::move(encryption_config);
google::protobuf::FieldMaskupdate_mask;
update_mask.add_paths("spec.cron_spec.text");
update_mask.add_paths("retention_duration");
update_mask.add_paths("encryption_config");
google::spanner::admin::database::v1::UpdateBackupScheduleRequestrequest;
*request.mutable_backup_schedule()=std::move(backup_schedule);
*request.mutable_update_mask()=std::move(update_mask);
autoupdated_backup_schedule=client.UpdateBackupSchedule(request);
if(!updated_backup_schedule){
throwstd::move(updated_backup_schedule).status();
}
std::cout << "Backup schedule " << updated_backup_schedule->name()
<< " updated at "
<< *google::cloud::spanner::MakeTimestamp(
updated_backup_schedule->update_time());
}Go
import(
"context"
"fmt"
"io"
"time"
database"cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
funcupdateBackupSchedule(wio.Writer,dbNamestring,scheduleIdstring)error{
ctx:=context.Background()
client,err:=database.NewDatabaseAdminClient(ctx)
iferr!=nil{
returnerr
}
deferclient.Close ()
// Update a schedule to create backups daily at 3:45 PM, using the database's
// encryption config, and retained for 48 hours.
req:=databasepb.UpdateBackupScheduleRequest{
BackupSchedule:&databasepb.BackupSchedule{
Name:fmt.Sprintf("%s/backupSchedules/%s",dbName,scheduleId),
Spec:&databasepb.BackupScheduleSpec{
ScheduleSpec:&databasepb.BackupScheduleSpec_CronSpec{
CronSpec:&databasepb.CrontabSpec{
Text:"45 15 * * *",
},
},
},
RetentionDuration:durationpb.New(48*time.Hour),
EncryptionConfig:&databasepb.CreateBackupEncryptionConfig{
EncryptionType:databasepb.CreateBackupEncryptionConfig_USE_DATABASE_ENCRYPTION ,
},
},
UpdateMask:&fieldmaskpb.FieldMask{
Paths:[]string{
"spec.cron_spec.text",
"retention_duration",
"encryption_config",
},
},
}
res,err:=client.UpdateBackupSchedule(ctx,&req)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"Updated backup schedule: %s",res)
returnnil
}
Java
importcom.google.cloud.spanner.admin.database.v1.DatabaseAdminClient ;
importcom.google.protobuf.Duration ;
importcom.google.protobuf.FieldMask ;
importcom.google.spanner.admin.database.v1.BackupSchedule ;
importcom.google.spanner.admin.database.v1.BackupScheduleName ;
importcom.google.spanner.admin.database.v1.BackupScheduleSpec ;
importcom.google.spanner.admin.database.v1.CreateBackupEncryptionConfig ;
importcom.google.spanner.admin.database.v1.CrontabSpec ;
importcom.google.spanner.admin.database.v1.UpdateBackupScheduleRequest ;
importjava.io.IOException;
class UpdateBackupScheduleSample{
staticvoidupdateBackupSchedule()throwsIOException{
// TODO(developer): Replace these variables before running the sample.
StringprojectId="my-project";
StringinstanceId="my-instance";
StringdatabaseId="my-database";
StringbackupScheduleId="my-backup-schedule";
updateBackupSchedule(projectId,instanceId,databaseId,backupScheduleId);
}
staticvoidupdateBackupSchedule(
StringprojectId,StringinstanceId,StringdatabaseId,StringbackupScheduleId)
throwsIOException{
BackupScheduleName backupScheduleName=
BackupScheduleName .of(projectId,instanceId,databaseId,backupScheduleId);
finalCreateBackupEncryptionConfig encryptionConfig=
CreateBackupEncryptionConfig .newBuilder()
.setEncryptionType(CreateBackupEncryptionConfig .EncryptionType.USE_DATABASE_ENCRYPTION)
.build();
finalBackupSchedule backupSchedule=
BackupSchedule .newBuilder()
.setName(backupScheduleName.toString ())
.setRetentionDuration (Duration .newBuilder().setSeconds(3600*48))
.setSpec (
BackupScheduleSpec .newBuilder()
.setCronSpec (CrontabSpec .newBuilder().setText ("45 15 * * *").build())
.build())
.setEncryptionConfig(encryptionConfig)
.build();
try(DatabaseAdminClient databaseAdminClient=DatabaseAdminClient .create()){
finalFieldMask fieldMask=
FieldMask .newBuilder()
.addPaths ("retention_duration")
.addPaths ("spec.cron_spec.text")
.addPaths ("encryption_config")
.build();
finalBackupSchedule updatedBackupSchedule=
databaseAdminClient.updateBackupSchedule(
UpdateBackupScheduleRequest .newBuilder()
.setBackupSchedule(backupSchedule)
.setUpdateMask(fieldMask)
.build());
System.out.println(
String.format(
"Updated backup schedule: %s\n%s",
updatedBackupSchedule.getName (),updatedBackupSchedule.toString()));
}
}
}Node.js
// Import the Google Cloud client library for Spanner.
const{Spanner}=require('@google-cloud/spanner');
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';
// const scheduleId = 'my-schedule-id';
// Create a Spanner database admin client.
constspanner=newSpanner ({projectId});
constclient=spanner.getDatabaseAdminClient ();
try{
// Update the schedule to create backups daily at 3:45 PM, using the
// database's encryption config, and retained for 48 hours.
const[response]=awaitclient .updateBackupSchedule({
backupSchedule:{
name:client .backupSchedulePath(
projectId,
instanceId,
databaseId,
scheduleId,
),
spec:{
cronSpec:{
text:'45 15 * * *',
},
},
retentionDuration:{
seconds:172800,
},
encryptionConfig:{
encryptionType:'USE_DATABASE_ENCRYPTION',
},
},
updateMask:{
paths:[
'spec.cron_spec.text',
'retention_duration',
'encryption_config',
],
},
});
console.log('Updated backup schedule:',response);
}catch(err){
console.error('ERROR:',err);
}PHP
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Database\V1\UpdateBackupScheduleRequest;
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig;
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig\EncryptionType;
use Google\Cloud\Spanner\Admin\Database\V1\BackupSchedule;
use Google\Cloud\Spanner\Admin\Database\V1\FullBackupSpec;
use Google\Cloud\Spanner\Admin\Database\V1\BackupScheduleSpec;
use Google\Cloud\Spanner\Admin\Database\V1\CrontabSpec;
use Google\Protobuf\Duration;
use Google\Protobuf\FieldMask;
/**
* Update an existing backup schedule.
* Example:
* ```
* update_backup_schedule($projectId, $instanceId, $databaseId, $backupScheduleId);
* ```
*
* @param string $projectId The Google Cloud project ID.
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
* @param string $backupScheduleId The ID of the backup schedule to be created.
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
*/
function update_backup_schedule(
string $projectId,
string $instanceId,
string $databaseId,
string $backupScheduleId,
): void {
$databaseAdminClient = new DatabaseAdminClient();
$encryptionConfig = new CreateBackupEncryptionConfig([
'encryption_type' => EncryptionType::USE_DATABASE_ENCRYPTION,
]);
$backupScheduleName = sprintf(
'projects/%s/instances/%s/databases/%s/backupSchedules/%s',
$projectId,
$instanceId,
$databaseId,
$backupScheduleId
);
$backupSchedule = new BackupSchedule([
'name' => $backupScheduleName,
'full_backup_spec' => new FullBackupSpec(),
'retention_duration' => (new Duration())
->setSeconds(48 * 60 * 60),
'spec' => new BackupScheduleSpec([
'cron_spec' => new CrontabSpec([
'text' => '45 15 * * *'
]),
]),
'encryption_config' => $encryptionConfig,
]);
$fieldMask = (new FieldMask())
->setPaths([
'retention_duration',
'spec.cron_spec.text',
'encryption_config',
]);
$request = new UpdateBackupScheduleRequest([
'backup_schedule' => $backupSchedule,
'update_mask' => $fieldMask,
]);
$updated_backup_schedule = $databaseAdminClient->updateBackupSchedule($request);
printf('Updated backup scehedule %s' . PHP_EOL, $updated_backup_schedule->getName());
}Python
defupdate_backup_schedule(
instance_id: str,
database_id: str,
schedule_id: str,
) -> None:
fromdatetimeimport timedelta
fromgoogle.cloudimport spanner
fromgoogle.cloud.spanner_admin_database_v1.typesimport (
backup_schedule as backup_schedule_pb,
)
fromgoogle.cloud.spanner_admin_database_v1.typesimport (
CreateBackupEncryptionConfig ,
)
fromgoogle.protobuf.field_mask_pb2import FieldMask
client = spanner .Client ()
database_admin_api = client .database_admin_api
request = backup_schedule_pb.UpdateBackupScheduleRequest (
backup_schedule=backup_schedule_pb.BackupSchedule (
name=database_admin_api .backup_schedule_path(
client .project,
instance_id,
database_id,
schedule_id,
),
spec=backup_schedule_pb.BackupScheduleSpec (
cron_spec=backup_schedule_pb.CrontabSpec (
text="45 15 * * *",
),
),
retention_duration=timedelta(hours=48),
encryption_config=CreateBackupEncryptionConfig(
encryption_type=CreateBackupEncryptionConfig .EncryptionType.USE_DATABASE_ENCRYPTION,
),
),
update_mask=FieldMask(
paths=[
"spec.cron_spec.text",
"retention_duration",
"encryption_config",
],
),
)
response = database_admin_api .update_backup_schedule(request)
print(f"Updated backup schedule: {response}")
Delete a backup schedule
Console
- In the Google Cloud console, go to the Spanner Instances page.
Go to the Spanner Instances page
Click the instance containing the database.
Click the database.
In the navigation menu, click the Backup/Restore.
Select More actions for your backup schedule and click Delete.
Type the backup schedule name.
Click Confirm.
gcloud
Before using any of the command data below, make the following replacements:
- SCHEDULE_ID: the backup schedule ID.
- PROJECT_ID: the project ID.
- INSTANCE_ID: the ID of the instance where you want to delete the backup schedule.
- DATABASE_ID: the ID of the database where you want to delete the backup schedule.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloudspannerbackup-schedulesdeleteSCHEDULE_ID\ --project=PROJECT_ID\ --instance=INSTANCE_ID\ --database=DATABASE_ID
Windows (PowerShell)
gcloudspannerbackup-schedulesdeleteSCHEDULE_ID` --project=PROJECT_ID` --instance=INSTANCE_ID` --database=DATABASE_ID
Windows (cmd.exe)
gcloudspannerbackup-schedulesdeleteSCHEDULE_ID^ --project=PROJECT_ID^ --instance=INSTANCE_ID^ --database=DATABASE_ID
REST v1
Before using any of the request data, make the following replacements:
- PROJECT_ID: the project ID.
- INSTANCE_ID: the ID of the instance where you want to delete the backup schedule.
- DATABASE_ID: the ID of the database where you want to delete the backup schedule.
- SCHEDULE_ID: the backup schedule ID.
HTTP method and URL:
DELETE https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Execute the following command:
curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID"
PowerShell (Windows)
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID" | Select-Object -Expand Content
You should receive a successful status code (2xx) and an empty response.
Client libraries
C#
usingGoogle.Cloud.Spanner.Admin.Database.V1 ;
usingSystem;
publicclassDeleteBackupScheduleSample
{
publicvoidDeleteBackupSchedule(stringprojectId,stringinstanceId,stringdatabaseId,stringscheduleId)
{
DatabaseAdminClient client=DatabaseAdminClient .Create ();
client.DeleteBackupSchedule (BackupScheduleName .FromProjectInstanceDatabaseSchedule (projectId,instanceId,databaseId,scheduleId));
Console.WriteLine("Deleted backup schedule");
}
}C++
voidDeleteBackupSchedule(
google::cloud::spanner_admin::DatabaseAdminClientclient,
std::stringconst&project_id,std::stringconst&instance_id,
std::stringconst&database_id,std::stringconst&backup_schedule_id){
google::cloud::spanner::Databasedb(project_id,instance_id,database_id);
std::stringbackup_schedule_name=
db.FullName()+"/backupSchedules/"+backup_schedule_id;
google::spanner::admin::database::v1::DeleteBackupScheduleRequestrequest;
request.set_name(backup_schedule_name);
autostatus=client.DeleteBackupSchedule(request);
if(!status.ok())throwstd::move(status);
std::cout << "Backup schedule " << backup_schedule_name << " deleted";
}Go
import(
"context"
"fmt"
"io"
database"cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
)
funcdeleteBackupSchedule(wio.Writer,dbNamestring,scheduleIdstring)error{
ctx:=context.Background()
client,err:=database.NewDatabaseAdminClient(ctx)
iferr!=nil{
returnerr
}
deferclient.Close ()
req:=databasepb.DeleteBackupScheduleRequest{
Name:fmt.Sprintf("%s/backupSchedules/%s",dbName,scheduleId),
}
err=client.DeleteBackupSchedule(ctx,&req)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"Deleted backup schedule")
returnnil
}
Java
importcom.google.cloud.spanner.admin.database.v1.DatabaseAdminClient ;
importcom.google.spanner.admin.database.v1.BackupScheduleName ;
importcom.google.spanner.admin.database.v1.DeleteBackupScheduleRequest ;
importjava.io.IOException;
class DeleteBackupScheduleSample{
staticvoiddeleteBackupSchedule()throwsIOException{
// TODO(developer): Replace these variables before running the sample.
StringprojectId="my-project";
StringinstanceId="my-instance";
StringdatabaseId="my-database";
StringbackupScheduleId="my-backup-schedule";
deleteBackupSchedule(projectId,instanceId,databaseId,backupScheduleId);
}
staticvoiddeleteBackupSchedule(
StringprojectId,StringinstanceId,StringdatabaseId,StringbackupScheduleId)
throwsIOException{
try(DatabaseAdminClient databaseAdminClient=DatabaseAdminClient .create()){
BackupScheduleName backupScheduleName=
BackupScheduleName .of(projectId,instanceId,databaseId,backupScheduleId);
databaseAdminClient.deleteBackupSchedule(
DeleteBackupScheduleRequest .newBuilder().setName(backupScheduleName.toString ()).build());
System.out.println(
String.format("Deleted backup schedule: %s",backupScheduleName.toString ()));
}
}
}Node.js
// Import the Google Cloud client library for Spanner.
const{Spanner}=require('@google-cloud/spanner');
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';
// const scheduleId = 'my-schedule-id';
// Create a Spanner database admin client.
constspanner=newSpanner ({projectId});
constclient=spanner.getDatabaseAdminClient ();
try{
// Delete the backup schedule.
awaitclient .deleteBackupSchedule({
name:client .backupSchedulePath(
projectId,
instanceId,
databaseId,
scheduleId,
),
});
console.log('Deleted backup schedule');
}catch(err){
console.error('ERROR:',err);
}PHP
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Database\V1\DeleteBackupScheduleRequest;
/**
* Delete a backup schedule.
* Example:
* ```
* delete_backup_schedule($projectId, $instanceId, $databaseId, $backupScheduleId);
* ```
*
* @param string $projectId The Google Cloud project ID.
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
* @param string $backupScheduleId The ID of the backup schedule to be created.
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
*/
function delete_backup_schedule(
string $projectId,
string $instanceId,
string $databaseId,
string $backupScheduleId,
): void {
$databaseAdminClient = new DatabaseAdminClient();
$backupScheduleName = sprintf(
'projects/%s/instances/%s/databases/%s/backupSchedules/%s',
$projectId,
$instanceId,
$databaseId,
$backupScheduleId
);
$request = new DeleteBackupScheduleRequest([
'name' => strval($backupScheduleName),
]);
$databaseAdminClient->deleteBackupSchedule($request);
printf('Deleted backup scehedule %s' . PHP_EOL, $backupScheduleName);
}Python
defdelete_backup_schedule(
instance_id: str,
database_id: str,
schedule_id: str,
) -> None:
fromgoogle.cloudimport spanner
fromgoogle.cloud.spanner_admin_database_v1.typesimport (
backup_schedule as backup_schedule_pb,
)
client = spanner .Client ()
database_admin_api = client .database_admin_api
request = backup_schedule_pb.DeleteBackupScheduleRequest (
name=database_admin_api .backup_schedule_path(
client .project,
instance_id,
database_id,
schedule_id,
),
)
database_admin_api .delete_backup_schedule(request)
print("Deleted backup schedule")
Set IAM access control policy
You can set an IAM access policy to specify who has access to the backup schedule.
gcloud
Before using any of the command data below, make the following replacements:
- SCHEDULE_ID: the backup schedule ID.
- PROJECT_ID: the project ID.
- INSTANCE_ID: the instance ID.
- DATABASE_ID: the database ID.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloudspannerbackup-schedulesset-iam-policySCHEDULE_ID\ --project=PROJECT_ID\ --instance=INSTANCE_ID\ --database=DATABASE_ID\ policy.json
Windows (PowerShell)
gcloudspannerbackup-schedulesset-iam-policySCHEDULE_ID` --project=PROJECT_ID` --instance=INSTANCE_ID` --database=DATABASE_ID` policy.json
Windows (cmd.exe)
gcloudspannerbackup-schedulesset-iam-policySCHEDULE_ID^ --project=PROJECT_ID^ --instance=INSTANCE_ID^ --database=DATABASE_ID^ policy.json
You should receive a response similar to the following:
Updated IAM policy for backupSchedule [SCHEDULE_ID]. bindings: - members: - user:test@google.com role: roles/editor etag: BwYi82k-fho= version: 1
policy.json file that you can use along with this gcloud CLI
command:
{
"version": 1,
"etag": "BwYi8ypICC0=",
"bindings": [
{
"role": "roles/editor",
"members": [
"user:test@gmail.com"
]
}
]
}
REST v1
Before using any of the request data, make the following replacements:
- PROJECT_ID: the project ID.
- INSTANCE_ID: the instance ID.
- DATABASE_ID: the database ID.
- SCHEDULE_ID: the backup schedule ID.
HTTP method and URL:
POST https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID:setIamPolicy?alt=json
Request JSON body:
{
"policy": {
"version": 1,
"etag": "BwYi8ypICC0=",
"bindings": [
{
"role": "roles/editor",
"members": [
"user:test@gmail.com"
]
}
]
}
}
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Save the request body in a file named request.json,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID:setIamPolicy?alt=json"
PowerShell (Windows)
Save the request body in a file named request.json,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID:setIamPolicy?alt=json" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{
{
"version": 1,
"etag": "etag",
"bindings": [
{
"role": "roles/non-primitive",
"members": [
"user:test@gmail.com"
]
}
]
}
}
Get IAM access control policy
gcloud
Before using any of the command data below, make the following replacements:
- SCHEDULE_ID: the backup schedule ID.
- PROJECT_ID: the project ID.
- INSTANCE_ID: the instance ID.
- DATABASE_ID: the database ID.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloudspannerbackup-schedulesget-iam-policySCHEDULE_ID\ --project=PROJECT_ID\ --instance=INSTANCE_ID\ --database=DATABASE_ID
Windows (PowerShell)
gcloudspannerbackup-schedulesget-iam-policySCHEDULE_ID` --project=PROJECT_ID` --instance=INSTANCE_ID` --database=DATABASE_ID
Windows (cmd.exe)
gcloudspannerbackup-schedulesget-iam-policySCHEDULE_ID^ --project=PROJECT_ID^ --instance=INSTANCE_ID^ --database=DATABASE_ID
You should receive a response similar to the following:
bindings: - members: - user:test@gmail.com role: roles/editor etag: BwYi82k-fho= version: 1
REST v1
Before using any of the request data, make the following replacements:
- INSTANCE_ID: the instance ID.
- DATABASE_ID: the database ID.
- SCHEDULE_ID: the backup schedule ID.
- PROJECT_ID: the project ID.
HTTP method and URL:
POST https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID:getIamPolicy?alt=json
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID:getIamPolicy?alt=json"
PowerShell (Windows)
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "https://spanner.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID/backupSchedules/SCHEDULE_ID:getIamPolicy?alt=json" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{
{
"version": 1,
"etag": "BwYbyZ9pc4o=",
"bindings": [
{
"role": "roles/editor",
"members": [
"user:test@gmail.com"
]
}
]
}
}