How I can migrate a Persistence Disk from one project to another, in Compute Engine?
If you want to copy (non-root) persistent disk, do the steps below:
- Go to Google Developer Console and detach
<disk-name>
disk from the machine. - Use
gcloud
command-line utility to switch to the old project:gcloud config set project <old-project>
- Create an image of the
<disk-name>
disk:gcloud compute images create <image-name> --source-disk=<disk-name> --source-disk-zone=<zone>
- The output of the above command will give you a fully qualified link to the image (in the form
https://www.googleapis.com/compute/v1/projects/<project-name>/global/images/<image-name>
). Alternatively, run:gcloud compute images list --no-standard-images --uri | grep <image-name>
to find the link. - Switch to the new project:
gcloud config set project <new-project>
- Create a new disk from the image:
gcloud compute disks create <new-disk-name> --image=<link-to-the-image> --zone=<zone>
As above Mike Lutz's answer, except gcutil
is now deprecated, but the 2nd command can also be done with gcloud compute instance
are:
1) create your image from you PD (NB! read first! https://cloud.google.com/compute/docs/images#creating_an_image_from_a_root_persistent_disk)
$ gcloud compute images create [example-image] --source-disk [example-disk] --source-disk-zone ZONE --project="old-project-name"
2) Instantiate the image in the new project (goes without saying but you must have access to both projects)
$ gcloud compute instances create [example-instance-1] --project=[new-project-name] --image="https://www.googleapis.com/compute/v1/projects/[old-project-name]/global/images/[image-name]" --boot-disk-size [XXXGB] --machine-type=[machine-type] --network="default" --zone=[datacenter-zone]
- you can see the URL of your image in the Images tab under "Equivalent REST"
For additional instance config options see: https://cloud.google.com/sdk/gcloud/reference/compute/instances/create
In case anyone else lands here from a google search, today you can create a disk in one project directly from a snapshot in another project, without creating any temporary physical disks or images.
Create snapshot:
gcloud compute disks snapshot your-disk-name --snapshot-names=your-snapshot-name --project=source-project --zone=source-zone
Describe the snapshot and find the selfLink:
gcloud compute snapshots describe your-snapshot-name --project=source-project
Use the selfLink instead of a snapshot name when creating a disk in the new project:
gcloud compute disks create your-new-disk-name --project=destination-project --zone=destination-zone --source-snapshot=https://www.googleapis.com/compute/v1/projects/source-project/global/snapshots/your-snapshot-name