0
# Backup and Restore Operations
1
2
Complete backup and restore functionality for metastore services including scheduled backups, point-in-time recovery, and cross-region backup management for disaster recovery scenarios. Supports automated backup policies and granular restore options.
3
4
## Capabilities
5
6
### List Backups
7
8
Retrieve all backups for a metastore service with filtering and pagination support.
9
10
```python { .api }
11
def list_backups(
12
self,
13
request: Optional[ListBackupsRequest] = None,
14
*,
15
parent: Optional[str] = None,
16
retry: OptionalRetry = gapic_v1.method.DEFAULT,
17
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
18
metadata: Sequence[Tuple[str, str]] = ()
19
) -> pagers.ListBackupsPager:
20
"""
21
Lists backups in a service.
22
23
Args:
24
request: The request object containing list parameters
25
parent: Required. The relative resource name of the service
26
Format: projects/{project_id}/locations/{location_id}/services/{service_id}
27
retry: Retry configuration for the request
28
timeout: Request timeout in seconds
29
metadata: Additional metadata for the request
30
31
Returns:
32
ListBackupsPager: Pageable list of backups
33
34
Raises:
35
google.api_core.exceptions.GoogleAPICallError: If the request fails
36
"""
37
```
38
39
Usage example:
40
41
```python
42
from google.cloud import metastore
43
44
client = metastore.DataprocMetastoreClient()
45
parent = "projects/my-project/locations/us-central1/services/my-metastore"
46
47
# List all backups
48
for backup in client.list_backups(parent=parent):
49
print(f"Backup: {backup.name}")
50
print(f"State: {backup.state.name}")
51
print(f"Created: {backup.create_time}")
52
print(f"Service revision: {backup.service_revision}")
53
54
# With filtering
55
request = metastore.ListBackupsRequest(
56
parent=parent,
57
filter="state=ACTIVE",
58
order_by="create_time desc"
59
)
60
```
61
62
### Get Backup
63
64
Retrieve detailed information about a specific backup including metadata and restore capabilities.
65
66
```python { .api }
67
def get_backup(
68
self,
69
request: Optional[GetBackupRequest] = None,
70
*,
71
name: Optional[str] = None,
72
retry: OptionalRetry = gapic_v1.method.DEFAULT,
73
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
74
metadata: Sequence[Tuple[str, str]] = ()
75
) -> Backup:
76
"""
77
Gets details of a single backup.
78
79
Args:
80
request: The request object
81
name: Required. The relative resource name of the backup
82
Format: projects/{project_id}/locations/{location_id}/services/{service_id}/backups/{backup_id}
83
retry: Retry configuration
84
timeout: Request timeout in seconds
85
metadata: Additional metadata
86
87
Returns:
88
Backup: The backup resource
89
90
Raises:
91
google.api_core.exceptions.NotFound: If the backup doesn't exist
92
"""
93
```
94
95
### Create Backup
96
97
Create a new backup of a metastore service with optional description and metadata.
98
99
```python { .api }
100
def create_backup(
101
self,
102
request: Optional[CreateBackupRequest] = None,
103
*,
104
parent: Optional[str] = None,
105
backup: Optional[Backup] = None,
106
backup_id: Optional[str] = None,
107
retry: OptionalRetry = gapic_v1.method.DEFAULT,
108
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
109
metadata: Sequence[Tuple[str, str]] = ()
110
) -> operation.Operation:
111
"""
112
Creates a metastore service backup.
113
114
Args:
115
request: The request object
116
parent: Required. The relative resource name of the service
117
backup: Required. The backup configuration
118
backup_id: Required. The ID to use for the backup
119
retry: Retry configuration
120
timeout: Request timeout in seconds
121
metadata: Additional metadata
122
123
Returns:
124
Operation: Long-running operation for backup creation
125
126
Raises:
127
google.api_core.exceptions.AlreadyExists: If backup_id already exists
128
google.api_core.exceptions.FailedPrecondition: If service cannot be backed up
129
"""
130
```
131
132
Usage example:
133
134
```python
135
from google.cloud import metastore
136
137
client = metastore.DataprocMetastoreClient()
138
139
# Create a backup
140
backup_config = metastore.Backup(
141
description="Weekly automated backup for disaster recovery"
142
)
143
144
operation = client.create_backup(
145
parent="projects/my-project/locations/us-central1/services/my-metastore",
146
backup_id="weekly-backup-2024-01-15",
147
backup=backup_config
148
)
149
150
# Wait for completion (typically 5-15 minutes)
151
backup = operation.result(timeout=900)
152
print(f"Backup created: {backup.name}")
153
print(f"Service revision: {backup.service_revision}")
154
```
155
156
### Delete Backup
157
158
Delete a backup permanently. This operation cannot be undone.
159
160
```python { .api }
161
def delete_backup(
162
self,
163
request: Optional[DeleteBackupRequest] = None,
164
*,
165
name: Optional[str] = None,
166
retry: OptionalRetry = gapic_v1.method.DEFAULT,
167
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
168
metadata: Sequence[Tuple[str, str]] = ()
169
) -> operation.Operation:
170
"""
171
Deletes a single backup.
172
173
Args:
174
request: The request object
175
name: Required. The relative resource name of the backup to delete
176
retry: Retry configuration
177
timeout: Request timeout in seconds
178
metadata: Additional metadata
179
180
Returns:
181
Operation: Long-running operation for backup deletion
182
183
Raises:
184
google.api_core.exceptions.NotFound: If the backup doesn't exist
185
google.api_core.exceptions.FailedPrecondition: If backup cannot be deleted
186
"""
187
```
188
189
### Restore Service
190
191
Restore a metastore service from a backup with options for full or metadata-only restoration.
192
193
```python { .api }
194
def restore_service(
195
self,
196
request: Optional[RestoreServiceRequest] = None,
197
*,
198
service: Optional[str] = None,
199
backup: Optional[str] = None,
200
retry: OptionalRetry = gapic_v1.method.DEFAULT,
201
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
202
metadata: Sequence[Tuple[str, str]] = ()
203
) -> operation.Operation:
204
"""
205
Restores a service from a backup.
206
207
Args:
208
request: The request object
209
service: Required. The relative resource name of the service to restore
210
backup: Required. The relative resource name of the backup to restore from
211
retry: Retry configuration
212
timeout: Request timeout in seconds
213
metadata: Additional metadata
214
215
Returns:
216
Operation: Long-running operation for service restoration
217
218
Raises:
219
google.api_core.exceptions.NotFound: If service or backup doesn't exist
220
google.api_core.exceptions.FailedPrecondition: If restore cannot be performed
221
"""
222
```
223
224
Usage example:
225
226
```python
227
from google.cloud import metastore
228
229
client = metastore.DataprocMetastoreClient()
230
231
# Restore from backup
232
restore_request = metastore.RestoreServiceRequest(
233
service="projects/my-project/locations/us-central1/services/my-metastore",
234
backup="projects/my-project/locations/us-central1/services/my-metastore/backups/backup-id",
235
restore_type=metastore.Restore.RestoreType.FULL
236
)
237
238
operation = client.restore_service(request=restore_request)
239
240
# Monitor restoration progress
241
print("Starting restoration...")
242
result = operation.result(timeout=3600) # Can take up to 1 hour
243
print(f"Service restored successfully")
244
```
245
246
## Core Types
247
248
### Backup Resource
249
250
```python { .api }
251
class Backup:
252
name: str
253
create_time: timestamp_pb2.Timestamp
254
end_time: timestamp_pb2.Timestamp
255
state: State
256
service_revision: Service
257
description: str
258
restoring_services: List[str]
259
260
class State(enum.Enum):
261
STATE_UNSPECIFIED = 0
262
CREATING = 1
263
DELETING = 2
264
ACTIVE = 3
265
FAILED = 4
266
RESTORING = 5
267
```
268
269
### Restore Configuration
270
271
```python { .api }
272
class Restore:
273
start_time: timestamp_pb2.Timestamp
274
end_time: timestamp_pb2.Timestamp
275
state: State
276
backup: str
277
type: RestoreType
278
details: str
279
280
class State(enum.Enum):
281
STATE_UNSPECIFIED = 0
282
RUNNING = 1
283
SUCCEEDED = 2
284
FAILED = 3
285
CANCELLED = 4
286
287
class RestoreType(enum.Enum):
288
RESTORE_TYPE_UNSPECIFIED = 0
289
FULL = 1
290
METADATA_ONLY = 2
291
```
292
293
### Request/Response Types
294
295
```python { .api }
296
class ListBackupsRequest:
297
parent: str
298
page_size: int
299
page_token: str
300
filter: str
301
order_by: str
302
303
class ListBackupsResponse:
304
backups: List[Backup]
305
next_page_token: str
306
unreachable: List[str]
307
308
class GetBackupRequest:
309
name: str
310
311
class CreateBackupRequest:
312
parent: str
313
backup_id: str
314
backup: Backup
315
request_id: str
316
317
class DeleteBackupRequest:
318
name: str
319
request_id: str
320
321
class RestoreServiceRequest:
322
service: str
323
backup: str
324
restore_type: Restore.RestoreType
325
request_id: str
326
```
327
328
## Usage Patterns
329
330
### Automated Backup Strategy
331
332
```python
333
import schedule
334
import time
335
from datetime import datetime
336
from google.cloud import metastore
337
338
def create_daily_backup():
339
client = metastore.DataprocMetastoreClient()
340
341
# Generate timestamp-based backup ID
342
backup_id = f"daily-backup-{datetime.now().strftime('%Y%m%d-%H%M%S')}"
343
344
backup_config = metastore.Backup(
345
description=f"Automated daily backup created at {datetime.now()}"
346
)
347
348
operation = client.create_backup(
349
parent="projects/my-project/locations/us-central1/services/my-metastore",
350
backup_id=backup_id,
351
backup=backup_config
352
)
353
354
# Don't wait for completion in automated scenarios
355
print(f"Backup operation started: {operation.name}")
356
357
# Schedule daily backups at 2 AM
358
schedule.every().day.at("02:00").do(create_daily_backup)
359
```
360
361
### Disaster Recovery Workflow
362
363
```python
364
from google.cloud import metastore
365
import logging
366
367
def disaster_recovery_restore(backup_name: str, target_service: str):
368
"""
369
Perform disaster recovery restoration from backup.
370
"""
371
client = metastore.DataprocMetastoreClient()
372
373
try:
374
# Verify backup exists and is in ACTIVE state
375
backup = client.get_backup(name=backup_name)
376
if backup.state != metastore.Backup.State.ACTIVE:
377
raise ValueError(f"Backup {backup_name} is not in ACTIVE state")
378
379
# Start restoration
380
restore_request = metastore.RestoreServiceRequest(
381
service=target_service,
382
backup=backup_name,
383
restore_type=metastore.Restore.RestoreType.FULL
384
)
385
386
operation = client.restore_service(request=restore_request)
387
logging.info(f"Restoration started: {operation.name}")
388
389
# Wait for completion with progress logging
390
while not operation.done():
391
time.sleep(30)
392
logging.info("Restoration in progress...")
393
394
result = operation.result()
395
logging.info(f"Disaster recovery completed successfully")
396
return result
397
398
except Exception as e:
399
logging.error(f"Disaster recovery failed: {e}")
400
raise
401
```