Skip to content

Commit b542470

Browse files
committed
add sample for CopyBackup with multi region encryption keys
1 parent 571a47e commit b542470

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2024 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_copy_backup_with_MR_CMEK]
20+
21+
import com.google.cloud.Timestamp;
22+
import com.google.cloud.spanner.Spanner;
23+
import com.google.cloud.spanner.SpannerException;
24+
import com.google.cloud.spanner.SpannerExceptionFactory;
25+
import com.google.cloud.spanner.SpannerOptions;
26+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
27+
import com.google.common.collect.ImmutableList;
28+
import com.google.spanner.admin.database.v1.Backup;
29+
import com.google.spanner.admin.database.v1.BackupName;
30+
import com.google.spanner.admin.database.v1.CopyBackupEncryptionConfig;
31+
import com.google.spanner.admin.database.v1.CopyBackupEncryptionConfig.EncryptionType;
32+
import com.google.spanner.admin.database.v1.CopyBackupRequest;
33+
import com.google.spanner.admin.database.v1.InstanceName;
34+
import java.time.Instant;
35+
import java.time.OffsetDateTime;
36+
import java.time.ZoneId;
37+
import java.util.concurrent.ExecutionException;
38+
import java.util.concurrent.TimeUnit;
39+
40+
public class CopyBackupWithMultiRegionEncryptionKey {
41+
42+
static void copyBackupWithMultiRegionEncryptionKey() {
43+
// TODO(developer): Replace these variables before running the sample.
44+
String projectId = "my-project";
45+
String instanceId = "my-instance";
46+
String sourceBackupId = "my-backup";
47+
String destinationBackupId = "my-destination-backup";
48+
String[] kmsKeyNames =
49+
new String[] {
50+
"projects/" + projectId + "/locations/<location1>/keyRings/<keyRing>/cryptoKeys/<keyId>",
51+
"projects/" + projectId + "/locations/<location2>/keyRings/<keyRing>/cryptoKeys/<keyId>",
52+
"projects/" + projectId + "/locations/<location3>/keyRings/<keyRing>/cryptoKeys/<keyId>"
53+
};
54+
55+
try (Spanner spanner =
56+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
57+
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
58+
copyBackupWithMultiRegionEncryptionKey(
59+
databaseAdminClient,
60+
projectId,
61+
instanceId,
62+
sourceBackupId,
63+
destinationBackupId,
64+
kmsKeyNames);
65+
}
66+
}
67+
68+
static void copyBackupWithMultiRegionEncryptionKey(
69+
DatabaseAdminClient databaseAdminClient,
70+
String projectId,
71+
String instanceId,
72+
String sourceBackupId,
73+
String destinationBackupId,
74+
String[] kmsKeyNames) {
75+
76+
Timestamp expireTime =
77+
Timestamp.ofTimeMicroseconds(
78+
TimeUnit.MICROSECONDS.convert(
79+
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
80+
81+
// Initiate the request which returns an OperationFuture.
82+
System.out.println("Copying backup [" + destinationBackupId + "]...");
83+
CopyBackupRequest request =
84+
CopyBackupRequest.newBuilder()
85+
.setParent(InstanceName.of(projectId, instanceId).toString())
86+
.setBackupId(destinationBackupId)
87+
.setSourceBackup(BackupName.of(projectId, instanceId, sourceBackupId).toString())
88+
.setExpireTime(expireTime.toProto())
89+
.setEncryptionConfig(
90+
CopyBackupEncryptionConfig.newBuilder()
91+
.setEncryptionType(EncryptionType.CUSTOMER_MANAGED_ENCRYPTION)
92+
.addAllKmsKeyNames(ImmutableList.copyOf(kmsKeyNames))
93+
.build())
94+
.build();
95+
Backup destinationBackup;
96+
try {
97+
// Creates a copy of an existing backup.
98+
// Wait for the backup operation to complete.
99+
destinationBackup = databaseAdminClient.copyBackupAsync(request).get();
100+
System.out.println("Copied backup [" + destinationBackup.getName() + "]");
101+
} catch (ExecutionException e) {
102+
throw (SpannerException) e.getCause();
103+
} catch (InterruptedException e) {
104+
throw SpannerExceptionFactory.propagateInterrupt(e);
105+
}
106+
// Load the metadata of the new backup from the server.
107+
destinationBackup = databaseAdminClient.getBackup(destinationBackup.getName());
108+
System.out.println(
109+
String.format(
110+
"Backup %s of size %d bytes was copied at %s for version of database at %s",
111+
destinationBackup.getName(),
112+
destinationBackup.getSizeBytes(),
113+
OffsetDateTime.ofInstant(
114+
Instant.ofEpochSecond(
115+
destinationBackup.getCreateTime().getSeconds(),
116+
destinationBackup.getCreateTime().getNanos()),
117+
ZoneId.systemDefault()),
118+
OffsetDateTime.ofInstant(
119+
Instant.ofEpochSecond(
120+
destinationBackup.getVersionTime().getSeconds(),
121+
destinationBackup.getVersionTime().getNanos()),
122+
ZoneId.systemDefault())));
123+
}
124+
}
125+
// [END spanner_copy_backup_with_MR_CMEK]

0 commit comments

Comments
 (0)