Cloning Instances

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, Cloud SQL flags, authorized IP addresses, and so on) is copied to the clone. Instance IP addresses and replicas are not copied; you must configure these items explictly on the clone.

You cannot clone a replica. You can clone an instance that was cloned from another instance.

Cloning a Second Generation instance

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

    Go to the Cloud SQL Instances page

  2. Find the instance you want to clone, and open its more actions menu at the far right of its listing.

    Image of three-dot menu

  3. Click Create clone.
  4. 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:

ACCESS_TOKEN="$(gcloud auth application-default 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

  1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

    Go to the Cloud SQL Instances page

  2. Find the First Generation instance you want to clone, and open its more actions menu at the far right of its listing.

    Image of three-dot menu

  3. Select Create clone.
  4. 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.

    Image of backups enabled in clone stepper

  5. When you see the Create clone page with the ability to update the instance ID, do so if needed, and click Create clone.

    Image of final clone creation page

    You are returned to the instance listing page while the clone initializes.

gcloud

  1. Get the instance details:
    gcloud sql instances desribe [INSTANCE_NAME]
    
  2. Find the values for binaryLogEnabled and enabled under backupConfiguration.
  3. If either of the values are not Enabled, enable them now:
    1. Enable backups:
      gcloud sql instances patch [INSTANCE_NAME] --backup-start-time [HH:MM]
      
    2. Enable binary logging:
      gcloud sql instances patch [INSTANCE_NAME] --enable-bin-log
      
  4. 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.

  5. Clone the instance:
    gcloud sql instances clone [SOURCE_INSTANCE_NAME] [TARGET_INSTANCE_NAME]
    

cURL

  1. Get the values of the current backup configuration.
    ACCESS_TOKEN="$(gcloud auth application-default 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
     }
    
  2. If either enabled or binaryLogEnabled are false, use the patch 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 to true and the startTime to a value which is the start of the backup window. To enable binary logging, set binaryLogEnabled to true.

    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]
    
  3. 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.

  4. Clone the instance:
    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
    

Send feedback about...

Cloud SQL for MySQL