Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add samples for backup schedule feature APIs. #3339

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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_backup_schedule_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved

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.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 CreateBackupScheduleSample {
aman-19 marked this conversation as resolved.
Show resolved Hide resolved

static void createBackupSchedule() 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";
createBackupSchedule(projectId, instanceId, databaseId, backupScheduleId);
}

static void createBackupSchedule(
String projectId, String instanceId, String databaseId, String backupScheduleId)
throws IOException {
final BackupSchedule backupSchedule =
BackupSchedule.newBuilder()
.setFullBackupSpec(FullBackupSpec.newBuilder().build())
.setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 7).build())
.setSpec(
BackupScheduleSpec.newBuilder()
.setCronSpec(CrontabSpec.newBuilder().setText("0 0 * * *").build())
.build())
.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", createdBackupSchedule.getName()));
}
}
}
// [END spanner_create_backup_schedule_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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_backup_schedule_with_encryption_sample]

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 CreateBackupScheduleWithEncryptionSample {

static void createBackupScheduleWithEncryption() 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";
String kmsKeyName =
"projects/" + projectId + "/locations/<location>/keyRings/<keyRing>/cryptoKeys/<keyId>";
createBackupScheduleWithEncryption(
projectId, instanceId, databaseId, backupScheduleId, kmsKeyName);
}

static void createBackupScheduleWithEncryption(
String projectId,
String instanceId,
String databaseId,
String backupScheduleId,
String kmsKeyName)
throws IOException {
final CreateBackupEncryptionConfig encryptionConfig =
CreateBackupEncryptionConfig.newBuilder()
.setEncryptionType(
CreateBackupEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION)
.setKmsKeyName(kmsKeyName)
.build();
final BackupSchedule backupSchedule =
BackupSchedule.newBuilder()
.setFullBackupSpec(FullBackupSpec.newBuilder().build())
.setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 7))
.setSpec(
BackupScheduleSpec.newBuilder()
.setCronSpec(CrontabSpec.newBuilder().setText("0 0 * * *").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 with encryption: %s", createdBackupSchedule.getName()));
}
}
}
// [END spanner_create_backup_schedule_with_encryption_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved

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.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 BackupSchedule backupSchedule =
BackupSchedule.newBuilder()
.setIncrementalBackupSpec(IncrementalBackupSpec.newBuilder().build())
.setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 7).build())
.setSpec(
BackupScheduleSpec.newBuilder()
.setCronSpec(CrontabSpec.newBuilder().setText("0 */6 * * *").build())
.build())
.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", createdBackupSchedule.getName()));
aman-19 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
// [END spanner_create_incremental_backup_schedule_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved
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_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved

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_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved
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_get_backup_schedule_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved

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("Retrieved backup schedule: %s", backupScheduleName.toString()));
aman-19 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
// [END spanner_get_backup_schedule_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved

import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListBackupSchedulesPage;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListBackupSchedulesPagedResponse;
import com.google.spanner.admin.database.v1.BackupSchedule;
import com.google.spanner.admin.database.v1.DatabaseName;
import com.google.spanner.admin.database.v1.ListBackupSchedulesRequest;
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);
ListBackupSchedulesPagedResponse backupSchedules =
databaseAdminClient.listBackupSchedules(
ListBackupSchedulesRequest.newBuilder().setParent(databaseName.toString()).build());

System.out.println(
String.format("Backup schedules for database '%s'", databaseName.toString()));
for (ListBackupSchedulesPage page : backupSchedules.iteratePages()) {
for (BackupSchedule backupSchedule : page.iterateAll()) {
System.out.println(String.format("Backup schedule: %s", backupSchedule.getName()));
aman-19 marked this conversation as resolved.
Show resolved Hide resolved
}
}
aman-19 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
// [END spanner_list_backup_schedules_sample]
aman-19 marked this conversation as resolved.
Show resolved Hide resolved
Loading