|
| 1 | +# Copyright 2020 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""This application demonstrates how to create and restore from backups |
| 16 | +using Cloud Spanner. |
| 17 | +
|
| 18 | +For more information, see the README.rst under /spanner. |
| 19 | +""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +from datetime import ( |
| 23 | + datetime, |
| 24 | + timedelta |
| 25 | +) |
| 26 | +import time |
| 27 | + |
| 28 | +from google.cloud import spanner |
| 29 | + |
| 30 | + |
| 31 | +# [START spanner_create_backup] |
| 32 | +def create_backup(instance_id, database_id, backup_id): |
| 33 | + """Creates a backup for a database.""" |
| 34 | + spanner_client = spanner.Client() |
| 35 | + instance = spanner_client.instance(instance_id) |
| 36 | + database = instance.database(database_id) |
| 37 | + |
| 38 | + # Create a backup |
| 39 | + expire_time = datetime.utcnow() + timedelta(days=14) |
| 40 | + backup = instance.backup( |
| 41 | + backup_id, database=database, expire_time=expire_time) |
| 42 | + operation = backup.create() |
| 43 | + |
| 44 | + # Wait for backup operation to complete. |
| 45 | + operation.result() |
| 46 | + |
| 47 | + # Verify that the backup is ready. |
| 48 | + backup.reload() |
| 49 | + assert backup.is_ready() is True |
| 50 | + |
| 51 | + # Get the name, create time and backup size. |
| 52 | + backup.reload() |
| 53 | + print("Backup {} of size {} bytes was created at {}".format( |
| 54 | + backup.name, backup.size_bytes, backup.create_time)) |
| 55 | +# [END spanner_create_backup] |
| 56 | + |
| 57 | + |
| 58 | +# [START spanner_restore_database] |
| 59 | +def restore_database(instance_id, new_database_id, backup_id): |
| 60 | + """Restores a database from a backup.""" |
| 61 | + spanner_client = spanner.Client() |
| 62 | + instance = spanner_client.instance(instance_id) |
| 63 | + # Create a backup on database_id. |
| 64 | + |
| 65 | + # Start restoring backup to a new database. |
| 66 | + backup = instance.backup(backup_id) |
| 67 | + new_database = instance.database(new_database_id) |
| 68 | + operation = new_database.restore(backup) |
| 69 | + |
| 70 | + # Wait for restore operation to complete. |
| 71 | + operation.result() |
| 72 | + |
| 73 | + # Newly created database has restore information. |
| 74 | + new_database.reload() |
| 75 | + restore_info = new_database.restore_info |
| 76 | + print("Database {} restored to {} from backup {}.".format( |
| 77 | + restore_info.backup_info.source_database, |
| 78 | + new_database_id, |
| 79 | + restore_info.backup_info.backup)) |
| 80 | +# [END spanner_restore_database] |
| 81 | + |
| 82 | + |
| 83 | +# [START spanner_cancel_backup] |
| 84 | +def cancel_backup(instance_id, database_id, backup_id): |
| 85 | + spanner_client = spanner.Client() |
| 86 | + instance = spanner_client.instance(instance_id) |
| 87 | + database = instance.database(database_id) |
| 88 | + |
| 89 | + expire_time = datetime.utcnow() + timedelta(days=30) |
| 90 | + |
| 91 | + # Create a backup. |
| 92 | + backup = instance.backup( |
| 93 | + backup_id, database=database, expire_time=expire_time) |
| 94 | + operation = backup.create() |
| 95 | + |
| 96 | + # Cancel backup creation. |
| 97 | + operation.cancel() |
| 98 | + |
| 99 | + # Cancel operations are best effort so either it will complete or |
| 100 | + # be cancelled. |
| 101 | + while not operation.done(): |
| 102 | + time.sleep(300) # 5 mins |
| 103 | + |
| 104 | + # Deal with resource if the operation succeeded. |
| 105 | + if backup.exists(): |
| 106 | + print("Backup was created before the cancel completed.") |
| 107 | + backup.delete() |
| 108 | + print("Backup deleted.") |
| 109 | + else: |
| 110 | + print("Backup creation was successfully cancelled.") |
| 111 | +# [END spanner_cancel_backup] |
| 112 | + |
| 113 | + |
| 114 | +# [START spanner_list_backup_operations] |
| 115 | +def list_backup_operations(instance_id, database_id): |
| 116 | + spanner_client = spanner.Client() |
| 117 | + instance = spanner_client.instance(instance_id) |
| 118 | + |
| 119 | + # List the CreateBackup operations. |
| 120 | + filter_ = ( |
| 121 | + "(metadata.database:{}) AND " |
| 122 | + "(metadata.@type:type.googleapis.com/" |
| 123 | + "google.spanner.admin.database.v1.CreateBackupMetadata)" |
| 124 | + ).format(database_id) |
| 125 | + operations = instance.list_backup_operations(filter_=filter_) |
| 126 | + for op in operations: |
| 127 | + metadata = op.metadata |
| 128 | + print("Backup {} on database {}: {}% complete.".format( |
| 129 | + metadata.name, metadata.database, |
| 130 | + metadata.progress.progress_percent)) |
| 131 | +# [END spanner_list_backup_operations] |
| 132 | + |
| 133 | + |
| 134 | +# [START spanner_list_database_operations] |
| 135 | +def list_database_operations(instance_id): |
| 136 | + spanner_client = spanner.Client() |
| 137 | + instance = spanner_client.instance(instance_id) |
| 138 | + |
| 139 | + # List the progress of restore. |
| 140 | + filter_ = ( |
| 141 | + "(metadata.@type:type.googleapis.com/" |
| 142 | + "google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata)" |
| 143 | + ) |
| 144 | + operations = instance.list_database_operations(filter_=filter_) |
| 145 | + for op in operations: |
| 146 | + print("Database {} restored from backup is {}% optimized.".format( |
| 147 | + op.metadata.name, op.metadata.progress.progress_percent)) |
| 148 | +# [END spanner_list_database_operations] |
| 149 | + |
| 150 | + |
| 151 | +# [START spanner_list_backups] |
| 152 | +def list_backups(instance_id, database_id, backup_id): |
| 153 | + spanner_client = spanner.Client() |
| 154 | + instance = spanner_client.instance(instance_id) |
| 155 | + |
| 156 | + # List all backups. |
| 157 | + print("All backups:") |
| 158 | + for backup in instance.list_backups(): |
| 159 | + print(backup.name) |
| 160 | + |
| 161 | + # List all backups that contain a name. |
| 162 | + print("All backups with backup name containing \"{}\":".format(backup_id)) |
| 163 | + for backup in instance.list_backups(filter_="name:{}".format(backup_id)): |
| 164 | + print(backup.name) |
| 165 | + |
| 166 | + # List all backups for a database that contains a name. |
| 167 | + print("All backups with database name containing \"{}\":".format(database_id)) |
| 168 | + for backup in instance.list_backups(filter_="database:{}".format(database_id)): |
| 169 | + print(backup.name) |
| 170 | + |
| 171 | + # List all backups that expire before a timestamp. |
| 172 | + expire_time = datetime.utcnow().replace(microsecond=0) + timedelta(days=30) |
| 173 | + print("All backups with expire_time before \"{}-{}-{}T{}:{}:{}Z\":".format( |
| 174 | + *expire_time.timetuple())) |
| 175 | + for backup in instance.list_backups( |
| 176 | + filter_="expire_time < \"{}-{}-{}T{}:{}:{}Z\"".format( |
| 177 | + *expire_time.timetuple())): |
| 178 | + print(backup.name) |
| 179 | + |
| 180 | + # List all backups with a size greater than some bytes. |
| 181 | + print("All backups with backup size more than 100 bytes:") |
| 182 | + for backup in instance.list_backups(filter_="size_bytes > 100"): |
| 183 | + print(backup.name) |
| 184 | + |
| 185 | + # List backups that were created after a timestamp that are also ready. |
| 186 | + create_time = datetime.utcnow().replace(microsecond=0) - timedelta(days=1) |
| 187 | + print("All backups created after \"{}-{}-{}T{}:{}:{}Z\" and are READY:".format( |
| 188 | + *create_time.timetuple())) |
| 189 | + for backup in instance.list_backups( |
| 190 | + filter_="create_time >= \"{}-{}-{}T{}:{}:{}Z\" AND state:READY".format( |
| 191 | + *create_time.timetuple())): |
| 192 | + print(backup.name) |
| 193 | + |
| 194 | + print("All backups with pagination") |
| 195 | + for page in instance.list_backups(page_size=2).pages: |
| 196 | + for backup in page: |
| 197 | + print(backup.name) |
| 198 | +# [END spanner_list_backups] |
| 199 | + |
| 200 | + |
| 201 | +# [START spanner_delete_backup] |
| 202 | +def delete_backup(instance_id, backup_id): |
| 203 | + spanner_client = spanner.Client() |
| 204 | + instance = spanner_client.instance(instance_id) |
| 205 | + backup = instance.backup(backup_id) |
| 206 | + backup.reload() |
| 207 | + |
| 208 | + # Wait for databases that reference this backup to finish optimizing. |
| 209 | + while backup.referencing_databases: |
| 210 | + time.sleep(30) |
| 211 | + backup.reload() |
| 212 | + |
| 213 | + # Delete the backup. |
| 214 | + backup.delete() |
| 215 | + |
| 216 | + # Verify that the backup is deleted. |
| 217 | + assert backup.exists() is False |
| 218 | + print("Backup {} has been deleted.".format(backup.name)) |
| 219 | +# [END spanner_delete_backup] |
| 220 | + |
| 221 | + |
| 222 | +# [START spanner_update_backup] |
| 223 | +def update_backup(instance_id, backup_id): |
| 224 | + spanner_client = spanner.Client() |
| 225 | + instance = spanner_client.instance(instance_id) |
| 226 | + backup = instance.backup(backup_id) |
| 227 | + backup.reload() |
| 228 | + |
| 229 | + # Expire time must be within 366 days of the create time of the backup. |
| 230 | + old_expire_time = backup.expire_time |
| 231 | + new_expire_time = old_expire_time + timedelta(days=30) |
| 232 | + backup.update_expire_time(new_expire_time) |
| 233 | + print("Backup {} expire time was updated from {} to {}.".format( |
| 234 | + backup.name, old_expire_time, new_expire_time)) |
| 235 | +# [END spanner_update_backup] |
| 236 | + |
| 237 | + |
| 238 | +if __name__ == '__main__': # noqa: C901 |
| 239 | + parser = argparse.ArgumentParser( |
| 240 | + description=__doc__, |
| 241 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 242 | + parser.add_argument( |
| 243 | + 'instance_id', help='Your Cloud Spanner instance ID.') |
| 244 | + parser.add_argument( |
| 245 | + '--database-id', help='Your Cloud Spanner database ID.', |
| 246 | + default='example_db') |
| 247 | + parser.add_argument( |
| 248 | + '--backup-id', help='Your Cloud Spanner backup ID.', |
| 249 | + default='example_backup') |
| 250 | + |
| 251 | + subparsers = parser.add_subparsers(dest='command') |
| 252 | + subparsers.add_parser('create_backup', help=create_backup.__doc__) |
| 253 | + subparsers.add_parser('cancel_backup', help=cancel_backup.__doc__) |
| 254 | + subparsers.add_parser('update_backup', help=update_backup.__doc__) |
| 255 | + subparsers.add_parser('restore_database', help=restore_database.__doc__) |
| 256 | + subparsers.add_parser('list_backups', help=list_backups.__doc__) |
| 257 | + subparsers.add_parser('list_backup_operations', help=list_backup_operations.__doc__) |
| 258 | + subparsers.add_parser('list_database_operations', |
| 259 | + help=list_database_operations.__doc__) |
| 260 | + subparsers.add_parser('delete_backup', help=delete_backup.__doc__) |
| 261 | + |
| 262 | + args = parser.parse_args() |
| 263 | + |
| 264 | + if args.command == 'create_backup': |
| 265 | + create_backup(args.instance_id, args.database_id, args.backup_id) |
| 266 | + elif args.command == 'cancel_backup': |
| 267 | + cancel_backup(args.instance_id, args.database_id, args.backup_id) |
| 268 | + elif args.command == 'update_backup': |
| 269 | + update_backup(args.instance_id, args.backup_id) |
| 270 | + elif args.command == 'restore_database': |
| 271 | + restore_database(args.instance_id, args.database_id, args.backup_id) |
| 272 | + elif args.command == 'list_backups': |
| 273 | + list_backups(args.instance_id, args.database_id, args.backup_id) |
| 274 | + elif args.command == 'list_backup_operations': |
| 275 | + list_backup_operations(args.instance_id, args.database_id) |
| 276 | + elif args.command == 'list_database_operations': |
| 277 | + list_database_operations(args.instance_id) |
| 278 | + elif args.command == 'delete_backup': |
| 279 | + delete_backup(args.instance_id, args.backup_id) |
| 280 | + else: |
| 281 | + print("Command {} did not match expected commands.".format(args.command)) |
0 commit comments