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