This page describes how to clone a Cloud SQL instance.
When you clone a Cloud SQL instance, you create a new instance that is a copy of the source instance, but completely independent. After the cloning operation is complete, changes to the source instance are not reflected in the clone, and vice versa.
The source instance configuration (activation policy, database flags, connectivity, and so on) is copied to the clone. Instance IP addresses and replicas are not copied; you must configure these items explicitly on the clone. Cloning an instance does not clone any existing backups.
You cannot clone a replica. You can clone an instance that was cloned from another instance.
You can clone an instance from an earlier point in time, which helps you recover your database from a destructive event. For more information, see Performing a point-in-time recovery.
Cloning a Second Generation instance
Console
- Go to the Cloud SQL Instances page in the Google Cloud Console.
- Find the instance you want to clone, and open its
more actions menu at the far right of its listing.
- Click Create clone.
- In the Create clone page, update the instance ID if needed, and
click Create clone, leaving Clone latest state of instance
selected.
You are returned to the instance listing page while the clone initializes.
gcloud
Clone the instance:
gcloud sql instances clone [SOURCE_INSTANCE_NAME] [TARGET_INSTANCE_NAME]
cURL
Clone the instance:
gcloud auth login ACCESS_TOKEN="$(gcloud auth print-access-token)" curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ --header 'Content-Type: application/json' \ --data '{"cloneContext": {"destinationInstanceName": "[TARGET_INSTANCE_NAME]" }}' \ -X POST \ https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[SOURCE_INSTANCE_NAME]/clone
Cloning a First Generation instance
Console
- Go to the Cloud SQL Instances page in the Google Cloud Console.
-
Find the First Generation instance you want to clone, and open its
more actions menu at the far right of its listing.
- Select Create clone.
- If you do not have backups and binary logging enabled for the instance,
enable them now and click Continue, then Wait for backup.
After the backup window, restart this task from the beginning to create the
clone.
- When you see the Create clone page with the ability to update
the instance ID, do so if needed, and click Create clone.
You are returned to the instance listing page while the clone initializes.
gcloud
- Get the instance details:
gcloud sql instances describe [INSTANCE_NAME]
- Find the values for
binaryLogEnabled
andenabled
underbackupConfiguration
. - If either of the values are not Enabled, enable them now:
- Enable backups:
gcloud sql instances patch [INSTANCE_NAME] --backup-start-time [HH:MM]
- Enable binary logging:
gcloud sql instances patch [INSTANCE_NAME] --enable-bin-log
- Enable backups:
- If backups or binary logging were not enabled, wait for a backup to be taken.
This could take up to a day, depending on the backup window.
- Clone the instance:
gcloud sql instances clone [SOURCE_INSTANCE_NAME] [TARGET_INSTANCE_NAME]
cURL
- Get the values of the current backup configuration.
gcloud auth login ACCESS_TOKEN="$(gcloud auth print-access-token)" curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ -X GET \ https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[SOURCE_INSTANCE_NAME]?fields=settings
The output will contain the
backupConfiguration
:"backupConfiguration": [ { "kind": "sql#backupConfiguration", "startTime": "12:00", "enabled": false, "binaryLogEnabled": false }
- If either
enabled
orbinaryLogEnabled
arefalse
, use thepatch
method of the instances resource to enable them both. Specify the properties of the backup configuration you want to keep as is and those you want to change.To enable backups, set
enabled
totrue
and thestartTime
to a value which is the start of the backup window. To enable binary logging, setbinaryLogEnabled
totrue
.gcloud auth login ACCESS_TOKEN="$(gcloud auth print-access-token)" curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ --header 'Content-Type: application/json' \ --data '{"settings" : {"backupConfiguration" : {"startTime": "[HH:MM]", "enabled": true, "binaryLogEnabled": true}}}' \ -X PATCH \ https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[SOURCE_INSTANCE_NAME]
- If backups or binary logging were not enabled, wait for a backup to be taken.
This could take up to a day, depending on the backup window.
-
Clone the instance:
gcloud auth login ACCESS_TOKEN="$(gcloud auth print-access-token)" curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ --header 'Content-Type: application/json' \ --data '{"cloneContext": {"kind": "sql#cloneContext", "destinationInstanceName": "[TARGET_INSTANCE_NAME]" }}' \ -X POST \ https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[SOURCE_INSTANCE_NAME]/clone