Skip to content

Commit cb9ff6f

Browse files
aandreassaAayush Kadam
andauthored
chore(spanner): Add samples for Spanner Backup Schedule Apis (#1619)
Adds samples for the following APIs: 1. CreateBackupSchedule 2. GetBackupSchedule 3. ListBackupSchedules 4. UpdateBackupSchedule 5. DeleteBackupSchedule --------- Co-authored-by: Aayush Kadam <[email protected]>
1 parent 9ad9996 commit cb9ff6f

7 files changed

+290
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START spanner_create_backup_schedule_config]
16+
require "google/cloud/spanner/admin/database"
17+
require "google/cloud/spanner/admin/database/v1"
18+
19+
##
20+
# This is a snippet for showcasing how to create a schedule for backups.
21+
#
22+
# @param project_id [String] The ID of the Google Cloud project.
23+
# @param instance_id [String] The ID of the spanner instance.
24+
# @param database_id [String] The ID of the database.
25+
# @param backup_schedule_id [String] The ID of the backup schedule to be created.
26+
#
27+
def spanner_create_backup_schedule project_id:, instance_id:, database_id:, backup_schedule_id:
28+
client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id
29+
database_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}"
30+
31+
# For creating schedule for incremental backup use:
32+
# backup_spec = Google::Cloud::Spanner::Admin::Database::V1::IncrementalBackupSpec.new
33+
backup_spec = Google::Cloud::Spanner::Admin::Database::V1::FullBackupSpec.new
34+
retention_duration = Google::Protobuf::Duration.new seconds: 3600 * 24
35+
encryption_type = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupEncryptionConfig::EncryptionType::USE_DATABASE_ENCRYPTION
36+
encryption_config = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupEncryptionConfig.new(
37+
encryption_type: encryption_type
38+
)
39+
cron_spec = Google::Cloud::Spanner::Admin::Database::V1::CrontabSpec.new text: "30 12 * * *"
40+
backup_schedule_spec = Google::Cloud::Spanner::Admin::Database::V1::BackupScheduleSpec.new(
41+
cron_spec: cron_spec
42+
)
43+
44+
backup_schedule = Google::Cloud::Spanner::Admin::Database::V1::BackupSchedule.new(
45+
full_backup_spec: backup_spec,
46+
retention_duration: retention_duration,
47+
spec: backup_schedule_spec,
48+
encryption_config: encryption_config
49+
)
50+
51+
request = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupScheduleRequest.new(
52+
parent: database_name,
53+
backup_schedule_id: backup_schedule_id,
54+
backup_schedule: backup_schedule
55+
)
56+
created_backup_schedule = client.create_backup_schedule request
57+
puts "Created backup schedule for #{created_backup_schedule.name}"
58+
end
59+
60+
# [END spanner_create_backup_schedule_config]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START spanner_delete_backup_schedule_config]
16+
require "google/cloud/spanner/admin/database"
17+
require "google/cloud/spanner/admin/database/v1"
18+
19+
##
20+
# This is a snippet for showcasing how to delete a schedule for creating backups.
21+
#
22+
# @param project_id [String] The ID of the Google Cloud project.
23+
# @param instance_id [String] The ID of the spanner instance.
24+
# @param database_id [String] The ID of the database.
25+
# @param backup_schedule_id [String] The ID of the backup schedule to be created.
26+
#
27+
def spanner_delete_backup_schedule project_id:, instance_id:, database_id:, backup_schedule_id:
28+
client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id
29+
backup_schedule_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}/backupSchedules/#{backup_schedule_id}"
30+
31+
request = Google::Cloud::Spanner::Admin::Database::V1::DeleteBackupScheduleRequest.new name: backup_schedule_name
32+
33+
client.delete_backup_schedule request
34+
puts "Deleted backup schedule for #{backup_schedule_name}"
35+
end
36+
37+
# [END spanner_delete_backup_schedule_config]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START spanner_get_backup_schedule_config]
16+
require "google/cloud/spanner/admin/database"
17+
require "google/cloud/spanner/admin/database/v1"
18+
19+
##
20+
# This is a snippet for showcasing how to get a backup creation schedule.
21+
#
22+
# @param project_id [String] The ID of the Google Cloud project.
23+
# @param instance_id [String] The ID of the spanner instance.
24+
# @param database_id [String] The ID of the database.
25+
# @param backup_schedule_id [String] The ID of the backup schedule to be created.
26+
#
27+
def spanner_get_backup_schedule project_id:, instance_id:, database_id:, backup_schedule_id:
28+
client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id
29+
backup_schedule_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}/backupSchedules/#{backup_schedule_id}"
30+
31+
request = Google::Cloud::Spanner::Admin::Database::V1::GetBackupScheduleRequest.new name: backup_schedule_name
32+
33+
backup_schedule = client.get_backup_schedule request
34+
puts "Backup schedule: #{backup_schedule.name}"
35+
end
36+
37+
# [END spanner_get_backup_schedule_config]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START spanner_list_backup_schedules_config]
16+
require "google/cloud/spanner/admin/database"
17+
require "google/cloud/spanner/admin/database/v1"
18+
19+
##
20+
# This is a snippet for showcasing how to get list of schedules for creating backups of a database.
21+
#
22+
# @param project_id [String] The ID of the Google Cloud project.
23+
# @param instance_id [String] The ID of the spanner instance.
24+
# @param database_id [String] The ID of the database.
25+
#
26+
def spanner_list_backup_schedules project_id:, instance_id:, database_id:
27+
client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id
28+
database_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}"
29+
30+
request = Google::Cloud::Spanner::Admin::Database::V1::ListBackupSchedulesRequest.new parent: database_name
31+
32+
backup_schedules_list = client.list_backup_schedules request
33+
34+
puts "Backup schedules list for #{database_name}"
35+
backup_schedules_list.each { |backup_schedule| puts backup_schedule.name }
36+
end
37+
38+
# [END spanner_list_backup_schedules_config]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START spanner_update_backup_schedule_config]
16+
require "google/cloud/spanner/admin/database"
17+
require "google/cloud/spanner/admin/database/v1"
18+
19+
##
20+
# This is a snippet for showcasing how to create a schedule for backups.
21+
#
22+
# @param project_id [String] The ID of the Google Cloud project.
23+
# @param instance_id [String] The ID of the spanner instance.
24+
# @param database_id [String] The ID of the database.
25+
# @param backup_schedule_id [String] The ID of the backup schedule to be created.
26+
#
27+
def spanner_update_backup_schedule project_id:, instance_id:, database_id:, backup_schedule_id:
28+
client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id
29+
backup_schedule_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}/backupSchedules/#{backup_schedule_id}"
30+
retention_duration = Google::Protobuf::Duration.new seconds: 3600 * 24
31+
encryption_type = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupEncryptionConfig::EncryptionType::GOOGLE_DEFAULT_ENCRYPTION
32+
encryption_config = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupEncryptionConfig.new encryption_type: encryption_type
33+
cron_spec = Google::Cloud::Spanner::Admin::Database::V1::CrontabSpec.new text: "45 10 * * *"
34+
backup_schedule_spec = Google::Cloud::Spanner::Admin::Database::V1::BackupScheduleSpec.new cron_spec: cron_spec
35+
36+
backup_schedule = Google::Cloud::Spanner::Admin::Database::V1::BackupSchedule.new(
37+
name: backup_schedule_name,
38+
retention_duration: retention_duration,
39+
spec: backup_schedule_spec,
40+
encryption_config: encryption_config
41+
)
42+
field_mask = Google::Protobuf::FieldMask.new paths: ["retention_duration", "spec.cron_spec.text", "encryption_config"]
43+
44+
request = Google::Cloud::Spanner::Admin::Database::V1::UpdateBackupScheduleRequest.new(
45+
backup_schedule: backup_schedule,
46+
update_mask: field_mask
47+
)
48+
49+
updated_backup_schedule = client.update_backup_schedule request
50+
puts "Updated backup schedule for #{updated_backup_schedule.name}"
51+
end
52+
53+
# [END spanner_update_backup_schedule_config]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require_relative "./spec_helper"
16+
require_relative "../spanner_create_backup_schedule_config"
17+
require_relative "../spanner_delete_backup_schedule_config"
18+
require_relative "../spanner_get_backup_schedule_config"
19+
require_relative "../spanner_list_backup_schedules_config"
20+
require_relative "../spanner_update_backup_schedule_config"
21+
22+
describe "Spanner schedule for backups:" do
23+
before :all do
24+
create_test_database @database_id
25+
end
26+
27+
after :all do
28+
cleanup_database_resources
29+
end
30+
example "Create backup schedule" do
31+
capture do
32+
spanner_create_backup_schedule project_id: @project_id, instance_id: @instance_id, database_id: @database_id, backup_schedule_id: @backup_schedule_id
33+
end
34+
expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}/backupSchedules/#{@backup_schedule_id}"
35+
end
36+
37+
example "Get backup schedule" do
38+
capture do
39+
spanner_get_backup_schedule project_id: @project_id, instance_id: @instance_id, database_id: @database_id, backup_schedule_id: @backup_schedule_id
40+
end
41+
expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}/backupSchedules/#{@backup_schedule_id}"
42+
end
43+
44+
example "List backup schedules" do
45+
capture do
46+
spanner_list_backup_schedules project_id: @project_id, instance_id: @instance_id, database_id: @database_id
47+
end
48+
expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}"
49+
end
50+
51+
example "Update backup schedules" do
52+
capture do
53+
spanner_update_backup_schedule project_id: @project_id, instance_id: @instance_id, database_id: @database_id, backup_schedule_id: @backup_schedule_id
54+
end
55+
expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}/backupSchedules/#{@backup_schedule_id}"
56+
end
57+
58+
example "Delete backup schedule" do
59+
capture do
60+
spanner_delete_backup_schedule project_id: @project_id, instance_id: @instance_id, database_id: @database_id, backup_schedule_id: @backup_schedule_id
61+
end
62+
expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}/backupSchedules/#{@backup_schedule_id}"
63+
end
64+
end

spanner/spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
@database_id = "test_db_#{seed}"
3131
@backup_id = "test_bu_#{seed}"
3232
@copied_backup_id = "test_cbu_#{seed}"
33+
@backup_schedule_id = "test_schedule_#{seed}"
3334
@restored_database_id = "restored_db_#{seed}"
3435
@spanner = Google::Cloud::Spanner.new project: @project_id
3536
@instance = @spanner.instance @instance_id

0 commit comments

Comments
 (0)