Skip to content

Commit eaf52ec

Browse files
committed
feat(spanner): backup and restore code samples
1 parent 34cb3b4 commit eaf52ec

11 files changed

+777
-8
lines changed

spanner/composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@
5454
"src/query_data_with_string_parameter.php",
5555
"src/query_data_with_timestamp_parameter.php",
5656
"src/create_client_with_query_options.php",
57-
"src/query_data_with_query_options.php"
57+
"src/query_data_with_query_options.php",
58+
"src/cancel_backup.php",
59+
"src/create_backup.php",
60+
"src/delete_backup.php",
61+
"src/list_backup_operations.php",
62+
"src/list_backups.php",
63+
"src/list_database_operations.php",
64+
"src/restore_backup.php",
65+
"src/update_backup.php"
5866
]
5967
}
6068
}

spanner/spanner.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@
3333
new InputArgument('database_id', InputArgument::REQUIRED, 'The database id'),
3434
]);
3535

36+
$instanceInputDefinition = new InputDefinition([
37+
new InputArgument('instance_id', InputArgument::REQUIRED, 'The instance id'),
38+
]);
39+
40+
$idbInputDefinition = new InputDefinition([
41+
new InputArgument('instance_id', InputArgument::REQUIRED, 'The instance id'),
42+
new InputArgument('database_id', InputArgument::REQUIRED, 'The database id'),
43+
new InputArgument('backup_id', InputArgument::REQUIRED, 'The backup id'),
44+
]);
45+
46+
$ibInputDefinition = new InputDefinition([
47+
new InputArgument('instance_id', InputArgument::REQUIRED, 'The instance id'),
48+
new InputArgument('backup_id', InputArgument::REQUIRED, 'The backup id'),
49+
]);
50+
3651
// Create Database command
3752
$application->add((new Command('create-database'))
3853
->setDefinition($inputDefinition)
@@ -636,6 +651,94 @@
636651
})
637652
);
638653

