24
votes

I have a GCE instance that I have customised and uploaded various applications to (such as PHP apps running under Apache). I now want to duplicate this instance - i.e. everything on it. I originally thought clone might do this but I had a play around with it and it only seems to clone the instance config and not anything customised on it.

I've been googling it and it looks like what I need to do is create an image and use this image on a new instance or clone? Is that correct? If so, are there any could steps by steps out there to do this? I had a look at the Google page on images and it talks about having to terminate the instance to do this. I'm a bit wary of this. Maybe it's just the language used in the docs, but I don't want to lose my existing instance.

Also, will everything be stored on the image? So, for example, will the following all make it onto the image?

  • MySQL - config & databases schemas & data?
  • Apache - All installed apps under /var/www/html
  • PHP - php.ini, etc...
  • All other server configs/modifications?
6

6 Answers

36
votes

You can create a snapshot of the source instance, then create a new instance selecting the source snapshot as disk. It will replicate the server very fast. For other attached disks, you have to create a new disk and copy file by net (scp, rsync etc)

3
votes

In the Web Console, create a snapshot, then click on the snapshot and over CREATE INSTANCE button, you can customize the settings and then click where it says:

Equivalent REST or command line

and copy the command line, this will be your template.

From this, you can create a a BASH script (clone_instance.sh), I did something like this:

#!/bin/bash -e

snapshot="my-snapshot-name"
gcloud_account="[email protected]"

#clone 10 machines
for machine in 01 02 03 04 05 06 07 08 09 10
do 

    gcloud compute --project "myProject" disks create "instance-${machine}" \
        --size "220" --zone "us-east1-d" --source-snapshot "${snapshot}" \
        --type "pd-standard"

    gcloud compute --project "bizqualify" instances create "webscrape-${machine}" \
        --zone "us-east1-d" --machine-type "n1-highmem-4" --network "default" \
        --maintenance-policy "MIGRATE" \
        --service-account "[email protected]" \
        --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring.write","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" \
        --tags "http-server","https-server" \
        --disk "name=webscrape-${machine},device-name=webscrape-${machine},mode=rw,boot=yes,auto-delete=yes"

done

Now, in your terminal, you can execute your script

sh clone_instance.sh
2
votes

In case you have other disks attached, the best way without actually unmounting them is changing the path of how they're mounted in /etc/fstab.

If you use the UUID in fstab and use the same disks from snapshots (which will have the same UUIDs) then you can do the cloning without unmounting anything.

Just change each disk in fstab to use UUID like this

UUID=[UUID_VALUE] [MNT_DIR] ext4 discard,defaults,[NOFAIL] 0 2

you can get the UUID from

sudo blkid /dev/[DEVICE_ID]

if you're unsure about your DEVICE_ID you can use

sudo lsblk

to get the list of device ids used by your system.

0
votes

As was mentioned, if the source instance has a secondary disk attached, it is not possible to ssh into the new instance. I had to take a snapshot of a production instance, so I couldn't unmount the secondary disk without causing disruption. I was able to fix the problem by creating a disk from the snapshot, mounting the disk on another instance, removing any reference to the secondary disk, i.e., removing the entry from /etc/fstab. Once I had done that, I was able to use the disk as boot disk in a new instance, and ssh to it.

0
votes

You can use the GCP Import VM option, to import this machine back to the project.

0
votes

It's 2021 and this is now very simple:

  1. Click the VM Instance you want to clone
  2. Click "Create Machine Image" at the top
  3. From Machine Images on the left, open your new image and click "Create VM Instance"

This will clone the machine specs and data.