|
| 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] |
0 commit comments