GCP Restore

How to restore from GCP Snapshots. Let’s say you have an instance called webserver with 2 disks.

Restore from snapshots:

gcloud compute disks create webserver-boot-r \
--project=your-project \
--source-snapshot=webserver-boot-190616 \
--zone=us-central1-a
gcloud compute disks create webserver-data-r \
--project=your-project \
--source-snapshot=webserver-data-190616 \
--zone=us-central1-a

Stop instance. Detach and attach disks:

gcloud compute instances detach-disk webserver --disk=webserver-boot --zone us-central1-a
gcloud compute instances attach-disk webserver --disk=webserver-boot-r --zone us-central1-a --boot
gcloud compute instances detach-disk webserver --disk=webserver-data --zone us-central1-a
gcloud compute instances attach-disk webserver --disk=webserver-data-r --zone us-central1-a

Boot instance.

GCP List Snapshots

Here are several examples how to display GCP snapshots using Google SDK (CLI – command line interface).

# list snapshots
gcloud compute snapshots list --project my-project --filter="name~'my-server-boot'"
# limit 10
gcloud compute snapshots list --project my-project --filter="name~'my-server-boot'" --limit 10
# reverse order
gcloud compute snapshots list --project my-project --filter="name~'my-server-boot'" --limit 10 --sort-by="~name"
# display uri
gcloud compute snapshots list --project my-project --filter="name~'my-server-boot'" --limit 10 --sort-by="~name" --uri

GCP Create Images

Here’s how to create a GCP Disk Image from a Boot Drive.

  • First, stop the instance.
  • If you can’t, stop the applications/database from writing to disks.
  • Run sudo sync .

Create From Disk

gcloud compute images create the-new-image-name \
   --source-disk the-source-boot-disk \
   --source-disk-zone us-central1-a \
   --family the-image-family \
   [--force]

Create From Image

gcloud compute images create the-new-image-name \
  --source-image the-source-image \
  --source-image-project the-project-where-the-image-is-located \
  --family the-image-family

Create From Snapshot

gcloud compute images create the-new-image-name \
  --source-snapshot the-source-snapshot

For more info.

GCP Change VPC of Instance

The following are steps on how to change the network of an instance from one VPC to another. This is assuming you want to keep the same server name, but will need to make a network change. Unfortunately, changing networks will require creating a new instance. The following are the steps to make that change.

  1. Keep track of all the disks.
  2. Keep all disks including boot.
  3. Stop the instance.
  4. Create an image of the boot drive. Wait for it to complete.
  5. Create a similar instance, but do not execute.
  6. Make the network changes. Save the GCloud CLI command.
  7. Delete the instance.
  8. Run the saved command from step 4. This creates a new instance.
  9. Stop the instance.
  10. Detach the boot disk.
  11. Attach all the other disks.
  12. Boot the instance.