Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Apr 7, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathRestoreBackupWithEncryptionKey.java
More file actions
Latest commit
91 lines (82 loc) · 3.75 KB
/
Copy pathRestoreBackupWithEncryptionKey.java
File metadata and controls
91 lines (82 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
packagecom.example.spanner;
// [START spanner_restore_backup_with_encryption_key]
importstaticcom.google.spanner.admin.database.v1.RestoreDatabaseEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION;
importcom.google.cloud.spanner.Spanner;
importcom.google.cloud.spanner.SpannerExceptionFactory;
importcom.google.cloud.spanner.SpannerOptions;
importcom.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
importcom.google.spanner.admin.database.v1.BackupName;
importcom.google.spanner.admin.database.v1.Database;
importcom.google.spanner.admin.database.v1.InstanceName;
importcom.google.spanner.admin.database.v1.RestoreDatabaseEncryptionConfig;
importcom.google.spanner.admin.database.v1.RestoreDatabaseRequest;
importjava.util.concurrent.ExecutionException;
publicclassRestoreBackupWithEncryptionKey {
staticvoidrestoreBackupWithEncryptionKey() {
// TODO(developer): Replace these variables before running the sample.
StringprojectId = "my-project";
StringinstanceId = "my-instance";
StringdatabaseId = "my-database";
StringbackupId = "my-backup";
StringkmsKeyName =
"projects/" + projectId + "/locations/<location>/keyRings/<keyRing>/cryptoKeys/<keyId>";
try (Spannerspanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClientadminClient = spanner.createDatabaseAdminClient()) {
restoreBackupWithEncryptionKey(
adminClient,
projectId,
instanceId,
backupId,
databaseId,
kmsKeyName);
}
}
staticVoidrestoreBackupWithEncryptionKey(DatabaseAdminClientadminClient,
StringprojectId, StringinstanceId, StringbackupId, StringrestoreId, StringkmsKeyName) {
RestoreDatabaseRequestrequest =
RestoreDatabaseRequest.newBuilder()
.setParent(InstanceName.of(projectId, instanceId).toString())
.setDatabaseId(restoreId)
.setBackup(BackupName.of(projectId, instanceId, backupId).toString())
.setEncryptionConfig(RestoreDatabaseEncryptionConfig.newBuilder()
.setEncryptionType(CUSTOMER_MANAGED_ENCRYPTION).setKmsKeyName(kmsKeyName)).build();
Databasedatabase;
try {
System.out.println("Waiting for operation to complete...");
database = adminClient.restoreDatabaseAsync(request).get();
;
} catch (ExecutionExceptione) {
// If the operation failed during execution, expose the cause.
throwSpannerExceptionFactory.asSpannerException(e.getCause());
} catch (InterruptedExceptione) {
// Throw when a thread is waiting, sleeping, or otherwise occupied,
// and the thread is interrupted, either before or during the activity.
throwSpannerExceptionFactory.propagateInterrupt(e);
}
System.out.printf(
"Database %s restored to %s from backup %s using encryption key %s%n",
database.getRestoreInfo().getBackupInfo().getSourceDatabase(),
database.getName(),
database.getRestoreInfo().getBackupInfo().getBackup(),
database.getEncryptionConfig().getKmsKeyName()
);
returnnull;
}
}
// [END spanner_restore_backup_with_encryption_key]