Skip to content

Commit f984b26

Browse files
authored
samples: PITR samples backup fix (#247)
* samples: creates a backup using the current time Instead of using the earliest version time of the database, uses the current time (from spanner). If we used the earliest version time of the database instead we would be creating an empty backup, since the database we are backing up is not 1 hour old (default version retention period). * samples: addresses pr comments Refactors create backup sample
1 parent 1c2a64f commit f984b26

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

samples/samples/backup_sample.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ def create_backup(instance_id, database_id, backup_id):
3232
instance = spanner_client.instance(instance_id)
3333
database = instance.database(database_id)
3434

35+
# Sets the version time as the current server time
36+
version_time = None
37+
with database.snapshot() as snapshot:
38+
results = snapshot.execute_sql("SELECT CURRENT_TIMESTAMP()")
39+
version_time = list(results)[0][0]
40+
3541
# Create a backup
3642
expire_time = datetime.utcnow() + timedelta(days=14)
37-
version_time = database.earliest_version_time
3843
backup = instance.backup(backup_id, database=database, expire_time=expire_time, version_time=version_time)
3944
operation = backup.create()
4045

0 commit comments

Comments
 (0)