Cloning Instances

This page describes how to clone a Google Cloud SQL instance.

When you clone a Google 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, and so on) is copied to the clone. Authorized IP addresses and authorized App Engine applications are also copied over. Instance IP addresses and replicas are not copied; you must configure these items explictly on the clone.

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 {[v2inst_short}} instance you want to clone, and open its dropdown menu at the far right of its listing.

    Image of three-dot menu

  3. Click Create clone, leaving Clone latest state of instance selected.
  4. In the Create clone page, update the instance ID 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

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": {"kind": "sql#cloneContext",
              "destinationInstanceName": "[TARGET_INSTANCE_NAME]" }}' \
     https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[SOURCE_INSTANCE_NAME]/clone \
     -X POST

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 dropdown 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
    
To see how the underlying REST API request is constructed for this task, see the APIs Explorer on the Instances:clone page.

Send feedback about...

Cloud SQL for MySQL