use of com.google.cloud.spanner.Backup in project java-spanner by googleapis.
the class ITBackupTest method testUpdateBackup.
private void testUpdateBackup(Backup backup) {
// Update the expire time.
Timestamp tomorrow = afterDays(1);
backup = backup.toBuilder().setExpireTime(tomorrow).build();
logger.info(String.format("Updating expire time of backup %s to 1 week", backup.getId().getBackup()));
backup.updateExpireTime();
// Re-get the backup and ensure the expire time was updated.
logger.info(String.format("Reloading backup %s", backup.getId().getBackup()));
backup = backup.reload();
assertEquals(tomorrow, backup.getExpireTime());
// Try to set the expire time to 5 minutes in the future.
Timestamp in5Minutes = afterMinutes(5);
final Backup backupWithNewExpireTime = backup.toBuilder().setExpireTime(in5Minutes).build();
logger.info(String.format("Updating expire time of backup %s to 5 minutes", backup.getId().getBackup()));
SpannerException spannerException = assertThrows(SpannerException.class, backupWithNewExpireTime::updateExpireTime);
assertEquals(ErrorCode.INVALID_ARGUMENT, spannerException.getErrorCode());
// Re-get the backup and ensure the expire time is still in one week.
backup = backup.reload();
assertEquals(tomorrow, backup.getExpireTime());
}
use of com.google.cloud.spanner.Backup 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.cloud.spanner.Backup in project java-spanner by googleapis.
the class SpannerSample method listBackups.
// [END spanner_list_database_operations]
// [START spanner_list_backups]
static void listBackups(InstanceAdminClient instanceAdminClient, DatabaseId databaseId, BackupId backupId) {
Instance instance = instanceAdminClient.getInstance(databaseId.getInstanceId().getInstance());
// List all backups.
System.out.println("All backups:");
for (Backup backup : instance.listBackups().iterateAll()) {
System.out.println(backup);
}
// List all backups with a specific name.
System.out.println(String.format("All backups with backup name containing \"%s\":", backupId.getBackup()));
for (Backup backup : instance.listBackups(Options.filter(String.format("name:%s", backupId.getBackup()))).iterateAll()) {
System.out.println(backup);
}
// List all backups for databases whose name contains a certain text.
System.out.println(String.format("All backups for databases with a name containing \"%s\":", databaseId.getDatabase()));
for (Backup backup : instance.listBackups(Options.filter(String.format("database:%s", databaseId.getDatabase()))).iterateAll()) {
System.out.println(backup);
}
// List all backups that expire before a certain time.
Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(30), TimeUnit.MILLISECONDS));
System.out.println(String.format("All backups that expire before %s:", expireTime.toString()));
for (Backup backup : instance.listBackups(Options.filter(String.format("expire_time < \"%s\"", expireTime.toString()))).iterateAll()) {
System.out.println(backup);
}
// List all backups with size greater than a certain number of bytes.
System.out.println("All backups with size greater than 100 bytes:");
for (Backup backup : instance.listBackups(Options.filter("size_bytes > 100")).iterateAll()) {
System.out.println(backup);
}
// List all backups with a create time after a certain timestamp and that are also ready.
Timestamp createTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1), TimeUnit.MILLISECONDS));
System.out.println(String.format("All databases created after %s and that are ready:", createTime.toString()));
for (Backup backup : instance.listBackups(Options.filter(String.format("create_time >= \"%s\" AND state:READY", createTime.toString()))).iterateAll()) {
System.out.println(backup);
}
// List backups using pagination.
System.out.println("All backups, listed using pagination:");
Page<Backup> page = instance.listBackups(Options.pageSize(10));
while (true) {
for (Backup backup : page.getValues()) {
System.out.println(backup);
}
if (!page.hasNextPage()) {
break;
}
page = page.getNextPage();
}
}
use of com.google.cloud.spanner.Backup in project java-spanner by googleapis.
the class SpannerSampleIT method deleteAllBackups.
private void deleteAllBackups(String instanceId) throws InterruptedException {
for (Backup backup : dbClient.listBackups(instanceId).iterateAll()) {
int attempts = 0;
while (attempts < 30) {
try {
attempts++;
backup.delete();
break;
} catch (SpannerException e) {
if (e.getErrorCode() == ErrorCode.FAILED_PRECONDITION && e.getMessage().contains("Please try deleting the backup once the restore or post-restore optimize " + "operations have completed on these databases.")) {
// Wait 30 seconds and then retry.
Thread.sleep(30_000L);
} else {
throw e;
}
}
}
}
}
Aggregations