Skip to content

Commit 89f7930

Browse files
feat(spanner): sample for copyBackup (GoogleCloudPlatform#1568)
1 parent 56329e3 commit 89f7930

File tree

6 files changed

+135
-15
lines changed

6 files changed

+135
-15
lines changed

spanner/composer.json

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-spanner": "^1.28.0"
3+
"google/cloud-spanner": "^1.48.0"
44
}
55
}

spanner/src/copy_backup.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
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);

spanner/src/create_backup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function create_backup($instanceId, $databaseId, $backupId, $versionTime)
6464
'Backup %s of size %d bytes was created at %s for version of database at %s' . PHP_EOL,
6565
basename($info['name']), $info['sizeBytes'], $info['createTime'], $info['versionTime']);
6666
} else {
67-
print('Backup is not ready!' . PHP_EOL);
67+
printf('Unexpected state: %s' . PHP_EOL, $backup->state());
6868
}
6969
}
7070
// [END spanner_create_backup]

spanner/src/list_backup_operations.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,27 @@
2828

2929
/**
3030
* List all create backup operations in an instance.
31-
* Example:
32-
* ```
33-
* list_backup_operations($instanceId, $databaseId);
34-
* ```
31+
* Optionally passing the backupId will also list the
32+
* copy backup operations on the backup.
3533
*
3634
* @param string $instanceId The Spanner instance ID.
3735
* @param string $databaseId The Spanner database ID.
36+
* @param string $backupId The Spanner backup ID whose copy operations need to be listed.
3837
*/
39-
function list_backup_operations($instanceId, $databaseId)
40-
{
38+
function list_backup_operations(
39+
string $instanceId,
40+
string $databaseId,
41+
string $backupId = null
42+
): void {
4143
$spanner = new SpannerClient();
4244
$instance = $spanner->instance($instanceId);
4345

4446
// List the CreateBackup operations.
45-
$filter = "(metadata.database:$databaseId) AND " .
46-
'(metadata.@type:type.googleapis.com/' .
47-
'google.spanner.admin.database.v1.CreateBackupMetadata)';
47+
$filter = '(metadata.@type:type.googleapis.com/' .
48+
'google.spanner.admin.database.v1.CreateBackupMetadata) AND ' . "(metadata.database:$databaseId)";
4849

50+
// See https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.database.v1#listbackupoperationsrequest
51+
// for the possible filter values
4952
$operations = $instance->backupOperations(['filter' => $filter]);
5053

5154
foreach ($operations as $operation) {
@@ -57,6 +60,25 @@ function list_backup_operations($instanceId, $databaseId)
5760
printf('Backup %s on database %s is %d%% complete.' . PHP_EOL, $backupName, $dbName, $progress);
5861
}
5962
}
63+
64+
if (is_null($backupId)) {
65+
return;
66+
}
67+
68+
// List copy backup operations
69+
$filter = '(metadata.@type:type.googleapis.com/' .
70+
'google.spanner.admin.database.v1.CopyBackupMetadata) AND ' . "(metadata.source_backup:$backupId)";
71+
72+
$operations = $instance->backupOperations(['filter' => $filter]);
73+
74+
foreach ($operations as $operation) {
75+
if (!$operation->done()) {
76+
$meta = $operation->info()['metadata'];
77+
$backupName = basename($meta['name']);
78+
$progress = $meta['progress']['progressPercent'];
79+
printf('Copy Backup %s on source backup %s is %d%% complete.' . PHP_EOL, $backupName, $backupId, $progress);
80+
}
81+
}
6082
}
6183
// [END spanner_list_backup_operations]
6284

spanner/src/update_backup.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
// [START spanner_update_backup]
2727
use Google\Cloud\Spanner\SpannerClient;
28+
use DateTime;
2829

2930
/**
3031
* Update the backup expire time.
@@ -40,12 +41,16 @@ function update_backup($instanceId, $backupId)
4041
$spanner = new SpannerClient();
4142
$instance = $spanner->instance($instanceId);
4243
$backup = $instance->backup($backupId);
44+
$backup->reload();
4345

44-
// Expire time must be within 366 days of the create time of the backup.
45-
$newTimestamp = new \DateTime('+30 days');
46-
$backup->updateExpireTime($newTimestamp);
46+
$newExpireTime = new DateTime('+30 days');
47+
$maxExpireTime = new DateTime($backup->info()['maxExpireTime']);
48+
// The new expire time can't be greater than maxExpireTime for the backup.
49+
$newExpireTime = min($newExpireTime, $maxExpireTime);
4750

48-
print("Backup $backupId new expire time: " . $backup->info()['expireTime'] . PHP_EOL);
51+
$backup->updateExpireTime($newExpireTime);
52+
53+
printf('Backup %s new expire time: %s' . PHP_EOL, $backupId, $backup->info()['expireTime']);
4954
}
5055
// [END spanner_update_backup]
5156

spanner/test/spannerBackupTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,23 @@ public function testListBackupOperations()
163163
$this->assertStringContainsString($databaseId2, $output);
164164
}
165165

166+
/**
167+
* @depends testCreateBackup
168+
*/
169+
public function testCopyBackup()
170+
{
171+
$newBackupId = 'copy-' . self::$backupId . '-' . time();
172+
173+
$output = $this->runFunctionSnippet('copy_backup', [
174+
$newBackupId,
175+
self::$instanceId,
176+
self::$backupId
177+
]);
178+
179+
$regex = '/Backup %s of size \d+ bytes was copied at (.+) from the source backup %s/';
180+
$this->assertRegExp(sprintf($regex, $newBackupId, self::$backupId), $output);
181+
}
182+
166183
/**
167184
* @depends testCreateBackup
168185
*/

0 commit comments

Comments
 (0)