gcloud-sql-export-sql
Export SQL database to a Cloud Storage bucket
TLDR
Export data from a specific Cloud SQL instance to a Google Cloud Storage bucket as an SQL dump file
Export data asynchronously, returning immediately without waiting for the operation to complete
Export data from specific databases within the Cloud SQL instance
Export specific tables from a specified database within the Cloud SQL instance
Export data while offloading the operation to a temporary instance to reduce strain on the source instance
Export data and compress the output with gzip
SYNOPSIS
gcloud sql export sql INSTANCE_NAME GS_URI [--database=DATABASE[,...]] [--table=TABLE[,...]] [--offload] [--async] [--file-per-table] [--uri-file-per-table-prefix=PREFIX] [GCLOUD_WIDE_FLAG...]
PARAMETERS
INSTANCE_NAME
The ID of the Cloud SQL instance from which to export data. This is a positional argument.
GS_URI
The Google Cloud Storage URI where the SQL export file will be written. For example, gs://my-bucket/my-dump.sql. This is a positional argument.
--database=DATABASE[,...]
Comma-separated list of database names to export. If not specified, all databases on the instance are exported, except for system databases.
--table=TABLE[,...]
Comma-separated list of table names to export. This flag requires the --database flag to be specified, and tables must belong to one of the specified databases.
--offload
Offload the export operation to reduce performance impact on the primary instance. Recommended for large exports.
--async
Return immediately, without waiting for the operation to complete.
--file-per-table
Exports each table to a separate SQL file. Requires --table to be specified. Each file will be named TABLE_NAME.sql. The GS_URI must be a folder.
--uri-file-per-table-prefix=PREFIX
Used with --file-per-table to specify a URI prefix for each exported file. The format is gs://BUCKET/PATH/PREFIX_TABLE_NAME.sql.
DESCRIPTION
The gcloud sql export sql command facilitates the export of a Cloud SQL instance's database content into a SQL dump file stored in a Google Cloud Storage bucket. This operation is crucial for various tasks, including creating backups for disaster recovery, migrating data between instances or to other environments, performing offline analysis, or sharing specific database subsets.
The command supports exporting entire databases, specific tables within a database, or even multiple databases from a single instance. The output is a standard SQL text file containing CREATE TABLE and INSERT statements, making it highly portable. Users must ensure that the Cloud SQL instance has network connectivity to the specified Cloud Storage bucket and that the service account associated with the instance has sufficient permissions to write to the bucket. The export process runs asynchronously, allowing you to monitor its progress or retrieve the operation's status.
CAVEATS
Export operations require specific Identity and Access Management (IAM) permissions for the service account associated with the Cloud SQL instance to write to the designated Cloud Storage bucket.
Large exports can consume significant instance resources and may impact performance unless the --offload flag is used.
Exports do not include Cloud SQL users, stored procedures, triggers, or event schedulers; only the schema and data of the tables are exported.
The target Cloud Storage bucket must be in the same region as the Cloud SQL instance or a multi-region location that includes the instance's region.
Exported files incur Cloud Storage costs.
Character set and collation issues might arise if not properly handled during import.
PERMISSIONS REQUIRED
The Cloud SQL instance's service account (e.g., project-id@cloudservices.gserviceaccount.com) must have the Storage Object Creator or Storage Object Admin IAM role on the target Cloud Storage bucket. Without these permissions, the export operation will fail.
MONITORING EXPORT OPERATIONS
After initiating an export, the operation ID is returned if run asynchronously (with --async). You can monitor its progress using gcloud sql operations describe OPERATION_ID or view operations via the Google Cloud Console. For synchronous exports, the command waits until completion or failure.
OUTPUT FORMAT
The exported file is a plain text SQL dump, typically containing CREATE TABLE statements for schema definition and INSERT INTO statements for data. It can be directly imported into another compatible SQL database or used for analysis.
HISTORY
The gcloud command-line tool, including gcloud sql export sql, is part of the Google Cloud SDK. It has evolved significantly since its initial release, providing a unified interface for managing Google Cloud resources. The sql component specifically targets Cloud SQL database management, with export capabilities being a core feature from early versions, continuously improved for performance and flexibility (e.g., introduction of offload exports, file-per-table). Its development aligns with the ongoing enhancements and new features of the Cloud SQL service itself.
SEE ALSO
gcloud sql import sql(1), gcloud sql export csv(1), gcloud sql backups create(1), gcloud sql operations describe(1), gsutil cp(1)