Skip to content

Commit b1ea772

Browse files
authored
Merge branch 'main' into trace-annotation-SessionPools
2 parents eb9dd5a + b2e6762 commit b1ea772

File tree

2 files changed

+462
-0
lines changed

2 files changed

+462
-0
lines changed
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
# Copyright 2024 Google Inc. All Rights Reserved.
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+
"""
16+
This application demonstrates how to create and manage backup schedules using
17+
Cloud Spanner.
18+
"""
19+
20+
import argparse
21+
22+
from enum import Enum
23+
24+
25+
# [START spanner_create_full_backup_schedule]
26+
def create_full_backup_schedule(
27+
instance_id: str,
28+
database_id: str,
29+
schedule_id: str,
30+
) -> None:
31+
from datetime import timedelta
32+
from google.cloud import spanner
33+
from google.cloud.spanner_admin_database_v1.types import \
34+
backup_schedule as backup_schedule_pb
35+
from google.cloud.spanner_admin_database_v1.types import \
36+
CreateBackupEncryptionConfig, FullBackupSpec
37+
38+
client = spanner.Client()
39+
database_admin_api = client.database_admin_api
40+
41+
request = backup_schedule_pb.CreateBackupScheduleRequest(
42+
parent=database_admin_api.database_path(
43+
client.project,
44+
instance_id,
45+
database_id
46+
),
47+
backup_schedule_id=schedule_id,
48+
backup_schedule=backup_schedule_pb.BackupSchedule(
49+
spec=backup_schedule_pb.BackupScheduleSpec(
50+
cron_spec=backup_schedule_pb.CrontabSpec(
51+
text="30 12 * * *",
52+
),
53+
),
54+
retention_duration=timedelta(hours=24),
55+
encryption_config=CreateBackupEncryptionConfig(
56+
encryption_type=CreateBackupEncryptionConfig.EncryptionType.USE_DATABASE_ENCRYPTION,
57+
),
58+
full_backup_spec=FullBackupSpec(),
59+
),
60+
)
61+
62+
response = database_admin_api.create_backup_schedule(request)
63+
print(f"Created full backup schedule: {response}")
64+
65+
# [END spanner_create_full_backup_schedule]
66+
67+
68+
# [START spanner_create_incremental_backup_schedule]
69+
def create_incremental_backup_schedule(
70+
instance_id: str,
71+
database_id: str,
72+
schedule_id: str,
73+
) -> None:
74+
from datetime import timedelta
75+
from google.cloud import spanner
76+
from google.cloud.spanner_admin_database_v1.types import \
77+
backup_schedule as backup_schedule_pb
78+
from google.cloud.spanner_admin_database_v1.types import \
79+
CreateBackupEncryptionConfig, IncrementalBackupSpec
80+
81+
client = spanner.Client()
82+
database_admin_api = client.database_admin_api
83+
84+
request = backup_schedule_pb.CreateBackupScheduleRequest(
85+
parent=database_admin_api.database_path(
86+
client.project,
87+
instance_id,
88+
database_id
89+
),
90+
backup_schedule_id=schedule_id,
91+
backup_schedule=backup_schedule_pb.BackupSchedule(
92+
spec=backup_schedule_pb.BackupScheduleSpec(
93+
cron_spec=backup_schedule_pb.CrontabSpec(
94+
text="30 12 * * *",
95+
),
96+
),
97+
retention_duration=timedelta(hours=24),
98+
encryption_config=CreateBackupEncryptionConfig(
99+
encryption_type=CreateBackupEncryptionConfig.EncryptionType.GOOGLE_DEFAULT_ENCRYPTION,
100+
),
101+
incremental_backup_spec=IncrementalBackupSpec(),
102+
),
103+
)
104+
105+
response = database_admin_api.create_backup_schedule(request)
106+
print(f"Created incremental backup schedule: {response}")
107+
108+
# [END spanner_create_incremental_backup_schedule]
109+
110+
111+
# [START spanner_list_backup_schedules]
112+
def list_backup_schedules(instance_id: str, database_id: str) -> None:
113+
from google.cloud import spanner
114+
from google.cloud.spanner_admin_database_v1.types import \
115+
backup_schedule as backup_schedule_pb
116+
117+
client = spanner.Client()
118+
database_admin_api = client.database_admin_api
119+
120+
request = backup_schedule_pb.ListBackupSchedulesRequest(
121+
parent=database_admin_api.database_path(
122+
client.project,
123+
instance_id,
124+
database_id,
125+
),
126+
)
127+
128+
for backup_schedule in database_admin_api.list_backup_schedules(request):
129+
print(f"Backup schedule: {backup_schedule}")
130+
131+
# [END spanner_list_backup_schedules]
132+
133+
134+
# [START spanner_get_backup_schedule]
135+
def get_backup_schedule(
136+
instance_id: str,
137+
database_id: str,
138+
schedule_id: str,
139+
) -> None:
140+
from google.cloud import spanner
141+
from google.cloud.spanner_admin_database_v1.types import \
142+
backup_schedule as backup_schedule_pb
143+
144+
client = spanner.Client()
145+
database_admin_api = client.database_admin_api
146+
147+
request = backup_schedule_pb.GetBackupScheduleRequest(
148+
name=database_admin_api.backup_schedule_path(
149+
client.project,
150+
instance_id,
151+
database_id,
152+
schedule_id,
153+
),
154+
)
155+
156+
response = database_admin_api.get_backup_schedule(request)
157+
print(f"Backup schedule: {response}")
158+
159+
# [END spanner_get_backup_schedule]
160+
161+
162+
# [START spanner_update_backup_schedule]
163+
def update_backup_schedule(
164+
instance_id: str,
165+
database_id: str,
166+
schedule_id: str,
167+
) -> None:
168+
from datetime import timedelta
169+
from google.cloud import spanner
170+
from google.cloud.spanner_admin_database_v1.types import \
171+
backup_schedule as backup_schedule_pb
172+
from google.cloud.spanner_admin_database_v1.types import \
173+
CreateBackupEncryptionConfig
174+
from google.protobuf.field_mask_pb2 import FieldMask
175+
176+
client = spanner.Client()
177+
database_admin_api = client.database_admin_api
178+
179+
request = backup_schedule_pb.UpdateBackupScheduleRequest(
180+
backup_schedule=backup_schedule_pb.BackupSchedule(
181+
name=database_admin_api.backup_schedule_path(
182+
client.project,
183+
instance_id,
184+
database_id,
185+
schedule_id,
186+
),
187+
spec=backup_schedule_pb.BackupScheduleSpec(
188+
cron_spec=backup_schedule_pb.CrontabSpec(
189+
text="45 15 * * *",
190+
),
191+
),
192+
retention_duration=timedelta(hours=48),
193+
encryption_config=CreateBackupEncryptionConfig(
194+
encryption_type=CreateBackupEncryptionConfig.EncryptionType.USE_DATABASE_ENCRYPTION,
195+
),
196+
),
197+
update_mask=FieldMask(
198+
paths=[
199+
"spec.cron_spec.text",
200+
"retention_duration",
201+
"encryption_config",
202+
],
203+
),
204+
)
205+
206+
response = database_admin_api.update_backup_schedule(request)
207+
print(f"Updated backup schedule: {response}")
208+
209+
# [END spanner_update_backup_schedule]
210+
211+
212+
# [START spanner_delete_backup_schedule]
213+
def delete_backup_schedule(
214+
instance_id: str,
215+
database_id: str,
216+
schedule_id: str,
217+
) -> None:
218+
from google.cloud import spanner
219+
from google.cloud.spanner_admin_database_v1.types import \
220+
backup_schedule as backup_schedule_pb
221+
222+
client = spanner.Client()
223+
database_admin_api = client.database_admin_api
224+
225+
request = backup_schedule_pb.DeleteBackupScheduleRequest(
226+
name=database_admin_api.backup_schedule_path(
227+
client.project,
228+
instance_id,
229+
database_id,
230+
schedule_id,
231+
),
232+
)
233+
234+
database_admin_api.delete_backup_schedule(request)
235+
print("Deleted backup schedule")
236+
237+
# [END spanner_delete_backup_schedule]
238+
239+
240+
class Command(Enum):
241+
CREATE_FULL_BACKUP_SCHEDULE = "create-full-backup-schedule"
242+
CREATE_INCREMENTAL_BACKUP_SCHEDULE = "create-incremental-backup-schedule"
243+
LIST_BACKUP_SCHEDULES = "list-backup-schedules"
244+
GET_BACKUP_SCHEDULE = "get-backup-schedule"
245+
UPDATE_BACKUP_SCHEDULE = "update-backup-schedule"
246+
DELETE_BACKUP_SCHEDULE = "delete-backup-schedule"
247+
248+
def __str__(self):
249+
return self.value
250+
251+
252+
if __name__ == "__main__":
253+
parser = argparse.ArgumentParser(
254+
description=__doc__,
255+
formatter_class=argparse.RawDescriptionHelpFormatter,
256+
)
257+
parser.add_argument("--instance-id", required=True)
258+
parser.add_argument("--database-id", required=True)
259+
parser.add_argument("--schedule-id", required=False)
260+
parser.add_argument(
261+
"command",
262+
type=Command,
263+
choices=list(Command),
264+
)
265+
args = parser.parse_args()
266+
267+
if args.command == Command.CREATE_FULL_BACKUP_SCHEDULE:
268+
create_full_backup_schedule(
269+
args.instance_id,
270+
args.database_id,
271+
args.schedule_id,
272+
)
273+
elif args.command == Command.CREATE_INCREMENTAL_BACKUP_SCHEDULE:
274+
create_incremental_backup_schedule(
275+
args.instance_id,
276+
args.database_id,
277+
args.schedule_id,
278+
)
279+
elif args.command == Command.LIST_BACKUP_SCHEDULES:
280+
list_backup_schedules(
281+
args.instance_id,
282+
args.database_id,
283+
)
284+
elif args.command == Command.GET_BACKUP_SCHEDULE:
285+
get_backup_schedule(
286+
args.instance_id,
287+
args.database_id,
288+
args.schedule_id,
289+
)
290+
elif args.command == Command.UPDATE_BACKUP_SCHEDULE:
291+
update_backup_schedule(
292+
args.instance_id,
293+
args.database_id,
294+
args.schedule_id,
295+
)
296+
elif args.command == Command.DELETE_BACKUP_SCHEDULE:
297+
delete_backup_schedule(
298+
args.instance_id,
299+
args.database_id,
300+
args.schedule_id,
301+
)
302+
else:
303+
print(f"Unknown command: {args.command}")

0 commit comments

Comments
 (0)