-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add samples for backup schedule feature APIs. (#3339)
* Add java sample for `CreateBackupSchedule` API. * Add samples for backups schedule APIs. * Fix formatting issues. * Fix `GetBackupSchedule` and `ListBackupSchedules` method names. * Fix tags for backup schedule samples. * Refactored samples' console logs.
- Loading branch information
Showing
12 changed files
with
710 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_create_full_backup_schedule] | ||
|
||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.protobuf.Duration; | ||
import com.google.spanner.admin.database.v1.BackupSchedule; | ||
import com.google.spanner.admin.database.v1.BackupScheduleSpec; | ||
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; | ||
import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; | ||
import com.google.spanner.admin.database.v1.CrontabSpec; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import com.google.spanner.admin.database.v1.FullBackupSpec; | ||
import java.io.IOException; | ||
|
||
class CreateFullBackupScheduleSample { | ||
|
||
static void createFullBackupSchedule() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
String backupScheduleId = "my-backup-schedule"; | ||
createFullBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); | ||
} | ||
|
||
static void createFullBackupSchedule( | ||
String projectId, String instanceId, String databaseId, String backupScheduleId) | ||
throws IOException { | ||
final CreateBackupEncryptionConfig encryptionConfig = | ||
CreateBackupEncryptionConfig.newBuilder() | ||
.setEncryptionType(CreateBackupEncryptionConfig.EncryptionType.USE_DATABASE_ENCRYPTION) | ||
.build(); | ||
final BackupSchedule 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); | ||
final BackupSchedule 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())); | ||
} | ||
} | ||
} | ||
// [END spanner_create_full_backup_schedule] |
78 changes: 78 additions & 0 deletions
78
...les/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_create_incremental_backup_schedule] | ||
|
||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.protobuf.Duration; | ||
import com.google.spanner.admin.database.v1.BackupSchedule; | ||
import com.google.spanner.admin.database.v1.BackupScheduleSpec; | ||
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; | ||
import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; | ||
import com.google.spanner.admin.database.v1.CrontabSpec; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import com.google.spanner.admin.database.v1.IncrementalBackupSpec; | ||
import java.io.IOException; | ||
|
||
class CreateIncrementalBackupScheduleSample { | ||
|
||
static void createIncrementalBackupSchedule() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
String backupScheduleId = "my-backup-schedule"; | ||
createIncrementalBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); | ||
} | ||
|
||
static void createIncrementalBackupSchedule( | ||
String projectId, String instanceId, String databaseId, String backupScheduleId) | ||
throws IOException { | ||
final CreateBackupEncryptionConfig encryptionConfig = | ||
CreateBackupEncryptionConfig.newBuilder() | ||
.setEncryptionType( | ||
CreateBackupEncryptionConfig.EncryptionType.GOOGLE_DEFAULT_ENCRYPTION) | ||
.build(); | ||
final BackupSchedule 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); | ||
final BackupSchedule 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())); | ||
} | ||
} | ||
} | ||
// [END spanner_create_incremental_backup_schedule] |
50 changes: 50 additions & 0 deletions
50
samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_delete_backup_schedule] | ||
|
||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.spanner.admin.database.v1.BackupScheduleName; | ||
import com.google.spanner.admin.database.v1.DeleteBackupScheduleRequest; | ||
import java.io.IOException; | ||
|
||
class DeleteBackupScheduleSample { | ||
|
||
static void deleteBackupSchedule() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
String backupScheduleId = "my-backup-schedule"; | ||
deleteBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); | ||
} | ||
|
||
static void deleteBackupSchedule( | ||
String projectId, String instanceId, String databaseId, String backupScheduleId) | ||
throws IOException { | ||
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())); | ||
} | ||
} | ||
} | ||
// [END spanner_delete_backup_schedule] |
53 changes: 53 additions & 0 deletions
53
samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_get_backup_schedule] | ||
|
||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.spanner.admin.database.v1.BackupSchedule; | ||
import com.google.spanner.admin.database.v1.BackupScheduleName; | ||
import com.google.spanner.admin.database.v1.GetBackupScheduleRequest; | ||
import java.io.IOException; | ||
|
||
class GetBackupScheduleSample { | ||
|
||
static void getBackupSchedule() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
String backupScheduleId = "my-backup-schedule"; | ||
getBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); | ||
} | ||
|
||
static void getBackupSchedule( | ||
String projectId, String instanceId, String databaseId, String backupScheduleId) | ||
throws IOException { | ||
try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { | ||
BackupScheduleName backupScheduleName = | ||
BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); | ||
final BackupSchedule backupSchedule = | ||
databaseAdminClient.getBackupSchedule( | ||
GetBackupScheduleRequest.newBuilder().setName(backupScheduleName.toString()).build()); | ||
System.out.println( | ||
String.format( | ||
"Backup schedule: %s\n%s", backupSchedule.getName(), backupSchedule.toString())); | ||
} | ||
} | ||
} | ||
// [END spanner_get_backup_schedule] |
52 changes: 52 additions & 0 deletions
52
samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_list_backup_schedules] | ||
|
||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.spanner.admin.database.v1.BackupSchedule; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import java.io.IOException; | ||
|
||
class ListBackupSchedulesSample { | ||
|
||
static void listBackupSchedules() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
listBackupSchedules(projectId, instanceId, databaseId); | ||
} | ||
|
||
static void listBackupSchedules(String projectId, String instanceId, String databaseId) | ||
throws IOException { | ||
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())); | ||
} | ||
} | ||
} | ||
} | ||
// [END spanner_list_backup_schedules] |
83 changes: 83 additions & 0 deletions
83
samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_update_backup_schedule] | ||
|
||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.protobuf.Duration; | ||
import com.google.protobuf.FieldMask; | ||
import com.google.spanner.admin.database.v1.BackupSchedule; | ||
import com.google.spanner.admin.database.v1.BackupScheduleName; | ||
import com.google.spanner.admin.database.v1.BackupScheduleSpec; | ||
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; | ||
import com.google.spanner.admin.database.v1.CrontabSpec; | ||
import com.google.spanner.admin.database.v1.UpdateBackupScheduleRequest; | ||
import java.io.IOException; | ||
|
||
class UpdateBackupScheduleSample { | ||
|
||
static void updateBackupSchedule() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
String backupScheduleId = "my-backup-schedule"; | ||
updateBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); | ||
} | ||
|
||
static void updateBackupSchedule( | ||
String projectId, String instanceId, String databaseId, String backupScheduleId) | ||
throws IOException { | ||
BackupScheduleName backupScheduleName = | ||
BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); | ||
final CreateBackupEncryptionConfig encryptionConfig = | ||
CreateBackupEncryptionConfig.newBuilder() | ||
.setEncryptionType(CreateBackupEncryptionConfig.EncryptionType.USE_DATABASE_ENCRYPTION) | ||
.build(); | ||
final BackupSchedule 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()) { | ||
final FieldMask fieldMask = | ||
FieldMask.newBuilder() | ||
.addPaths("retention_duration") | ||
.addPaths("spec.cron_spec.text") | ||
.addPaths("encryption_config") | ||
.build(); | ||
final BackupSchedule 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())); | ||
} | ||
} | ||
} | ||
// [END spanner_update_backup_schedule] |
Oops, something went wrong.