|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * For instructions on how to run the full sample: |
| 20 | + * |
| 21 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/spanner/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Spanner; |
| 25 | + |
| 26 | +// [START spanner_update_backup_schedule] |
| 27 | +use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient; |
| 28 | +use Google\Cloud\Spanner\Admin\Database\V1\UpdateBackupScheduleRequest; |
| 29 | +use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig; |
| 30 | +use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig\EncryptionType; |
| 31 | +use Google\Cloud\Spanner\Admin\Database\V1\BackupSchedule; |
| 32 | +use Google\Cloud\Spanner\Admin\Database\V1\FullBackupSpec; |
| 33 | +use Google\Cloud\Spanner\Admin\Database\V1\BackupScheduleSpec; |
| 34 | +use Google\Cloud\Spanner\Admin\Database\V1\CrontabSpec; |
| 35 | +use Google\Protobuf\Duration; |
| 36 | +use Google\Protobuf\FieldMask; |
| 37 | + |
| 38 | +/** |
| 39 | + * Update an existing backup schedule. |
| 40 | + * Example: |
| 41 | + * ``` |
| 42 | + * update_backup_schedule($projectId, $instanceId, $databaseId, $backupScheduleId); |
| 43 | + * ``` |
| 44 | + * |
| 45 | + * @param string $projectId The Google Cloud project ID. |
| 46 | + * @param string $instanceId The Spanner instance ID. |
| 47 | + * @param string $databaseId The Spanner database ID. |
| 48 | + * @param string $backupScheduleId The ID of the backup schedule to be created. |
| 49 | + * at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS |
| 50 | + */ |
| 51 | +function update_backup_schedule( |
| 52 | + string $projectId, |
| 53 | + string $instanceId, |
| 54 | + string $databaseId, |
| 55 | + string $backupScheduleId, |
| 56 | +): void { |
| 57 | + $databaseAdminClient = new DatabaseAdminClient(); |
| 58 | + |
| 59 | + $encryptionConfig = new CreateBackupEncryptionConfig([ |
| 60 | + 'encryption_type' => EncryptionType::USE_DATABASE_ENCRYPTION, |
| 61 | + ]); |
| 62 | + $backupScheduleName = sprintf( |
| 63 | + 'projects/%s/instances/%s/databases/%s/backupSchedules/%s', |
| 64 | + $projectId, |
| 65 | + $instanceId, |
| 66 | + $databaseId, |
| 67 | + $backupScheduleId |
| 68 | + ); |
| 69 | + $backupSchedule = new BackupSchedule([ |
| 70 | + 'name' => $backupScheduleName, |
| 71 | + 'full_backup_spec' => new FullBackupSpec(), |
| 72 | + 'retention_duration' => (new Duration()) |
| 73 | + ->setSeconds(48 * 60 * 60), |
| 74 | + 'spec' => new BackupScheduleSpec([ |
| 75 | + 'cron_spec' => new CrontabSpec([ |
| 76 | + 'text' => '45 15 * * *' |
| 77 | + ]), |
| 78 | + ]), |
| 79 | + 'encryption_config' => $encryptionConfig, |
| 80 | + ]); |
| 81 | + $fieldMask = (new FieldMask()) |
| 82 | + ->setPaths([ |
| 83 | + 'retention_duration', |
| 84 | + 'spec.cron_spec.text', |
| 85 | + 'encryption_config', |
| 86 | + ]); |
| 87 | + |
| 88 | + $request = new UpdateBackupScheduleRequest([ |
| 89 | + 'backup_schedule' => $backupSchedule, |
| 90 | + 'update_mask' => $fieldMask, |
| 91 | + ]); |
| 92 | + |
| 93 | + $updated_backup_schedule = $databaseAdminClient->updateBackupSchedule($request); |
| 94 | + |
| 95 | + printf('Updated backup scehedule %s' . PHP_EOL, $updated_backup_schedule->getName()); |
| 96 | +} |
| 97 | +// [END spanner_update_backup_schedule] |
| 98 | + |
| 99 | +// The following 2 lines are only needed to run the samples |
| 100 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 101 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments