Skip to content
56 changes: 50 additions & 6 deletions samples/samples/backup_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def create_backup(instance_id, database_id, backup_id):

# Create a backup
expire_time = datetime.utcnow() + timedelta(days=14)
backup = instance.backup(backup_id, database=database, expire_time=expire_time)
version_time = database.earliest_version_time()
backup = instance.backup(backup_id, database=database, expire_time=expire_time, version_time=version_time)
operation = backup.create()

# Wait for backup operation to complete.
Expand All @@ -47,8 +48,8 @@ def create_backup(instance_id, database_id, backup_id):
# Get the name, create time and backup size.
backup.reload()
print(
"Backup {} of size {} bytes was created at {}".format(
backup.name, backup.size_bytes, backup.create_time
"Backup {} of size {} bytes was created at {} for version of database at {}".format(
backup.name, backup.size_bytes, backup.create_time, backup.version_time
)
)

Expand All @@ -57,14 +58,17 @@ def create_backup(instance_id, database_id, backup_id):


# [START spanner_restore_backup]
def restore_database(instance_id, new_database_id, backup_id):
def restore_database(instance_id, database_id, new_database_id, backup_id):
"""Restores a database from a backup."""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
# Create a backup on database_id.

# Start restoring backup to a new database.
backup = instance.backup(backup_id)
expire_time = datetime.utcnow() + timedelta(days=14)
version_time = datetime.now()
backup = instance.backup(backup_id, database=database, expire_time=expire_time, version_time=version_time)
new_database = instance.database(new_database_id)
operation = new_database.restore(backup)

Expand All @@ -75,10 +79,11 @@ def restore_database(instance_id, new_database_id, backup_id):
new_database.reload()
restore_info = new_database.restore_info
print(
"Database {} restored to {} from backup {}.".format(
"Database {} restored to {} from backup {} with version time {}.".format(
restore_info.backup_info.source_database,
new_database_id,
restore_info.backup_info.backup,
restore_info.backup_info.version_time
)
)

Expand Down Expand Up @@ -269,6 +274,45 @@ def update_backup(instance_id, backup_id):
# [END spanner_update_backup]


# [START spanner_create_database_with_version_retention_period]
def create_database_with_version_retention_period(instance_id, database_id, retention_period):
"""Creates a database with a version retention period."""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
ddl_statements = [
"CREATE TABLE Singers ("
+ " SingerId INT64 NOT NULL,"
+ " FirstName STRING(1024),"
+ " LastName STRING(1024),"
+ " SingerInfo BYTES(MAX)"
+ ") PRIMARY KEY (SingerId)",
"CREATE TABLE Albums ("
+ " SingerId INT64 NOT NULL,"
+ " AlbumId INT64 NOT NULL,"
+ " AlbumTitle STRING(MAX)"
+ ") PRIMARY KEY (SingerId, AlbumId),"
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE",
"ALTER DATABASE {}"
" SET OPTIONS (version_retention_period = '{}')".format(
database_id, retention_period
)
]
db = instance.database(database_id, ddl_statements)
operation = db.create()

operation.result(30)

db.reload()

print("Database {} created with version retention period {} and earliest version time {}".format(
db.database_id, db.version_retention_period(), db.earliest_version_time()
))

db.drop()

# [END spanner_create_database_with_version_retention_period]


if __name__ == "__main__": # noqa: C901
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
Expand Down
11 changes: 10 additions & 1 deletion samples/samples/backup_sample_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,22 @@ def test_create_backup(capsys, database):

@RetryErrors(exception=DeadlineExceeded, max_tries=2)
def test_restore_database(capsys):
backup_sample.restore_database(INSTANCE_ID, RESTORE_DB_ID, BACKUP_ID)
backup_sample.restore_database(INSTANCE_ID, DATABASE_ID, RESTORE_DB_ID, BACKUP_ID)
out, _ = capsys.readouterr()
assert (DATABASE_ID + " restored to ") in out
assert (RESTORE_DB_ID + " from backup ") in out
assert BACKUP_ID in out


@RetryErrors(exception=DeadlineExceeded, max_tries=2)
def test_restore_database_with_retention_period(capsys):
retention_period = "7d"
backup_sample.create_database_with_version_retention_period(INSTANCE_ID, DATABASE_ID, retention_period)
out, _ = capsys.readouterr()
assert (DATABASE_ID + " created with ") in out
assert ("retention period " + retention_period) in out


def test_list_backup_operations(capsys, spanner_instance):
backup_sample.list_backup_operations(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
Expand Down