Skip to content

feat: Copy backup samples #1802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 22, 2022
Prev Previous commit
Next Next commit
changes as per comments
  • Loading branch information
asthamohta committed Apr 22, 2022
commit 1c76896612e5d56f501a3983edd5d3d4311e14bc
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,6 @@
import java.util.concurrent.TimeUnit;

public class CopyBackupSample {
public static void main(String[] args) {
if (args.length != 4) {
throw new IllegalArgumentException(
"Invalid number of arguments. "
+ "Usage: CopyBackupSample "
+ "<project-id> <instance-id> <source-backup-id> <destination-backup-id>");
}
String projectId = args[0];
String instanceId = args[1];
String sourceBackupId = args[2];
String destinationBackupId = args[3];

try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient();
copyBackup(databaseAdminClient, projectId, instanceId, sourceBackupId, destinationBackupId);
}
}

static void copyBackup() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
Expand Down Expand Up @@ -87,12 +68,12 @@ static void copyBackup(

// Initiate the request which returns an OperationFuture.
System.out.println("Copying backup [" + destinationBackup.getId() + "]...");
OperationFuture<Backup, CopyBackupMetadata> op =
OperationFuture<Backup, CopyBackupMetadata> operation =
databaseAdminClient.copyBackup(
BackupId.of(projectId, instanceId, sourceBackupId), destinationBackup);
try {
// Wait for the backup operation to complete.
destinationBackup = op.get();
destinationBackup = operation.get();
System.out.println("Copied backup [" + destinationBackup.getId() + "]");
} catch (ExecutionException e) {
throw (SpannerException) e.getCause();
Expand All @@ -114,7 +95,7 @@ static void copyBackup(
destinationBackup.getProto().getVersionTime().getSeconds(),
destinationBackup.getProto().getVersionTime().getNanos(),
OffsetDateTime.now().getOffset())));
return null;
return;
}
}
// [END spanner_copy_backup]
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public class SpannerSample {

/** Class to contain singer sample data. */
static class Singer {

final long singerId;
final String firstName;
final String lastName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.example.spanner;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertTrue;

import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Backup;
Expand Down Expand Up @@ -328,9 +329,8 @@ public void testSample() throws Exception {
"Backup %s on database %s pending:",
backupId.getName(),
dbId.getName()));
assertThat(out).contains(
"Copy Backup Operations"
);
assertTrue("Out does not contain copy backup operations", out.contains(
"Copy Backup Operations"));
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
assertThat(e.getMessage()).contains("Cannot evaluate filter expression");
Expand Down