Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spanner/composer.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-spanner": "^1.28.0"
"google/cloud-spanner": "^1.48.0"
}
}
76 changes: 76 additions & 0 deletions spanner/src/copy_backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md
*/

namespace Google\Cloud\Samples\Spanner;

// [START spanner_copy_backup]
use Google\Cloud\Spanner\Backup;
use Google\Cloud\Spanner\SpannerClient;

/**
* Create a copy backup from another source backup.
* Example:
* ```
* copy_backup($destInstanceId, $destBackupId, $sourceInstanceId, $sourceBackupId);
* ```
*
* @param string $destInstanceId The Spanner instance ID where the copy backup will reside.
* @param string $destBackupId The Spanner backup ID of the new backup to be created.
* @param string $sourceInstanceId The Spanner instance ID of the source backup.
* @param string $sourceBackupId The Spanner backup ID of the source.
*/
function copy_backup($destInstanceId, $destBackupId, $sourceInstanceId, $sourceBackupId)
{
$spanner = new SpannerClient();

$destInstance = $spanner->instance($destInstanceId);
$sourceInstance = $spanner->instance($sourceInstanceId);
$sourceBackup = $sourceInstance->backup($sourceBackupId);
$destBackup = $destInstance->backup($destBackupId);

$expireTime = new \DateTime('+8 hours');
$operation = $sourceBackup->createCopy($destBackup, $expireTime);

print('Waiting for operation to complete...' . PHP_EOL);

$operation->pollUntilComplete();
$destBackup->reload();

$ready = ($destBackup->state() == Backup::STATE_READY);

if ($ready) {
print('Backup is ready!' . PHP_EOL);
$info = $destBackup->info();
printf(
'Backup %s of size %d bytes was copied at %s from the source backup %s' . PHP_EOL,
basename($info['name']), $info['sizeBytes'], $info['createTime'], $sourceBackupId);
printf('Version time of the copied backup: %s' . PHP_EOL, $info['versionTime']);
} else {
printf('Unexpected state: %s' . PHP_EOL, $destBackup->state());
}
}
// [END spanner_copy_backup]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
2 changes: 1 addition & 1 deletion spanner/src/create_backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function create_backup($instanceId, $databaseId, $backupId, $versionTime)
'Backup %s of size %d bytes was created at %s for version of database at %s' . PHP_EOL,
basename($info['name']), $info['sizeBytes'], $info['createTime'], $info['versionTime']);
} else {
print('Backup is not ready!' . PHP_EOL);
printf('Unexpected state: %s' . PHP_EOL, $backup->state());
}
}
// [END spanner_create_backup]
Expand Down
40 changes: 31 additions & 9 deletions spanner/src/list_backup_operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,27 @@

/**
* List all create backup operations in an instance.
* Example:
* ```
* list_backup_operations($instanceId, $databaseId);
* ```
* Optionally passing the backupId will also list the
* copy backup operations on the backup.
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
* @param string $backupId The Spanner backup ID whose copy operations need to be listed.
*/
function list_backup_operations($instanceId, $databaseId)
{
function list_backup_operations(
string $instanceId,
string $databaseId,
string $backupId = null
): void {
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);

// List the CreateBackup operations.
$filter = "(metadata.database:$databaseId) AND " .
'(metadata.@type:type.googleapis.com/' .
'google.spanner.admin.database.v1.CreateBackupMetadata)';
$filter = '(metadata.@type:type.googleapis.com/' .
'google.spanner.admin.database.v1.CreateBackupMetadata) AND ' . "(metadata.database:$databaseId)";

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

foreach ($operations as $operation) {
Expand All @@ -57,6 +60,25 @@ function list_backup_operations($instanceId, $databaseId)
printf('Backup %s on database %s is %d%% complete.' . PHP_EOL, $backupName, $dbName, $progress);
}
}

if (is_null($backupId)) {
return;
}

// List copy backup operations
$filter = '(metadata.@type:type.googleapis.com/' .
'google.spanner.admin.database.v1.CopyBackupMetadata) AND ' . "(metadata.source_backup:$backupId)";

$operations = $instance->backupOperations(['filter' => $filter]);

foreach ($operations as $operation) {
if (!$operation->done()) {
$meta = $operation->info()['metadata'];
$backupName = basename($meta['name']);
$progress = $meta['progress']['progressPercent'];
printf('Copy Backup %s on source backup %s is %d%% complete.' . PHP_EOL, $backupName, $backupId, $progress);
}
}
}
// [END spanner_list_backup_operations]

Expand Down
13 changes: 9 additions & 4 deletions spanner/src/update_backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

// [START spanner_update_backup]
use Google\Cloud\Spanner\SpannerClient;
use DateTime;

/**
* Update the backup expire time.
Expand All @@ -40,12 +41,16 @@ function update_backup($instanceId, $backupId)
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$backup = $instance->backup($backupId);
$backup->reload();

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

print("Backup $backupId new expire time: " . $backup->info()['expireTime'] . PHP_EOL);
$backup->updateExpireTime($newExpireTime);

printf('Backup %s new expire time: %s' . PHP_EOL, $backupId, $backup->info()['expireTime']);
}
// [END spanner_update_backup]

Expand Down
17 changes: 17 additions & 0 deletions spanner/test/spannerBackupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ public function testListBackupOperations()
$this->assertStringContainsString($databaseId2, $output);
}

/**
* @depends testCreateBackup
*/
public function testCopyBackup()
{
$newBackupId = 'copy-' . self::$backupId . '-' . time();

$output = $this->runFunctionSnippet('copy_backup', [
$newBackupId,
self::$instanceId,
self::$backupId
]);

$regex = '/Backup %s of size \d+ bytes was copied at (.+) from the source backup %s/';
$this->assertRegExp(sprintf($regex, $newBackupId, self::$backupId), $output);
}

/**
* @depends testCreateBackup
*/
Expand Down