Skip to content

Commit 8cd5163

Browse files
authored
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.
1 parent 254b2f5 commit 8cd5163

12 files changed

+710
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_create_full_backup_schedule]
20+
21+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
22+
import com.google.protobuf.Duration;
23+
import com.google.spanner.admin.database.v1.BackupSchedule;
24+
import com.google.spanner.admin.database.v1.BackupScheduleSpec;
25+
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig;
26+
import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest;
27+
import com.google.spanner.admin.database.v1.CrontabSpec;
28+
import com.google.spanner.admin.database.v1.DatabaseName;
29+
import com.google.spanner.admin.database.v1.FullBackupSpec;
30+
import java.io.IOException;
31+
32+
class CreateFullBackupScheduleSample {
33+
34+
static void createFullBackupSchedule() throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "my-project";
37+
String instanceId = "my-instance";
38+
String databaseId = "my-database";
39+
String backupScheduleId = "my-backup-schedule";
40+
createFullBackupSchedule(projectId, instanceId, databaseId, backupScheduleId);
41+
}
42+
43+
static void createFullBackupSchedule(
44+
String projectId, String instanceId, String databaseId, String backupScheduleId)
45+
throws IOException {
46+
final CreateBackupEncryptionConfig encryptionConfig =
47+
CreateBackupEncryptionConfig.newBuilder()
48+
.setEncryptionType(CreateBackupEncryptionConfig.EncryptionType.USE_DATABASE_ENCRYPTION)
49+
.build();
50+
final BackupSchedule backupSchedule =
51+
BackupSchedule.newBuilder()
52+
.setFullBackupSpec(FullBackupSpec.newBuilder().build())
53+
.setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24).build())
54+
.setSpec(
55+
BackupScheduleSpec.newBuilder()
56+
.setCronSpec(CrontabSpec.newBuilder().setText("30 12 * * *").build())
57+
.build())
58+
.setEncryptionConfig(encryptionConfig)
59+
.build();
60+
61+
try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
62+
DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId);
63+
final BackupSchedule createdBackupSchedule =
64+
databaseAdminClient.createBackupSchedule(
65+
CreateBackupScheduleRequest.newBuilder()
66+
.setParent(databaseName.toString())
67+
.setBackupScheduleId(backupScheduleId)
68+
.setBackupSchedule(backupSchedule)
69+
.build());
70+
System.out.println(
71+
String.format(
72+
"Created backup schedule: %s\n%s",
73+
createdBackupSchedule.getName(), createdBackupSchedule.toString()));
74+
}
75+
}
76+
}
77+
// [END spanner_create_full_backup_schedule]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_create_incremental_backup_schedule]
20+
21+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
22+
import com.google.protobuf.Duration;
23+
import com.google.spanner.admin.database.v1.BackupSchedule;
24+
import com.google.spanner.admin.database.v1.BackupScheduleSpec;
25+
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig;
26+
import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest;
27+
import com.google.spanner.admin.database.v1.CrontabSpec;
28+
import com.google.spanner.admin.database.v1.DatabaseName;
29+
import com.google.spanner.admin.database.v1.IncrementalBackupSpec;
30+
import java.io.IOException;
31+
32+
class CreateIncrementalBackupScheduleSample {
33+
34+
static void createIncrementalBackupSchedule() throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "my-project";
37+
String instanceId = "my-instance";
38+
String databaseId = "my-database";
39+
String backupScheduleId = "my-backup-schedule";
40+
createIncrementalBackupSchedule(projectId, instanceId, databaseId, backupScheduleId);
41+
}
42+
43+
static void createIncrementalBackupSchedule(
44+
String projectId, String instanceId, String databaseId, String backupScheduleId)
45+
throws IOException {
46+
final CreateBackupEncryptionConfig encryptionConfig =
47+
CreateBackupEncryptionConfig.newBuilder()
48+
.setEncryptionType(
49+
CreateBackupEncryptionConfig.EncryptionType.GOOGLE_DEFAULT_ENCRYPTION)
50+
.build();
51+
final BackupSchedule backupSchedule =
52+
BackupSchedule.newBuilder()
53+
.setIncrementalBackupSpec(IncrementalBackupSpec.newBuilder().build())
54+
.setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24).build())
55+
.setSpec(
56+
BackupScheduleSpec.newBuilder()
57+
.setCronSpec(CrontabSpec.newBuilder().setText("30 12 * * *").build())
58+
.build())
59+
.setEncryptionConfig(encryptionConfig)
60+
.build();
61+
62+
try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
63+
DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId);
64+
final BackupSchedule createdBackupSchedule =
65+
databaseAdminClient.createBackupSchedule(
66+
CreateBackupScheduleRequest.newBuilder()
67+
.setParent(databaseName.toString())
68+
.setBackupScheduleId(backupScheduleId)
69+
.setBackupSchedule(backupSchedule)
70+
.build());
71+
System.out.println(
72+
String.format(
73+
"Created incremental backup schedule: %s\n%s",
74+
createdBackupSchedule.getName(), createdBackupSchedule.toString()));
75+
}
76+
}
77+
}
78+
// [END spanner_create_incremental_backup_schedule]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_delete_backup_schedule]
20+
21+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
22+
import com.google.spanner.admin.database.v1.BackupScheduleName;
23+
import com.google.spanner.admin.database.v1.DeleteBackupScheduleRequest;
24+
import java.io.IOException;
25+
26+
class DeleteBackupScheduleSample {
27+
28+
static void deleteBackupSchedule() throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "my-project";
31+
String instanceId = "my-instance";
32+
String databaseId = "my-database";
33+
String backupScheduleId = "my-backup-schedule";
34+
deleteBackupSchedule(projectId, instanceId, databaseId, backupScheduleId);
35+
}
36+
37+
static void deleteBackupSchedule(
38+
String projectId, String instanceId, String databaseId, String backupScheduleId)
39+
throws IOException {
40+
try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
41+
BackupScheduleName backupScheduleName =
42+
BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId);
43+
databaseAdminClient.deleteBackupSchedule(
44+
DeleteBackupScheduleRequest.newBuilder().setName(backupScheduleName.toString()).build());
45+
System.out.println(
46+
String.format("Deleted backup schedule: %s", backupScheduleName.toString()));
47+
}
48+
}
49+
}
50+
// [END spanner_delete_backup_schedule]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_get_backup_schedule]
20+
21+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
22+
import com.google.spanner.admin.database.v1.BackupSchedule;
23+
import com.google.spanner.admin.database.v1.BackupScheduleName;
24+
import com.google.spanner.admin.database.v1.GetBackupScheduleRequest;
25+
import java.io.IOException;
26+
27+
class GetBackupScheduleSample {
28+
29+
static void getBackupSchedule() throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my-project";
32+
String instanceId = "my-instance";
33+
String databaseId = "my-database";
34+
String backupScheduleId = "my-backup-schedule";
35+
getBackupSchedule(projectId, instanceId, databaseId, backupScheduleId);
36+
}
37+
38+
static void getBackupSchedule(
39+
String projectId, String instanceId, String databaseId, String backupScheduleId)
40+
throws IOException {
41+
try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
42+
BackupScheduleName backupScheduleName =
43+
BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId);
44+
final BackupSchedule backupSchedule =
45+
databaseAdminClient.getBackupSchedule(
46+
GetBackupScheduleRequest.newBuilder().setName(backupScheduleName.toString()).build());
47+
System.out.println(
48+
String.format(
49+
"Backup schedule: %s\n%s", backupSchedule.getName(), backupSchedule.toString()));
50+
}
51+
}
52+
}
53+
// [END spanner_get_backup_schedule]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_list_backup_schedules]
20+
21+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
22+
import com.google.spanner.admin.database.v1.BackupSchedule;
23+
import com.google.spanner.admin.database.v1.DatabaseName;
24+
import java.io.IOException;
25+
26+
class ListBackupSchedulesSample {
27+
28+
static void listBackupSchedules() throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "my-project";
31+
String instanceId = "my-instance";
32+
String databaseId = "my-database";
33+
listBackupSchedules(projectId, instanceId, databaseId);
34+
}
35+
36+
static void listBackupSchedules(String projectId, String instanceId, String databaseId)
37+
throws IOException {
38+
try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
39+
DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId);
40+
41+
System.out.println(
42+
String.format("Backup schedules for database '%s'", databaseName.toString()));
43+
for (BackupSchedule backupSchedule :
44+
databaseAdminClient.listBackupSchedules(databaseName).iterateAll()) {
45+
System.out.println(
46+
String.format(
47+
"Backup schedule: %s\n%s", backupSchedule.getName(), backupSchedule.toString()));
48+
}
49+
}
50+
}
51+
}
52+
// [END spanner_list_backup_schedules]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_update_backup_schedule]
20+
21+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
22+
import com.google.protobuf.Duration;
23+
import com.google.protobuf.FieldMask;
24+
import com.google.spanner.admin.database.v1.BackupSchedule;
25+
import com.google.spanner.admin.database.v1.BackupScheduleName;
26+
import com.google.spanner.admin.database.v1.BackupScheduleSpec;
27+
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig;
28+
import com.google.spanner.admin.database.v1.CrontabSpec;
29+
import com.google.spanner.admin.database.v1.UpdateBackupScheduleRequest;
30+
import java.io.IOException;
31+
32+
class UpdateBackupScheduleSample {
33+
34+
static void updateBackupSchedule() throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "my-project";
37+
String instanceId = "my-instance";
38+
String databaseId = "my-database";
39+
String backupScheduleId = "my-backup-schedule";
40+
updateBackupSchedule(projectId, instanceId, databaseId, backupScheduleId);
41+
}
42+
43+
static void updateBackupSchedule(
44+
String projectId, String instanceId, String databaseId, String backupScheduleId)
45+
throws IOException {
46+
BackupScheduleName backupScheduleName =
47+
BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId);
48+
final CreateBackupEncryptionConfig encryptionConfig =
49+
CreateBackupEncryptionConfig.newBuilder()
50+
.setEncryptionType(CreateBackupEncryptionConfig.EncryptionType.USE_DATABASE_ENCRYPTION)
51+
.build();
52+
final BackupSchedule backupSchedule =
53+
BackupSchedule.newBuilder()
54+
.setName(backupScheduleName.toString())
55+
.setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 48))
56+
.setSpec(
57+
BackupScheduleSpec.newBuilder()
58+
.setCronSpec(CrontabSpec.newBuilder().setText("45 15 * * *").build())
59+
.build())
60+
.setEncryptionConfig(encryptionConfig)
61+
.build();
62+
63+
try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
64+
final FieldMask fieldMask =
65+
FieldMask.newBuilder()
66+
.addPaths("retention_duration")
67+
.addPaths("spec.cron_spec.text")
68+
.addPaths("encryption_config")
69+
.build();
70+
final BackupSchedule updatedBackupSchedule =
71+
databaseAdminClient.updateBackupSchedule(
72+
UpdateBackupScheduleRequest.newBuilder()
73+
.setBackupSchedule(backupSchedule)
74+
.setUpdateMask(fieldMask)
75+
.build());
76+
System.out.println(
77+
String.format(
78+
"Updated backup schedule: %s\n%s",
79+
updatedBackupSchedule.getName(), updatedBackupSchedule.toString()));
80+
}
81+
}
82+
}
83+
// [END spanner_update_backup_schedule]

0 commit comments

Comments
 (0)