HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //snap/google-cloud-cli/396/lib/googlecloudsdk/command_lib/sql/export_util.py
# -*- coding: utf-8 -*- #
# Copyright 2017 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Common utility functions for sql export commands."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from googlecloudsdk.api_lib.sql import export_util
from googlecloudsdk.api_lib.sql import operations
from googlecloudsdk.api_lib.sql import validate
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.sql import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties


def AddBaseExportFlags(
    parser,
    gz_supported=True,
    database_required=False,
    database_help_text=flags.DEFAULT_DATABASE_LIST_EXPORT_HELP_TEXT):
  """Adds the base export flags to the parser.

  Args:
    parser: The current argparse parser to add these flags to.
    gz_supported: Boolean, specifies whether gz compression is supported.
    database_required: Boolean, specifies whether the database flag is required.
    database_help_text: String, specifies the help text for the database flag.
  """
  base.ASYNC_FLAG.AddToParser(parser)
  flags.AddInstanceArgument(parser)

  uri_help_text = ('The path to the file in Google Cloud Storage where the '
                   'export will be stored. The URI is in the form '
                   'gs://bucketName/fileName. If the file already exists, the '
                   'operation fails.')
  if gz_supported:
    uri_help_text = uri_help_text + (' If the filename ends with .gz, the '
                                     'contents are compressed.')
  flags.AddUriArgument(parser, uri_help_text)
  flags.AddDatabaseList(parser, database_help_text, database_required)


def RunExportCommand(args, client, export_context):
  """Exports data from a Cloud SQL instance.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.
    export_context: ExportContext; format-specific export metadata.

  Returns:
    A dict representing the export operation resource, if '--async' is used,
    or else None.

  Raises:
    HttpException: An HTTP error response was received while executing API
        request.
    ToolException: An error other than HTTP error occurred while executing the
        command.
  """
  sql_client = client.sql_client
  sql_messages = client.sql_messages

  validate.ValidateInstanceName(args.instance)
  instance_ref = client.resource_parser.Parse(
      args.instance,
      params={'project': properties.VALUES.core.project.GetOrFail},
      collection='sql.instances')

  export_request = sql_messages.SqlInstancesExportRequest(
      instance=instance_ref.instance,
      project=instance_ref.project,
      instancesExportRequest=sql_messages.InstancesExportRequest(
          exportContext=export_context))

  result_operation = sql_client.instances.Export(export_request)

  operation_ref = client.resource_parser.Create(
      'sql.operations',
      operation=result_operation.name,
      project=instance_ref.project)

  if args.async_:
    return sql_client.operations.Get(
        sql_messages.SqlOperationsGetRequest(
            project=operation_ref.project, operation=operation_ref.operation))

  is_tde_export = (
      export_context.fileType
      == sql_messages.ExportContext.FileTypeValueValuesEnum.TDE
  )

  export_dest = args.cert_path if is_tde_export else args.uri

  operations.OperationsV1Beta4.WaitForOperation(sql_client, operation_ref,
                                                'Exporting Cloud SQL instance')

  log.status.write('Exported [{instance}] to [{bucket}].\n'.format(
      instance=instance_ref, bucket=export_dest))

  return None


def RunSqlExportCommand(args, client):
  """Exports data from a Cloud SQL instance to a MySQL dump file.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.

  Returns:
    A dict object representing the operations resource describing the export
    operation if the export was successful.
  """
  sql_export_context = export_util.SqlExportContext(
      client.sql_messages,
      args.uri,
      args.database,
      args.table,
      offload=args.offload,
      parallel=args.parallel,
      threads=args.threads,
      clean=args.clean,
      if_exists=args.if_exists,
  )
  if args.offload:
    log.status.write(
        'Serverless exports cost extra. See the pricing page for more information: https://cloud.google.com/sql/pricing.\n'
    )
  return RunExportCommand(args, client, sql_export_context)


def RunCsvExportCommand(args, client):
  """Exports data from a Cloud SQL instance to a CSV file.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.

  Returns:
    A dict object representing the operations resource describing the export
    operation if the export was successful.
  """
  csv_export_context = export_util.CsvExportContext(
      client.sql_messages,
      args.uri,
      args.database,
      args.query,
      offload=args.offload,
      quote=args.quote,
      escape=args.escape,
      fields_terminated_by=args.fields_terminated_by,
      lines_terminated_by=args.lines_terminated_by)
  if args.offload:
    log.status.write(
        'Serverless exports cost extra. See the pricing page for more information: https://cloud.google.com/sql/pricing.\n'
    )
  return RunExportCommand(args, client, csv_export_context)


def RunBakExportCommand(args, client):
  """Export data from a Cloud SQL instance to a SQL Server BAK file.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.

  Returns:
    A dict object representing the operations resource describing the export
    operation if the export was successful.
  """
  sql_export_context = export_util.BakExportContext(
      client.sql_messages,
      args.uri,
      args.database,
      args.stripe_count,
      args.striped,
      args.bak_type,
      args.differential_base,
      args.export_log_start_time,
      args.export_log_end_time,
  )
  return RunExportCommand(args, client, sql_export_context)


def RunTdeExportCommand(args, client):
  """Export TDE certificate from a Cloud SQL instance.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    client: SqlClient instance, with sql_client and sql_messages props, for use
      in generating messages and making API calls.

  Returns:
    A dict object representing the operations resource describing the export
    operation if the export was successful.
  """
  tde_export_context = export_util.TdeExportContext(
      client.sql_messages,
      args.certificate,
      args.cert_path,
      args.pvk_path,
      args.pvk_password,
  )
  return RunExportCommand(args, client, tde_export_context)