654+
$application->add((new Command('create-backup'))
655+
->setDefinition($idbInputDefinition)
656+
->setDescription('Lists existing backups for instance.')
657+
->setCode(function ($input, $output) {
658+
create_backup(
659+
$input->getArgument('instance_id'),
660+
$input->getArgument('database_id'),
661+
$input->getArgument('backup_id')
662+
);
663+
})
664+
);
665+
666+
$application->add((new Command('cancel-backup'))
667+
->setDefinition($inputDefinition)
668+
->setDescription('Cancels backup operation.')
669+
->setCode(function ($input, $output) {
670+
cancel_backup_operation(
671+
$input->getArgument('instance_id'),
672+
$input->getArgument('database_id')
673+
);
674+
})
675+
);
676+
677+
$application->add((new Command('list-backups'))
678+
->setDefinition($instanceInputDefinition)
679+
->setDescription('Lists existing backups for instance.')
680+
->setCode(function ($input, $output) {
681+
list_backups(
682+
$input->getArgument('instance_id')
683+
);
684+
})
685+
);
686+
687+
$application->add((new Command('restore-backup'))
688+
->setDefinition($idbInputDefinition)
689+
->setDescription('Restore database from backup.')
690+
->setCode(function ($input, $output) {
691+
restore_backup(
692+
$input->getArgument('instance_id'),
693+
$input->getArgument('database_id'),
694+
$input->getArgument('backup_id')
695+
);
696+
})
697+
);
698+
699+
$application->add((new Command('update-backup'))
700+
->setDefinition($ibInputDefinition)
701+
->setDescription('Update backup expire time.')
702+
->setCode(function ($input, $output) {
703+
update_backup(
704+
$input->getArgument('instance_id'),
705+
$input->getArgument('backup_id')
706+
);
707+
})
708+
);
709+
710+
$application->add((new Command('delete-backup'))
711+
->setDefinition($ibInputDefinition)
712+
->setDescription('Lists existing backups.')
713+
->setCode(function ($input, $output) {
714+
delete_backup(
715+
$input->getArgument('instance_id'),
716+
$input->getArgument('backup_id')
717+
);
718+
})
719+
);
720+
721+
$application->add((new Command('list-backup-operations'))
722+
->setDefinition($inputDefinition)
723+
->setDescription('Lists backup operations.')
724+
->setCode(function ($input, $output) {
725+
list_backup_operations(
726+
$input->getArgument('instance_id'),
727+
$input->getArgument('database_id')
728+
);
729+
})
730+
);
731+
732+
$application->add((new Command('list-database-operations'))
733+
->setDefinition($instanceInputDefinition)
734+
->setDescription('Lists database operations.')
735+
->setCode(function ($input, $output) {
736+
list_database_operations(
737+
$input->getArgument('instance_id')
738+
);
739+
})
740+
);
741+
639742
// for testing
640743
if (getenv('PHPUNIT_TESTS') === '1') {
641744
return $application;

spanner/src/cancel_backup.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* Copyright 2019 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_cancel_backup_create]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Cancel a backup operation.
31+
* Example:
32+
* ```
33+
* cancel_backup_operation($instanceId, $databaseId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
*/
39+
function cancel_backup_operation($instanceId, $databaseId)
40+
{
41+
$spanner = new SpannerClient();
42+
$instance = $spanner->instance($instanceId);
43+
$database = $instance->database($databaseId);
44+
$backupId = uniqid('backup-' . $databaseId . '-cancel');
45+
46+
$expireTime = new \DateTime('+14 days');
47+
$backup = $instance->backup($backupId);
48+
$operation = $backup->create($database->name(), $expireTime);
49+
$operation->cancel();
50+
print('Waiting for operation to complete ...' . PHP_EOL);
51+
$operation->pollUntilComplete();
52+
53+
// Cancel operations are always successful regardless of whether the operation is
54+
// still in progress or is complete.
55+
printf('Cancel backup operation complete.' . PHP_EOL);
56+
57+
// Operation may succeed before cancel() has been called. So we need to clean up created backup.
58+
$backup->delete();
59+
}
60+
// [END spanner_cancel_backup_create]

spanner/src/create_backup.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
/**
3+
* Copyright 2019 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_create_backup]
27+
use Google\Cloud\Spanner\Backup;
28+
use Google\Cloud\Spanner\SpannerClient;
29+
30+
/**
31+
* Create a backup.
32+
* Example:
33+
* ```
34+
* create_backup($instanceId, $databaseId, $backupId);
35+
* ```
36+
*
37+
* @param string $instanceId The Spanner instance ID.
38+
* @param string $databaseId The Spanner database ID.
39+
* @param string $backupId The Spanner backup ID.
40+
*/
41+
function create_backup($instanceId, $databaseId, $backupId)
42+
{
43+
$spanner = new SpannerClient();
44+
$instance = $spanner->instance($instanceId);
45+
$database = $instance->database($databaseId);
46+
47+
$expireTime = new \DateTime('+14 days');
48+
$backup = $instance->backup($backupId);
49+
$operation = $backup->create($database->name(), $expireTime);
50+
51+
print('Waiting for operation to complete...' . PHP_EOL);
52+
$operation->pollUntilComplete();
53+
54+
$backup->reload();
55+
$ready = ($backup->state() == Backup::STATE_READY);
56+
57+
if ($ready) {
58+
print('Backup is ready!' . PHP_EOL);
59+
$info = $backup->info();
60+
printf('Backup %s of size %d bytes was created at %s' . PHP_EOL, basename($info['name']), $info['sizeBytes'], $info['createTime']);
61+
} else {
62+
print('Backup is not ready!' . PHP_EOL);
63+
}
64+
}
65+
// [END spanner_create_backup]

spanner/src/delete_backup.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2019 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_delete_backup]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Delete a backup.
31+
* Example:
32+
* ```
33+
* delete_backup($instanceId, $backupId);
34+
* ```
35+
* @param string $instanceId The Spanner instance ID.
36+
* @param string $backupId The Spanner backup ID.
37+
*/
38+
function delete_backup($instanceId, $backupId)
39+
{
40+
$spanner = new SpannerClient();
41+
$instance = $spanner->instance($instanceId);
42+
$backup = $instance->backup($backupId);
43+
$backupName = $backup->name();
44+
$backup->delete();
45+
print("Backup $backupName deleted" . PHP_EOL);
46+
}
47+
// [END spanner_delete_backup]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2019 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_list_backup_operations]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* List all create backup operations in an instance.
31+
* Example:
32+
* ```
33+
* list_backup_operations($instanceId, $databaseId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
*/
39+
function list_backup_operations($instanceId, $databaseId)
40+
{
41+
$spanner = new SpannerClient();
42+
$instance = $spanner->instance($instanceId);
43+
44+
// List the CreateBackup operations.
45+
$filter = "(metadata.database:$databaseId) AND " .
46+
"(metadata.@type:type.googleapis.com/" .
47+
"google.spanner.admin.database.v1.CreateBackupMetadata)";
48+
49+
$operations = $instance->backupOperations(['filter' => $filter]);
50+
51+
foreach ($operations as $operation) {
52+
if (!$operation->done()) {
53+
$operationName = basename($operation->info()['name']);
54+
print("Backup operation $operationName pending" . PHP_EOL);
55+
}
56+
}
57+
}
58+
// [END spanner_list_backup_operations]

0 commit comments

Comments
 (0)