use of com.google.spanner.admin.database.v1.CreateBackupMetadata in project java-spanner by googleapis.
the class ITBackupTest method testCancelBackupOperation.
private void testCancelBackupOperation(Database database) throws InterruptedException, ExecutionException {
Timestamp expireTime = afterDays(7);
String backupId = testHelper.getUniqueBackupId();
logger.info(String.format("Starting to create backup %s", backupId));
OperationFuture<Backup, CreateBackupMetadata> op = dbAdminClient.createBackup(instanceId, backupId, database.getId().getDatabase(), expireTime);
backups.add(backupId);
// Cancel the backup operation.
logger.info(String.format("Cancelling the creation of backup %s", backupId));
dbAdminClient.cancelOperation(op.getName());
logger.info("Fetching backup operations");
boolean operationFound = false;
for (Operation operation : dbAdminClient.listBackupOperations(instanceId, Options.filter(String.format("name:%s", op.getName()))).iterateAll()) {
assertEquals(Code.CANCELLED.value(), operation.getError().getCode());
operationFound = true;
}
assertTrue(operationFound);
logger.info("Finished cancel test");
}
use of com.google.spanner.admin.database.v1.CreateBackupMetadata in project java-spanner by googleapis.
the class CreateBackupWithEncryptionKey method createBackupWithEncryptionKey.
static Void createBackupWithEncryptionKey(DatabaseAdminClient adminClient, String projectId, String instanceId, String databaseId, String backupId, String kmsKeyName) throws InterruptedException {
// Set expire time to 14 days from now.
final Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
final Backup backupToCreate = adminClient.newBackupBuilder(BackupId.of(projectId, instanceId, backupId)).setDatabase(DatabaseId.of(projectId, instanceId, databaseId)).setExpireTime(expireTime).setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(kmsKeyName)).build();
final OperationFuture<Backup, CreateBackupMetadata> operation = adminClient.createBackup(backupToCreate);
Backup backup;
try {
System.out.println("Waiting for operation to complete...");
backup = operation.get(1200, TimeUnit.SECONDS);
} catch (ExecutionException e) {
// If the operation failed during execution, expose the cause.
throw SpannerExceptionFactory.asSpannerException(e.getCause());
} catch (InterruptedException e) {
// and the thread is interrupted, either before or during the activity.
throw SpannerExceptionFactory.propagateInterrupt(e);
} catch (TimeoutException e) {
// If the operation timed out propagates the timeout
throw SpannerExceptionFactory.propagateTimeout(e);
}
System.out.printf("Backup %s of size %d bytes was created at %s using encryption key %s%n", backup.getId().getName(), backup.getSize(), LocalDateTime.ofEpochSecond(backup.getProto().getCreateTime().getSeconds(), backup.getProto().getCreateTime().getNanos(), OffsetDateTime.now().getOffset()), kmsKeyName);
return null;
}
use of com.google.spanner.admin.database.v1.CreateBackupMetadata in project java-spanner by googleapis.
the class SpannerSample method listBackupOperations.
// [END spanner_cancel_backup_create]
// [START spanner_list_backup_operations]
static void listBackupOperations(InstanceAdminClient instanceAdminClient, DatabaseId databaseId) {
Instance instance = instanceAdminClient.getInstance(databaseId.getInstanceId().getInstance());
// Get create backup operations for the sample database.
Timestamp last24Hours = Timestamp.ofTimeSecondsAndNanos(TimeUnit.SECONDS.convert(TimeUnit.HOURS.convert(Timestamp.now().getSeconds(), TimeUnit.SECONDS) - 24, TimeUnit.HOURS), 0);
String filter = String.format("(metadata.database:%s) AND " + "(metadata.@type:type.googleapis.com/" + "google.spanner.admin.database.v1.CreateBackupMetadata) AND " + "(metadata.progress.start_time > \"%s\")", databaseId.getName(), last24Hours);
Page<Operation> operations = instance.listBackupOperations(Options.filter(filter));
for (Operation op : operations.iterateAll()) {
try {
CreateBackupMetadata metadata = op.getMetadata().unpack(CreateBackupMetadata.class);
System.out.println(String.format("Backup %s on database %s pending: %d%% complete", metadata.getName(), metadata.getDatabase(), metadata.getProgress().getProgressPercent()));
} catch (InvalidProtocolBufferException e) {
// The returned operation does not contain CreateBackupMetadata.
System.err.println(e.getMessage());
}
}
}
Aggregations