Exporting Cloud SQL databases and tables

To export the Cloud SQL database to an SQL file or export the data from a table to a CSV, we can use the following command. Use the help option to learn about this command:

gcloud help sql instances export

The export option will export the file and store it in the Google Cloud Storage bucket or folder within the bucket. Before exporting the Google Cloud Storage bucket to which the file is to be saved, it should be granted a write permission for the Cloud SQL service account. Navigate to the Cloud SQL instance from which you want to export the data, as shown in the following screenshot. Copy the value of the service account:

Navigate to the Google Cloud Storage screen, click on the options button for a bucket, and choose Edit bucket permission. A panel to enter the permission details will be displayed, as shown in the screenshot. Enter the service account in the Add members textbox and in the Select a role dropdown choose Storage | Storage Object Creator. Click the Add button:

Run the following command to export the database from Cloud SQL instance to SQL file. The exported file will be stored in a GCS bucket to which we granted the permission previously:

gcloud sql instances export <instance name> gs://<bucketname>/<sql filename> --database=<database name>

To export the SQL for only one table in the database, use the --table option as shown in the following code. This will create an SQL file with drop and create option, and an insert command for all records in the table:

 gcloud sql instances export <instance name> gs://<bucketname>/<sql filename> --database=<database name> --table=<table name>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.54.245