Back up and restore servers on Google Cloud
Use this guide to restore from server snapshots and create backups using scheduled snapshots at Google Cloud.
Create a snapshot
Create a snapshot of your server’s disk that can be used to restore a server’s data to the instant the snapshot was taken.
- In Google Cloud console, go to the VM instances page.
- Click on the name of the server you want to snapshot.
- Scroll down to the Storage section.
- In the Name column, click on the name of the disk. The disk will usually have the same name as the server.
- Click Create snapshot.
- If you don’t see Create snapshot, click on the three blue vertical dots to the left of the icon.
- In the Name field, enter a unique name for the snapshot that identifies
the purpose of the snapshot, for example:
my-first-server-before-upgrade-to-ubuntu-2404
my-first-server-after-upgrade-to-ubuntu-2404
- Click Create.
Set shell variables with the name of the server’s disk and the name of the snapshot to create. The name of a server’s disk is usually the same as the server’s name.
INSTANCE_NAME="my-first-server"DISK_NAME="${INSTANCE_NAME}"SNAPSHOT_NAME="snapshot-of-my-first-server"
Create a snapshot of the server’s disk using the
gcloud compute snapshots create
command.
gcloud compute snapshots create "${SNAPSHOT_NAME}" \ --source-disk="${DISK_NAME}"
Snapshot schedules
A snapshot schedule will automatically create hourly, daily, or weekly snapshots of a server’s disk. Additionally, a snapshot schedule can automatically delete old snapshots created by the snapshot schedule after a configurable number of days.
A snapshot schedule must be added to the disk of each server that should have snapshots taken based on the schedule.
You can use the same snapshot schedule on multiple servers.
Create a snapshot schedule
Create a daily snapshot schedule that retains snapshots for 90 days.
- In Google Cloud console, go to the Snapshots page.
- Click on Create snapshot schedule.
- If you don’t see Create snapshot schedule, click on the three blue vertical dots to the left of the icon.
- Set the following values:
- Name:
daily-snapshots
- Region: choose the region where you will create your server.
- Schedule frequency: select Daily.
- Autodelete snapshots after:
90
days.
- Name:
- Click Create at the bottom of the page.
Set shell variables with the name of the snapshot schedule and the number of days snapshots will be retained.
SNAPSHOT_SCHEDULE="daily-snapshots"RETENTION_DAYS="90"
Create the schedule using the
gcloud compute resource-policies create snapshot-schedule
command.
gcloud compute resource-policies create snapshot-schedule "${SNAPSHOT_SCHEDULE}" \ --max-retention-days="${RETENTION_DAYS}" \ --start-time="4:00" \ --daily-schedule
Add a snapshot schedule to a server
- In Google Cloud console, go to the VM instances page.
- Click on the name of the server to which you want to add a snapshot schedule.
- Scroll down to the Storage section.
- In the Name column, click on the name of the disk. The disk will usually have the same name as the server.
- Click Edit.
- If you don’t see Edit, click on the three blue vertical dots to the left of the icon.
- In the Snapshot schedule field, select the snapshot schedule to use.
- Scroll to the bottom of the page and click Save.
Set shell variables with the name of the server’s disk and the name of the snapshot schedule. The name of a server’s disk is usually the same as the server’s name.
INSTANCE_NAME="my-first-server"DISK_NAME="${INSTANCE_NAME}"SNAPSHOT_SCHEDULE="daily-snapshots"
Add the snapshot schedule to the server’s disk using the
gcloud compute disks add-resource-policies
command.
gcloud compute disks add-resource-policies "${DISK_NAME}" \ --resource-policies="${SNAPSHOT_SCHEDULE}"
See which servers have a snapshot schedule
- In Google Cloud console, go to the Snapshots page.
- Click on Snapshot schedules.
- Click on the name of the snapshot schedule.
- The In use by fields shows the disks that are using the snapshot schedule. The disk name is usually the same as the server name.
A snapshot schedule is a resource policy applied to a server’s disk. To see which servers have a snapshot schedule, view the resource policies of all disks.
gcloud compute disks list --format="value(name,resourcePolicies)"
Each line in the output of the above command will have either one or two values. The first value on each line will be the name of the disk. The second value on each line will be the disk’s resource policies such as snapshot schedules. If a line only shows the name of a disk, then that disk does not have a snapshot schedule.
Remove a snapshot schedule from a server
- In Google Cloud console, go to the VM instances page.
- Click on the name of the server to which you want to add a snapshot schedule.
- Scroll down to the Storage section.
- In the Name column, click on the name of the disk. The disk will usually have the same name as the server.
- Click Edit.
- If you don’t see Edit, click on the three blue vertical dots to the left of the icon.
- In the Snapshot schedule field, select No schedule.
- Scroll to the bottom of the page and click Save.
Set shell variables with the name of the server’s disk and the name of the snapshot schedule. The name of a server’s disk is usually the same as the server’s name.
INSTANCE_NAME="my-first-server"DISK_NAME="${INSTANCE_NAME}"SNAPSHOT_SCHEDULE="daily-snapshots"
Remove the snapshot schedule from the server’s disk using the
gcloud compute disks remove-resource-policies
command.
gcloud compute disks remove-resource-policies "${DISK_NAME}" \ --resource-policies="${SNAPSHOT_SCHEDULE}"
Delete a snapshot schedule
- In Google Cloud console, go to the Snapshots page.
- Click on Snapshot schedules.
- Click on the name of the snapshot schedule.
- Click Delete schedule.
- If Delete schedule is disabled, the snapshot schedule is still in use. Remove the snapshot schedule from all disks where the snapshot schedule is in use before deleting the snapshot schedule.
- If you don’t see Delete schedule, click on the three blue vertical dots to the left of the icon.
Set a shell variable with the name of the snapshot schedule.
SNAPSHOT_SCHEDULE="daily-snapshots"
Delete the snapshot schedule using the
gcloud compute resource-policies delete snapshot-schedule
command.
gcloud compute resource-policies delete snapshot-schedule "${SNAPSHOT_SCHEDULE}"
Restore a server from a snapshot
When restoring a server from a snapshot, a new server is created.
- In Google Cloud console, go to the VM instances page.
- Click Create instance.
- You’ll start out in the Machine configuration pane.
- Choose a Name for the server. Only lowercase letters (a-z), numbers, and hyphens are allowed.
- Choose a Region.
- Leave Zone set to Any.
- Choose a Machine type.
- Click on the OS and Storage pane.
- In the Operating system and storage section, click Change.
- Click Snapshots.
- Select the snapshot to use.
- For Boot disk type, select Balanced persistent disk.
- The Size (GB) was automatically changed to the disk size of the snapshot you selected. You can increase this value, if desired.
- Click on the Data protection pane.
- Select Snapshot schedules.
- Choose the schedule named daily-snapshots.
- Click Create at the bottom of the page.
Set shell variables with the instance name of the server to create, the machine type, the new server’s disk size in gigabytes, and the name of the snapshot the server’s disk will be created from.
INSTANCE_NAME="my-second-server"DISK_SIZE="50"MACHINE_TYPE="e2-standard-2"SNAPSHOT_NAME="snapshot-of-my-first-server"
gcloud compute instances create "${VM_NAME}" \ --machine-type="${MACHINE_TYPE}" \ --source-snapshot="${SNAPSHOT_NAME}" \ --boot-disk-size="${DISK_SIZE}" \ --boot-disk-type="pd-balanced"
Delete snapshots
- In Google Cloud console, go to the Compute Engine Snapshots page.
- Check the box next to each snapshot you want to delete.
- Click Delete.
- If Delete is disabled, confirm that you have checked the box next to at least one snapshot.
- If you don’t see Delete, click on the three blue vertical dots to the left of the icon.
First, get a list of all snapshots using the
gcloud compute snapshots list
command.
gcloud compute snapshots list
Next, repeat the following command for each snapshot you want to delete.
Replace SNAPSHOT_NAME
with the name of the snapshot shown in the output
of the above command.
gcloud compute snapshots delete SNAPSHOT_NAME