1# -*- coding: utf-8 -*- # 2# Copyright 2016 Google LLC. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15"""Restores a backup of a Cloud SQL instance.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import unicode_literals 20 21from googlecloudsdk.api_lib.sql import api_util 22from googlecloudsdk.api_lib.sql import operations 23from googlecloudsdk.api_lib.sql import validate 24from googlecloudsdk.calliope import base 25from googlecloudsdk.command_lib.sql import flags 26from googlecloudsdk.core import log 27from googlecloudsdk.core import properties 28from googlecloudsdk.core.console import console_io 29 30 31@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA, 32 base.ReleaseTrack.ALPHA) 33class RestoreBackup(base.RestoreCommand): 34 """Restores a backup of a Cloud SQL instance.""" 35 36 @staticmethod 37 def Args(parser): 38 """Args is called by calliope to gather arguments for this command. 39 40 Args: 41 parser: An argparse parser that you can use to add arguments that go 42 on the command line after this command. Positional arguments are 43 allowed. 44 """ 45 parser.add_argument( 46 'backup_id', type=int, help='The ID of the backup run to restore from.') 47 parser.add_argument( 48 '--restore-instance', 49 required=True, 50 completer=flags.InstanceCompleter, 51 help='Cloud SQL instance ID that will be restored.') 52 parser.add_argument( 53 '--backup-instance', 54 completer=flags.InstanceCompleter, 55 help='The ID of the instance that the backup was taken from.') 56 base.ASYNC_FLAG.AddToParser(parser) 57 58 def Run(self, args): 59 """Restores a backup of a Cloud SQL instance. 60 61 Args: 62 args: argparse.Namespace, The arguments that this command was invoked 63 with. 64 65 Returns: 66 A dict object representing the operations resource describing the 67 restoreBackup operation if the restoreBackup was successful. 68 """ 69 70 client = api_util.SqlClient(api_util.API_VERSION_DEFAULT) 71 sql_client = client.sql_client 72 sql_messages = client.sql_messages 73 74 validate.ValidateInstanceName(args.restore_instance) 75 instance_ref = client.resource_parser.Parse( 76 args.restore_instance, 77 params={'project': properties.VALUES.core.project.GetOrFail}, 78 collection='sql.instances') 79 if not console_io.PromptContinue( 80 'All current data on the instance will be lost when the backup is ' 81 'restored.'): 82 return None 83 if not args.backup_instance: 84 args.backup_instance = args.restore_instance 85 86 result_operation = sql_client.instances.RestoreBackup( 87 sql_messages.SqlInstancesRestoreBackupRequest( 88 project=instance_ref.project, 89 instance=instance_ref.instance, 90 instancesRestoreBackupRequest=( 91 sql_messages.InstancesRestoreBackupRequest( 92 restoreBackupContext=sql_messages.RestoreBackupContext( 93 backupRunId=args.backup_id, 94 instanceId=args.backup_instance,))))) 95 96 operation_ref = client.resource_parser.Create( 97 'sql.operations', 98 operation=result_operation.name, 99 project=instance_ref.project) 100 101 if args.async_: 102 return sql_client.operations.Get( 103 sql_messages.SqlOperationsGetRequest( 104 project=operation_ref.project, 105 operation=operation_ref.operation)) 106 107 operations.OperationsV1Beta4.WaitForOperation( 108 sql_client, operation_ref, 'Restoring Cloud SQL instance') 109 110 log.status.write('Restored [{instance}].\n'.format(instance=instance_ref)) 111 112 return None 113