-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Revert "removing samples" This reverts commit b06a3df. * removing samples * changes to samples * linting * changes as per comments * Update samples/snippets/src/main/java/com/example/spanner/SpannerSample.java Co-authored-by: Knut Olav Løite <[email protected]> * Update samples/snippets/src/main/java/com/example/spanner/CopyBackupSample.java Co-authored-by: Knut Olav Løite <[email protected]> * don't merge * Revert "don't merge" This reverts commit cdf4d17. * linting * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * linting * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Knut Olav Løite <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
18ce38a
commit 787ccad
Showing
4 changed files
with
157 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
samples/snippets/src/main/java/com/example/spanner/CopyBackupSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2022 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_copy_backup] | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.Timestamp; | ||
import com.google.cloud.spanner.Backup; | ||
import com.google.cloud.spanner.BackupId; | ||
import com.google.cloud.spanner.DatabaseAdminClient; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerException; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.spanner.admin.database.v1.CopyBackupMetadata; | ||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class CopyBackupSample { | ||
static void copyBackup() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String sourceBackupId = "my-backup"; | ||
String destinationBackupId = "my-destination-backup"; | ||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient(); | ||
copyBackup(databaseAdminClient, projectId, instanceId, sourceBackupId, destinationBackupId); | ||
} | ||
} | ||
|
||
static void copyBackup( | ||
DatabaseAdminClient databaseAdminClient, | ||
String projectId, | ||
String instanceId, | ||
String sourceBackupId, | ||
String destinationBackupId) { | ||
|
||
Timestamp expireTime = | ||
Timestamp.ofTimeMicroseconds( | ||
TimeUnit.MICROSECONDS.convert( | ||
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), | ||
TimeUnit.MILLISECONDS)); | ||
// Creates a copy of an existing backup. | ||
Backup destinationBackup = | ||
databaseAdminClient | ||
.newBackupBuilder(BackupId.of(projectId, instanceId, destinationBackupId)) | ||
.setExpireTime(expireTime) | ||
.build(); | ||
|
||
// Initiate the request which returns an OperationFuture. | ||
System.out.println("Copying backup [" + destinationBackup.getId() + "]..."); | ||
OperationFuture<Backup, CopyBackupMetadata> operation = | ||
databaseAdminClient.copyBackup( | ||
BackupId.of(projectId, instanceId, sourceBackupId), destinationBackup); | ||
try { | ||
// Wait for the backup operation to complete. | ||
destinationBackup = operation.get(); | ||
System.out.println("Copied backup [" + destinationBackup.getId() + "]"); | ||
} catch (ExecutionException e) { | ||
throw (SpannerException) e.getCause(); | ||
} catch (InterruptedException e) { | ||
throw SpannerExceptionFactory.propagateInterrupt(e); | ||
} | ||
// Load the metadata of the new backup from the server. | ||
destinationBackup = destinationBackup.reload(); | ||
System.out.println( | ||
String.format( | ||
"Backup %s of size %d bytes was copied at %s for version of database at %s", | ||
destinationBackup.getId().getName(), | ||
destinationBackup.getSize(), | ||
LocalDateTime.ofEpochSecond( | ||
destinationBackup.getProto().getCreateTime().getSeconds(), | ||
destinationBackup.getProto().getCreateTime().getNanos(), | ||
OffsetDateTime.now().getOffset()), | ||
LocalDateTime.ofEpochSecond( | ||
destinationBackup.getProto().getVersionTime().getSeconds(), | ||
destinationBackup.getProto().getVersionTime().getNanos(), | ||
OffsetDateTime.now().getOffset()))); | ||
return; | ||
} | ||
} | ||
// [END spanner_copy_backup] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters