Reputation: 421
I created a snapshot of a VM instance via cloud console. I would like to know how I can restore an instance using a snapshot. The documentation for compute engine is not very helpful. The instance runs on Ubuntu. Thanks.
Upvotes: 18
Views: 20026
Reputation: 2106
I felt I would expand on it for the full steps for how to snapshot/restore a compute instance, since it illustrates a few things and might relieve some frustration in the comments about this "simple operation".
In GCP, one manages the disks, snapshots, and instances separately. To "snapshot and restore an instance", one really snapshots the disk(s) and then swaps in new disks created from the snapshots.
There are 6 steps:
Create a snapshot from the instance's disk:
gcloud compute disks snapshot \
DISK_NAME \
--snapshot-names SNAPSHOT_NAME
To restore a snapshot, first create a new disk from the snapshot:
gcloud compute disks create \
NEW_DISK_NAME \
--source-snapshot SNAPSHOT_NAME
Stop the instance:
The snapshot and new disk can be made with the instance still running, but you must stop it to swap disks.
gcloud compute instances stop \
INSTANCE_NAME
Detatch the current disk from the instance:
Instances can only have one boot disk
gcloud compute instances detach-disk \
INSTANCE_NAME \
--disk DISK_NAME
Attach the new disk to the instance:
gcloud compute instances attach-disk \
INSTANCE_NAME \
--disk NEW_DISK_NAME \
--boot
Start the instance
gcloud compute instances start \
INSTANCE_NAME
If the instance has multiple disks, then — to maintain consistency of data — it may be necessary to stop the instance for multiple snapshots. That would depend on the application and the how the data are distributed over the disks. Also be mindful of which is the --boot
disk.
Some more commands are helpful in working with disks and snapshots
gcloud compute disks list
gcloud compute disks delete DISK_NAME
gcloud compute snapshots list
gcloud compute snapshots delete SNAPSHOT_NAME
Once you have a snapshot, you can create a new instance from it. But as Sirui pointed out, creating new disks from snapshots and swapping them in has the advantage that the compute instance itself is kept, with the same IP address and other attributes.
If it were a spot instance, or you must not stop the old instance, then creating a new instance may be better.
The gcloud commands allow this separation of concern, so that you can do snapshot operations and create new instances, without stopping the current instances. In a production environment that's the more common scenario: you can't always stop an instance. But it is still possible, and it's conceptually only 3 steps to do it.
Upvotes: 0
Reputation: 636
To restore an instance from a snapshot without deleting/re-creating the instance:
Shut down the instance and detach the boot disk: gcloud compute instances detach-disk INSTANCE_NAME --disk BOOT_DISK_NAME
Create a new disk from the snapshot: gcloud compute disks create DISK_NAME --source-snapshot SNAPSHOT_NAME
Attach the disk created from step 2 as the boot disk: gcloud compute instances attach-disk INSTANCE_NAME --disk DISK_NAME --boot
Restoring the instance without deleting/re-creating the instance means that after restore, the instance will keep its IP address and other properties like tags, labels, etc.
Upvotes: 28
Reputation: 21
If you want to do it in the web interface, it's pretty easy. Edit your VM instance. Scroll down to your boot disk and click "x" next to the boot volume, then click create button. In the next window give your new volume a meaningful name, set up your snapshot schedule, and then under "source type" select "Snapshot". Choose your snapshot from the dropdown. If you have a customer managed/supplied key (not recommended) select it, otherwise leave it as google-managed key. Click create. Depending on disk size it might take a while. Be patient and let it finish the setup and then start your instance once it is al complete.
Upvotes: 2
Reputation: 352
In Console, you can go to VM Instances from Compute Engine tab. There you click on '+Create Instance' and there in section of boot-disk, you can navigate to 'snapshots' tab and select the snapshot you took.
Like this from console, you can Restore your instance.
Let me know, if this doesn't work for you!
Upvotes: 5
Reputation: 636
Try using:
gcloud compute disks \
create <NEW_INSTANCE_NAME> \
--source-snapshot <SNAPSHOT_NAME> \
--type pd-ssd \
--zone <ZONE>
You could find useful instruction here.
Upvotes: 4