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 all 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,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]
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]
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]
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]
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]
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]
Loading
Loading