|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2021 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/master/spanner/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Spanner; |
| 25 | + |
| 26 | +// [START spanner_copy_backup] |
| 27 | +use Google\Cloud\Spanner\Backup; |
| 28 | +use Google\Cloud\Spanner\SpannerClient; |
| 29 | + |
| 30 | +/** |
| 31 | + * Create a copy backup from another source backup. |
| 32 | + * Example: |
| 33 | + * ``` |
| 34 | + * copy_backup($destInstanceId, $destBackupId, $sourceInstanceId, $sourceBackupId); |
| 35 | + * ``` |
| 36 | + * |
| 37 | + * @param string $destInstanceId The Spanner instance ID where the copy backup will reside. |
| 38 | + * @param string $destBackupId The Spanner backup ID of the new backup to be created. |
| 39 | + * @param string $sourceInstanceId The Spanner instance ID of the source backup. |
| 40 | + * @param string $sourceBackupId The Spanner backup ID of the source. |
| 41 | + */ |
| 42 | +function copy_backup($destInstanceId, $destBackupId, $sourceInstanceId, $sourceBackupId) |
| 43 | +{ |
| 44 | + $spanner = new SpannerClient(); |
| 45 | + |
| 46 | + $destInstance = $spanner->instance($destInstanceId); |
| 47 | + $sourceInstance = $spanner->instance($sourceInstanceId); |
| 48 | + $sourceBackup = $sourceInstance->backup($sourceBackupId); |
| 49 | + $destBackup = $destInstance->backup($destBackupId); |
| 50 | + |
| 51 | + $expireTime = new \DateTime('+8 hours'); |
| 52 | + $operation = $sourceBackup->createCopy($destBackup, $expireTime); |
| 53 | + |
| 54 | + print('Waiting for operation to complete...' . PHP_EOL); |
| 55 | + |
| 56 | + $operation->pollUntilComplete(); |
| 57 | + $destBackup->reload(); |
| 58 | + |
| 59 | + $ready = ($destBackup->state() == Backup::STATE_READY); |
| 60 | + |
| 61 | + if ($ready) { |
| 62 | + print('Backup is ready!' . PHP_EOL); |
| 63 | + $info = $destBackup->info(); |
| 64 | + printf( |
| 65 | + 'Backup %s of size %d bytes was copied at %s from the source backup %s' . PHP_EOL, |
| 66 | + basename($info['name']), $info['sizeBytes'], $info['createTime'], $sourceBackupId); |
| 67 | + printf('Version time of the copied backup: %s' . PHP_EOL, $info['versionTime']); |
| 68 | + } else { |
| 69 | + printf('Unexpected state: %s' . PHP_EOL, $destBackup->state()); |
| 70 | + } |
| 71 | +} |
| 72 | +// [END spanner_copy_backup] |
| 73 | + |
| 74 | +// The following 2 lines are only needed to run the samples |
| 75 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 76 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments