|
| 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_copy_backup_with_MR_CMEK] |
| 27 | +use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient; |
| 28 | +use Google\Cloud\Spanner\Admin\Database\V1\CopyBackupRequest; |
| 29 | +use Google\Cloud\Spanner\Admin\Database\V1\CopyBackupEncryptionConfig; |
| 30 | +use Google\Protobuf\Timestamp; |
| 31 | + |
| 32 | +/** |
| 33 | + * Copy a MR CMEK backup. |
| 34 | + * Example: |
| 35 | + * ``` |
| 36 | + * copy_backup_with_mr_cmek($projectId, $instanceId, $sourceBackupId, $backupId, $kmsKeyNames); |
| 37 | + * ``` |
| 38 | + * @param string $projectId The Google Cloud project ID. |
| 39 | + * @param string $instanceId The Spanner instance ID. |
| 40 | + * @param string $sourceBackupId The Spanner source backup ID. |
| 41 | + * @param string $backupId The Spanner backup ID. |
| 42 | + * @param string[] $kmsKeyNames The KMS keys used for encryption. |
| 43 | + */ |
| 44 | +/** |
| 45 | + * Create a copy MR CMEK backup from another source backup. |
| 46 | + * Example: |
| 47 | + * ``` |
| 48 | + * copy_backup_with_mr_cmek($projectId, $destInstanceId, $destBackupId, $sourceInstanceId, $sourceBackupId, $kmsKeyNames); |
| 49 | + * ``` |
| 50 | + * |
| 51 | + * @param string $projectId The Google Cloud project ID. |
| 52 | + * @param string $destInstanceId The Spanner instance ID where the copy backup will reside. |
| 53 | + * @param string $destBackupId The Spanner backup ID of the new backup to be created. |
| 54 | + * @param string $sourceInstanceId The Spanner instance ID of the source backup. |
| 55 | + * @param string $sourceBackupId The Spanner backup ID of the source. |
| 56 | +* @param string[] $kmsKeyNames The KMS keys used for encryption. |
| 57 | + */ |
| 58 | +function copy_backup_with_mr_cmek( |
| 59 | + string $projectId, |
| 60 | + string $destInstanceId, |
| 61 | + string $destBackupId, |
| 62 | + string $sourceInstanceId, |
| 63 | + string $sourceBackupId, |
| 64 | + array $kmsKeyNames |
| 65 | +): void { |
| 66 | + $databaseAdminClient = new DatabaseAdminClient(); |
| 67 | + |
| 68 | + $destInstanceFullName = DatabaseAdminClient::instanceName($projectId, $destInstanceId); |
| 69 | + $expireTime = new Timestamp(); |
| 70 | + $expireTime->setSeconds((new \DateTime('+8 hours'))->getTimestamp()); |
| 71 | + $sourceBackupFullName = DatabaseAdminClient::backupName($projectId, $sourceInstanceId, $sourceBackupId); |
| 72 | + $request = new CopyBackupRequest([ |
| 73 | + 'source_backup' => $sourceBackupFullName, |
| 74 | + 'parent' => $destInstanceFullName, |
| 75 | + 'backup_id' => $destBackupId, |
| 76 | + 'expire_time' => $expireTime, |
| 77 | + 'encryption_config' => new CopyBackupEncryptionConfig([ |
| 78 | + 'kms_key_names' => $kmsKeyNames, |
| 79 | + 'encryption_type' => CopyBackupEncryptionConfig\EncryptionType::CUSTOMER_MANAGED_ENCRYPTION |
| 80 | + ]) |
| 81 | + ]); |
| 82 | + |
| 83 | + $operationResponse = $databaseAdminClient->copyBackup($request); |
| 84 | + $operationResponse->pollUntilComplete(); |
| 85 | + |
| 86 | + if (!$operationResponse->operationSucceeded()) { |
| 87 | + $error = $operationResponse->getError(); |
| 88 | + printf('Backup not created due to error: %s.' . PHP_EOL, $error->getMessage()); |
| 89 | + return; |
| 90 | + } |
| 91 | + $destBackupInfo = $operationResponse->getResult(); |
| 92 | + $kmsKeyVersions = []; |
| 93 | + foreach ($destBackupInfo->getEncryptionInformation() as $encryptionInfo) { |
| 94 | + $kmsKeyVersions[] = $encryptionInfo->getKmsKeyVersion(); |
| 95 | + } |
| 96 | + printf( |
| 97 | + 'Backup %s of size %d bytes was copied at %d from the source backup %s using encryption keys %s' . PHP_EOL, |
| 98 | + basename($destBackupInfo->getName()), |
| 99 | + $destBackupInfo->getSizeBytes(), |
| 100 | + $destBackupInfo->getCreateTime()->getSeconds(), |
| 101 | + $sourceBackupId, |
| 102 | + print_r($kmsKeyVersions, true) |
| 103 | + ); |
| 104 | + printf('Version time of the copied backup: %d' . PHP_EOL, $destBackupInfo->getVersionTime()->getSeconds()); |
| 105 | +} |
| 106 | +// [END spanner_copy_backup_with_MR_CMEK] |
| 107 | + |
| 108 | +// The following 2 lines are only needed to run the samples |
| 109 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 110 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments