Skip to content

Commit 75edc7d

Browse files
Add samples for Backup Schedule feature. (#2064)
--------- Co-authored-by: Brent Shaffer <[email protected]>
1 parent 5c64fc6 commit 75edc7d

File tree

6 files changed

+559
-0
lines changed

6 files changed

+559
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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_create_backup_schedule]
27+
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
28+
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupScheduleRequest;
29+
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig;
30+
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig\EncryptionType;
31+
use Google\Cloud\Spanner\Admin\Database\V1\BackupSchedule;
32+
use Google\Cloud\Spanner\Admin\Database\V1\FullBackupSpec;
33+
use Google\Cloud\Spanner\Admin\Database\V1\BackupScheduleSpec;
34+
use Google\Cloud\Spanner\Admin\Database\V1\CrontabSpec;
35+
use Google\Protobuf\Duration;
36+
37+
/**
38+
* Create a backup schedule.
39+
* Example:
40+
* ```
41+
* create_backup_schedule($projectId, $instanceId, $databaseId, $backupScheduleId);
42+
* ```
43+
*
44+
* @param string $projectId The Google Cloud project ID.
45+
* @param string $instanceId The Spanner instance ID.
46+
* @param string $databaseId The Spanner database ID.
47+
* @param string $backupScheduleId The ID of the backup schedule to be created.
48+
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
49+
*/
50+
function create_backup_schedule(
51+
string $projectId,
52+
string $instanceId,
53+
string $databaseId,
54+
string $backupScheduleId,
55+
): void {
56+
$databaseAdminClient = new DatabaseAdminClient();
57+
$databaseFullName = DatabaseAdminClient::databaseName($projectId, $instanceId, $databaseId);
58+
printf('%s', $databaseFullName);
59+
60+
$encryptionConfig = (new CreateBackupEncryptionConfig())
61+
->setEncryptionType(EncryptionType::USE_DATABASE_ENCRYPTION);
62+
$backupSchedule = new BackupSchedule([
63+
'full_backup_spec' => new FullBackupSpec(),
64+
'retention_duration' => (new Duration())
65+
->setSeconds(24 * 60 * 60),
66+
'spec' => new BackupScheduleSpec([
67+
'cron_spec' => new CrontabSpec([
68+
'text' => '30 12 * * *'
69+
]),
70+
]),
71+
'encryption_config' => $encryptionConfig,
72+
]);
73+
$request = new CreateBackupScheduleRequest([
74+
'parent' => $databaseFullName,
75+
'backup_schedule_id' => $backupScheduleId,
76+
'backup_schedule' => $backupSchedule,
77+
]);
78+
79+
$created_backup_schedule = $databaseAdminClient->createBackupSchedule($request);
80+
81+
printf('Created backup scehedule %s' . PHP_EOL, $created_backup_schedule->getName());
82+
}
83+
// [END spanner_create_backup_schedule]
84+
85+
// The following 2 lines are only needed to run the samples
86+
require_once __DIR__ . '/../../testing/sample_helpers.php';
87+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_delete_backup_schedule]
27+
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
28+
use Google\Cloud\Spanner\Admin\Database\V1\DeleteBackupScheduleRequest;
29+
30+
/**
31+
* Delete a backup schedule.
32+
* Example:
33+
* ```
34+
* delete_backup_schedule($projectId, $instanceId, $databaseId, $backupScheduleId);
35+
* ```
36+
*
37+
* @param string $projectId The Google Cloud project ID.
38+
* @param string $instanceId The Spanner instance ID.
39+
* @param string $databaseId The Spanner database ID.
40+
* @param string $backupScheduleId The ID of the backup schedule to be created.
41+
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
42+
*/
43+
function delete_backup_schedule(
44+
string $projectId,
45+
string $instanceId,
46+
string $databaseId,
47+
string $backupScheduleId,
48+
): void {
49+
$databaseAdminClient = new DatabaseAdminClient();
50+
51+
$backupScheduleName = sprintf(
52+
'projects/%s/instances/%s/databases/%s/backupSchedules/%s',
53+
$projectId,
54+
$instanceId,
55+
$databaseId,
56+
$backupScheduleId
57+
);
58+
$request = new DeleteBackupScheduleRequest([
59+
'name' => strval($backupScheduleName),
60+
]);
61+
62+
$databaseAdminClient->deleteBackupSchedule($request);
63+
printf('Deleted backup scehedule %s' . PHP_EOL, $backupScheduleName);
64+
}
65+
// [END spanner_delete_backup_schedule]
66+
67+
// The following 2 lines are only needed to run the samples
68+
require_once __DIR__ . '/../../testing/sample_helpers.php';
69+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/get_backup_schedule.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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_get_backup_schedule]
27+
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
28+
use Google\Cloud\Spanner\Admin\Database\V1\GetBackupScheduleRequest;
29+
30+
/**
31+
* Get a backup schedule.
32+
* Example:
33+
* ```
34+
* get_backup_schedule($projectId, $instanceId, $databaseId, $backupScheduleId);
35+
* ```
36+
*
37+
* @param string $projectId The Google Cloud project ID.
38+
* @param string $instanceId The Spanner instance ID.
39+
* @param string $databaseId The Spanner database ID.
40+
* @param string $backupScheduleId The ID of the backup schedule to be created.
41+
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
42+
*/
43+
function get_backup_schedule(
44+
string $projectId,
45+
string $instanceId,
46+
string $databaseId,
47+
string $backupScheduleId,
48+
): void {
49+
$databaseAdminClient = new DatabaseAdminClient();
50+
51+
$backupScheduleName = sprintf(
52+
'projects/%s/instances/%s/databases/%s/backupSchedules/%s',
53+
$projectId,
54+
$instanceId,
55+
$databaseId,
56+
$backupScheduleId
57+
);
58+
$request = new GetBackupScheduleRequest([
59+
'name' => $backupScheduleName,
60+
]);
61+
62+
$backup_schedule = $databaseAdminClient->getBackupSchedule($request);
63+
64+
printf('Fetched backup scehedule %s' . PHP_EOL, $backup_schedule->getName());
65+
}
66+
// [END spanner_get_backup_schedule]
67+
68+
// The following 2 lines are only needed to run the samples
69+
require_once __DIR__ . '/../../testing/sample_helpers.php';
70+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/list_backup_schedules.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_list_backup_schedules]
27+
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
28+
use Google\Cloud\Spanner\Admin\Database\V1\ListBackupSchedulesRequest;
29+
30+
/**
31+
* Get list of all backup schedules for a given database.
32+
* Example:
33+
* ```
34+
* list_backup_schedules($projectId, $instanceId, $databaseId, $backupScheduleId);
35+
* ```
36+
*
37+
* @param string $projectId The Google Cloud project ID.
38+
* @param string $instanceId The Spanner instance ID.
39+
* @param string $databaseId The Spanner database ID.
40+
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
41+
*/
42+
function list_backup_schedules(
43+
string $projectId,
44+
string $instanceId,
45+
string $databaseId,
46+
): void {
47+
$databaseAdminClient = new DatabaseAdminClient();
48+
$databaseFullName = DatabaseAdminClient::databaseName($projectId, $instanceId, $databaseId);
49+
50+
$request = new ListBackupSchedulesRequest([
51+
'parent' => $databaseFullName,
52+
]);
53+
$backup_schedules = $databaseAdminClient->listBackupSchedules($request);
54+
55+
printf('Backup schedules for database %s' . PHP_EOL, $databaseFullName);
56+
foreach ($backup_schedules as $schedule) {
57+
printf('Backup schedule: %s' . PHP_EOL, $schedule->getName());
58+
}
59+
}
60+
// [END spanner_list_backup_schedules]
61+
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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_update_backup_schedule]
27+
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
28+
use Google\Cloud\Spanner\Admin\Database\V1\UpdateBackupScheduleRequest;
29+
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig;
30+
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig\EncryptionType;
31+
use Google\Cloud\Spanner\Admin\Database\V1\BackupSchedule;
32+
use Google\Cloud\Spanner\Admin\Database\V1\FullBackupSpec;
33+
use Google\Cloud\Spanner\Admin\Database\V1\BackupScheduleSpec;
34+
use Google\Cloud\Spanner\Admin\Database\V1\CrontabSpec;
35+
use Google\Protobuf\Duration;
36+
use Google\Protobuf\FieldMask;
37+
38+
/**
39+
* Update an existing backup schedule.
40+
* Example:
41+
* ```
42+
* update_backup_schedule($projectId, $instanceId, $databaseId, $backupScheduleId);
43+
* ```
44+
*
45+
* @param string $projectId The Google Cloud project ID.
46+
* @param string $instanceId The Spanner instance ID.
47+
* @param string $databaseId The Spanner database ID.
48+
* @param string $backupScheduleId The ID of the backup schedule to be created.
49+
* at https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.backupSchedules#BackupSchedule.FIELDS
50+
*/
51+
function update_backup_schedule(
52+
string $projectId,
53+
string $instanceId,
54+
string $databaseId,
55+
string $backupScheduleId,
56+
): void {
57+
$databaseAdminClient = new DatabaseAdminClient();
58+
59+
$encryptionConfig = new CreateBackupEncryptionConfig([
60+
'encryption_type' => EncryptionType::USE_DATABASE_ENCRYPTION,
61+
]);
62+
$backupScheduleName = sprintf(
63+
'projects/%s/instances/%s/databases/%s/backupSchedules/%s',
64+
$projectId,
65+
$instanceId,
66+
$databaseId,
67+
$backupScheduleId
68+
);
69+
$backupSchedule = new BackupSchedule([
70+
'name' => $backupScheduleName,
71+
'full_backup_spec' => new FullBackupSpec(),
72+
'retention_duration' => (new Duration())
73+
->setSeconds(48 * 60 * 60),
74+
'spec' => new BackupScheduleSpec([
75+
'cron_spec' => new CrontabSpec([
76+
'text' => '45 15 * * *'
77+
]),
78+
]),
79+
'encryption_config' => $encryptionConfig,
80+
]);
81+
$fieldMask = (new FieldMask())
82+
->setPaths([
83+
'retention_duration',
84+
'spec.cron_spec.text',
85+
'encryption_config',
86+
]);
87+
88+
$request = new UpdateBackupScheduleRequest([
89+
'backup_schedule' => $backupSchedule,
90+
'update_mask' => $fieldMask,
91+
]);
92+
93+
$updated_backup_schedule = $databaseAdminClient->updateBackupSchedule($request);
94+
95+
printf('Updated backup scehedule %s' . PHP_EOL, $updated_backup_schedule->getName());
96+
}
97+
// [END spanner_update_backup_schedule]
98+
99+
// The following 2 lines are only needed to run the samples
100+
require_once __DIR__ . '/../../testing/sample_helpers.php';
101+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)