use of com.google.cloud.spanner.Backup in project java-spanner by googleapis.
the class SpannerSample method restoreBackup.
// [END spanner_list_backups]
// [START spanner_restore_backup]
static void restoreBackup(DatabaseAdminClient dbAdminClient, BackupId backupId, DatabaseId sourceDatabaseId, DatabaseId restoreToDatabase) {
Backup backup = dbAdminClient.newBackupBuilder(backupId).build();
// Initiate the request which returns an OperationFuture.
System.out.println(String.format("Restoring backup [%s] to database [%s]...", backup.getId().toString(), restoreToDatabase.toString()));
try {
OperationFuture<Database, RestoreDatabaseMetadata> op = backup.restore(restoreToDatabase);
// Wait until the database has been restored.
Database db = op.get();
// Refresh database metadata and get the restore info.
RestoreInfo restore = db.reload().getRestoreInfo();
Timestamp versionTime = Timestamp.fromProto(restore.getProto().getBackupInfo().getVersionTime());
System.out.println("Restored database [" + restore.getSourceDatabase().getName() + "] from [" + restore.getBackup().getName() + "] with version time [" + versionTime + "]");
} catch (ExecutionException e) {
throw SpannerExceptionFactory.newSpannerException(e.getCause());
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
}
}
use of com.google.cloud.spanner.Backup in project java-spanner by googleapis.
the class SpannerSample method cancelCreateBackup.
// [END spanner_create_backup]
// [START spanner_cancel_backup_create]
static void cancelCreateBackup(DatabaseAdminClient dbAdminClient, DatabaseId databaseId, BackupId backupId) {
// Set expire time to 14 days from now.
Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
// Create a backup instance.
Backup backup = dbAdminClient.newBackupBuilder(backupId).setDatabase(databaseId).setExpireTime(expireTime).build();
// Start the creation of a backup.
System.out.println("Creating backup [" + backup.getId() + "]...");
OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
try {
// Try to cancel the backup operation.
System.out.println("Cancelling create backup operation for [" + backup.getId() + "]...");
dbAdminClient.cancelOperation(op.getName());
// Get a polling future for the running operation. This future will regularly poll the server
// for the current status of the backup operation.
RetryingFuture<OperationSnapshot> pollingFuture = op.getPollingFuture();
// successful or not.
while (!pollingFuture.get().isDone()) {
System.out.println("Waiting for the cancelled backup operation to finish...");
Thread.sleep(TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS));
}
if (pollingFuture.get().getErrorCode() == null) {
// Backup was created before it could be cancelled. Delete the backup.
backup.delete();
System.out.println("Backup operation for [" + backup.getId() + "] successfully finished before it could be cancelled");
} else if (pollingFuture.get().getErrorCode().getCode() == StatusCode.Code.CANCELLED) {
System.out.println("Backup operation for [" + backup.getId() + "] successfully cancelled");
}
} catch (ExecutionException e) {
throw SpannerExceptionFactory.newSpannerException(e.getCause());
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
}
}
use of com.google.cloud.spanner.Backup in project java-spanner by googleapis.
the class SpannerSample method createBackup.
// [END spanner_query_with_query_options]
// [START spanner_create_backup]
static void createBackup(DatabaseAdminClient dbAdminClient, DatabaseId databaseId, BackupId backupId, Timestamp versionTime) {
// Set expire time to 14 days from now.
Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
Backup backup = dbAdminClient.newBackupBuilder(backupId).setDatabase(databaseId).setExpireTime(expireTime).setVersionTime(versionTime).build();
// Initiate the request which returns an OperationFuture.
System.out.println("Creating backup [" + backup.getId() + "]...");
OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
try {
// Wait for the backup operation to complete.
backup = op.get();
System.out.println("Created backup [" + backup.getId() + "]");
} catch (ExecutionException e) {
throw (SpannerException) e.getCause();
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
}
// Reload the metadata of the backup from the server.
backup = backup.reload();
System.out.println(String.format("Backup %s of size %d bytes was created at %s for version of database at %s", backup.getId().getName(), backup.getSize(), LocalDateTime.ofEpochSecond(backup.getProto().getCreateTime().getSeconds(), backup.getProto().getCreateTime().getNanos(), OffsetDateTime.now().getOffset()), LocalDateTime.ofEpochSecond(backup.getProto().getVersionTime().getSeconds(), backup.getProto().getVersionTime().getNanos(), OffsetDateTime.now().getOffset())));
}
use of com.google.cloud.spanner.Backup in project java-spanner by googleapis.
the class ITBackupTest method test01_Backups.
@Test
public void test01_Backups() throws InterruptedException, ExecutionException, TimeoutException {
final String databaseId = testHelper.getUniqueDatabaseId() + "_db1";
final Database sourceDatabase = dbAdminClient.newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId)).setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(keyName)).build();
logger.info(String.format("Creating test database %s", databaseId));
OperationFuture<Database, CreateDatabaseMetadata> createDatabaseOperation = dbAdminClient.createDatabase(sourceDatabase, Collections.singletonList("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"));
// Make sure the database has been created before we try to create a backup.
Database database = createDatabaseOperation.get(DATABASE_TIMEOUT_MINUTES, TimeUnit.MINUTES);
databases.add(database.getId().getDatabase());
// Insert some data to make sure the backup will have a size>0.
DatabaseClient client = testHelper.getDatabaseClient(database);
client.writeAtLeastOnce(Collections.singletonList(Mutation.newInsertOrUpdateBuilder("FOO").set("ID").to(1L).set("NAME").to("TEST").build()));
// Verifies that the database encryption has been properly set
testDatabaseEncryption(database, keyName);
// Verifies that the database dialect has been properly set
testDatabaseDialect(database, Dialect.GOOGLE_STANDARD_SQL);
// Create a backup of the database.
String backupId = testHelper.getUniqueBackupId() + "_bck1";
Timestamp expireTime = afterDays(7);
Timestamp versionTime = getCurrentTimestamp(client);
logger.info(String.format("Creating backup %s", backupId));
// This backup has the version time specified as the server's current timestamp
// This backup is encrypted with a customer managed key
// The expiry time is 7 days in the future.
final Backup backupToCreate = dbAdminClient.newBackupBuilder(BackupId.of(projectId, instanceId, backupId)).setDatabase(database.getId()).setExpireTime(expireTime).setVersionTime(versionTime).setExpireTime(expireTime).setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(keyName)).build();
OperationFuture<Backup, CreateBackupMetadata> operation = dbAdminClient.createBackup(backupToCreate);
backups.add(backupId);
// Execute metadata tests as part of this integration test to reduce total execution time.
testMetadata(operation, backupId, database);
// Ensure that the backup has been created before we proceed.
logger.info("Waiting for backup operation to finish");
Backup backup = operation.get(BACKUP_TIMEOUT_MINUTES, TimeUnit.MINUTES);
// Verifies that backup version time is the specified one
testBackupVersionTime(backup, versionTime);
// Verifies that backup encryption has been properly set
testBackupEncryption(backup, keyName);
// Insert some more data into the database to get a timestamp from the server.
Timestamp commitTs = client.writeAtLeastOnce(Collections.singletonList(Mutation.newInsertOrUpdateBuilder("FOO").set("ID").to(2L).set("NAME").to("TEST2").build()));
// Test listing operations.
// List all backups.
logger.info("Listing all backups");
assertTrue(Iterables.contains(instance.listBackups().iterateAll(), backup));
// List all backups whose names contain 'bck1'.
logger.info("Listing backups with name bck1");
assertTrue(Iterables.elementsEqual(dbAdminClient.listBackups(instanceId, Options.filter(String.format("name:%s", backup.getId().getName()))).iterateAll(), Collections.singleton(backup)));
logger.info("Listing ready backups");
Iterable<Backup> readyBackups = dbAdminClient.listBackups(instanceId, Options.filter("state:READY")).iterateAll();
assertTrue(Iterables.contains(readyBackups, backup));
// List all backups for databases whose names contain 'db1'.
logger.info("Listing backups for database db1");
assertTrue(Iterables.elementsEqual(dbAdminClient.listBackups(instanceId, Options.filter(String.format("database:%s", database.getId().getName()))).iterateAll(), Collections.singleton(backup)));
// List all backups that were created before a certain time.
Timestamp ts = Timestamp.ofTimeSecondsAndNanos(commitTs.getSeconds(), 0);
logger.info(String.format("Listing backups created before %s", ts));
assertTrue(Iterables.contains(dbAdminClient.listBackups(instanceId, Options.filter(String.format("create_time<\"%s\"", ts))).iterateAll(), backup));
// List all backups with a size > 0.
logger.info("Listing backups with size>0");
assertTrue(Iterables.contains(dbAdminClient.listBackups(instanceId, Options.filter("size_bytes>0")).iterateAll(), backup));
// Test pagination.
testPagination();
logger.info("Finished listBackup tests");
// Execute other tests as part of this integration test to reduce total execution time.
testGetBackup(database, backupId, expireTime);
testUpdateBackup(backup);
testCreateInvalidExpirationDate(database);
testRestore(backup, versionTime, keyName);
testCancelBackupOperation(database);
// Finished all tests.
logger.info("Finished all backup tests");
}
use of com.google.cloud.spanner.Backup in project java-spanner by googleapis.
the class ITBackupTest method test05_backupCreationWithVersionTimeInTheFutureFails.
@Test(expected = SpannerException.class)
public void test05_backupCreationWithVersionTimeInTheFutureFails() throws Exception {
final Database testDatabase = testHelper.createTestDatabase();
final DatabaseId databaseId = testDatabase.getId();
final InstanceId instanceId = databaseId.getInstanceId();
final String backupId = testHelper.getUniqueBackupId();
final Timestamp expireTime = afterDays(7);
final Timestamp versionTime = afterDays(1);
final Backup backupToCreate = dbAdminClient.newBackupBuilder(BackupId.of(instanceId, backupId)).setDatabase(databaseId).setExpireTime(expireTime).setVersionTime(versionTime).build();
getOrThrow(dbAdminClient.createBackup(backupToCreate));
}
Aggregations