Create snapshots to periodically back up data from your zonal persistent disks or regional persistent disks.
You can create snapshots from disks even while they are attached to running instances. Snapshots are global resources, so you can use them to restore data to a new disk or instance within the same project. You can also share snapshots across projects.
Before you begin
- If you want to use the command-line examples in this guide:
- Install or update to the latest version of the gcloud command-line tool.
- Set a default region and zone.
- If you want to use the API examples in this guide, set up API access.
Creating snapshots
To prepare for creating persistent disk snapshots, do the following:
Review Best practices for persistent disk snapshots to prepare your disk for snapshotting.
Read Creating a persistent disk snapshot schedule to learn about creating a snapshot schedule and attaching it to your disks. Backing up your disks regularly with scheduled snapshots can reduce the risk of unexpected data loss.
Creating a snapshot of a zonal persistent disk
Console
- Go to the Create a Snapshot page in the Google Cloud Console.
Go to the Create a Snapshot page - Enter a snapshot Name.
- Optionally, enter a Description of the snapshot.
- Select the Source disk from the drop-down menu.
Determine your snapshot storage location. You can use the default storage location, or a custom storage location.
- Under Location, select whether you want to store your snapshot in a Multi-regional location or a Regional location.
- Select which specific region or multi-region that you want to use. To use the region or multi-region that is closest to your source disk, select Based on disk's location (default).
Click Create to create the snapshot.
gcloud
Create your snapshot in the default storage location or in a selected
storage location. Optionally, you can specify a snapshot name with the
--snapshot-names
flag. The default snapshot name is a random 12-character
alphanumeric string that starts with a letter.
To create a snapshot of a zonal persistent disk in the default storage location, use the
gcloud compute disks snapshot
command:gcloud compute disks snapshot DISK_NAME
Replace DISK_NAME with the name of the zonal persistent disk from which you want to create a snapshot.
Alternatively, to create a snapshot in a custom storage location, use the
--storage-location
flag to indicate where to store your snapshot:gcloud compute disks snapshot DISK_NAME \ --storage-location STORAGE_LOCATION
where:
- DISK_NAME is the name of the zonal persistent disk from which you want to create a snapshot.
- STORAGE_LOCATION is the Cloud Storage multi-region or the Cloud Storage region where you want to store your snapshot. You can specify only one storage location.
The gcloud
tool waits until the operation returns a status of
READY
or FAILED
, or reaches the maximum timeout and returns the last
known details of the snapshot.
API
Create your snapshot in the default storage location or in a selected
storage location. You must specify a snapshot name with the
name
field. The name must be 1-63 characters long, and comply with
RFC 1035.
To create your snapshot in the default storage location, make a
POST
request to thedisks.createSnapshot
method:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME/createSnapshot { "name": SNAPSHOT_NAME }
where:
- PROJECT_ID is your project ID.
- ZONE is the zone where your instance and disk are located.
- DISK_NAME is the name of the zonal persistent disk from which you want to create a snapshot.
- SNAPSHOT_NAME is a name for your snapshot.
Alternatively, to create a snapshot in a custom storage location, make a
POST
request to thedisks.createSnapshot
method and specify the storage location:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME/createSnapshot { "name": SNAPSHOT_NAME "storageLocations": STORAGE_LOCATION }
where:
- PROJECT_ID is your project ID.
- ZONE is the zone where your instance and disk are located.
- DISK_NAME is the name of the zonal persistent disk from which you want to create a snapshot.
- SNAPSHOT_NAME is a name for your snapshot.
- STORAGE_LOCATION is the Cloud Storage multi-region or the Cloud Storage region where you want to store your snapshot. You can specify only one storage location.
Creating a snapshot of a regional persistent disk
After you have prepared the disk, you can create a snapshot. When creating a snapshot of a regional persistent disk, you must indicate the region where the disk is located.
Console
- Go to the Create a Snapshot page in the Google Cloud Console.
Go to the Create a Snapshot page - Enter a snapshot Name.
- Optionally, enter a Description of the snapshot.
- Under Source disk, select an existing disk from which you want to create a snapshot.
Optionally, you can specify a custom storage location:
- Under Location, select whether you want to store your snapshot in a Multi-regional location or a Regional location.
- Select which specific region or multi-region that you want to use. To use the region or multi-region that is closest to your source disk, select Based on disk's location.
Click Create to create the snapshot.
gcloud
Create your snapshot in the default storage location or in a selected storage location.
To create a snapshot of a regional persistent disk in the default storage location, use the
gcloud compute disks snapshot
command.gcloud compute disks snapshot DISK_NAME \ --region=REGION
Replace the following:
DISK_NAME
: the name of the regional persistent disk from which you want to create a snapshotREGION
: the region where your regional persistent disk is located
Optionally, you can specify a custom storage location:
gcloud compute disks snapshot DISK_NAME --region REGION --storage-location STORAGE_LOCATION
where:
- DISK_NAME is the name of the regional persistent disk from which you want to create a snapshot.
- REGION is the region where your regional persistent disk is located.
- STORAGE_LOCATION is the Cloud Storage multi-region or the Cloud Storage region where you want to store your snapshot. You can specify only one storage location.
The
gcloud
command-line tool waits until the operation returns a status ofREADY
,FAILED
, or reaches the maximum timeout and returns the last known details of the snapshot.
API
To create a snapshot in the default storage location, make a
POST
request to theregionDisk.createSnapshot
method:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/disks/DISK_NAME/createSnapshot { "name": SNAPSHOT_NAME }
where:
- PROJECT_ID is your project ID.
- REGION is the region where your regional persistent disk is located.
- DISK_NAME is the name of the disk you want to snapshot.
- SNAPSHOT_NAME is a name for your snapshot.
Alternatively, to create a snapshot in a custom storage location, make a
POST
request to theregionDisk.createSnapshot
method and specify the storage location:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/VAR/disks/DISK_NAME/createSnapshot { "name": SNAPSHOT_NAME "storageLocations": STORAGE_LOCATION }
where:
- PROJECT_ID is your project ID.
- REGION is the region where your regional persistent disk is located.
- DISK_NAME is the name of the regional persistent disk from which you want to create a snapshot.
- SNAPSHOT_NAME is a name for your snapshot.
- STORAGE_LOCATION is the Cloud Storage multi-region or the Cloud Storage region where you want to store your snapshot. You can specify only one storage location.
Sharing snapshots across projects
To move disk data across projects, share a snapshot of the disk with users in
other projects. For example, to move the data from the disk disk-1
in
project-a
to a new disk disk-2
in project-b
, use the following steps. In
this example, disk-1
is located in zone us-west2-a
and instance-1
is
located in zone us-west2-b
.
gcloud
Create a snapshot of
disk-1
:gcloud compute disks snapshot disk-1 --project project-a --snapshot-names snapshot-1
Create a new disk in
project-b
that is based on the snapshot:gcloud compute disks create disk-2 --project project-b --source-snapshot projects/project-a/global/snapshots/snapshot-1
Attach the new disk to the instance
instance-1
inproject-b
.gcloud compute instances attach-disk instance-1 --project project-b --disk disk-2
Mount the disk. See steps 5-7 of Formatting and mounting a persistent disk.
API
Create a snapshot of
disk-1
by making aPOST
request to thedisks.createSnapshot
method.POST https://compute.googleapis.com/compute/v1/projects/project-a/zones/us-west2-a/disks/disk-1/createSnapshot { "name": "snapshot-1" }
Create a new disk
disk-2
inproject-b
that is based on the snapshot. Construct aPOST
request to create a zonal persistent disk using thedisks.insert
method.POST https://compute.googleapis.com/compute/v1/projects/project-b/zones/us-west2-b/disks { "name": "disk-2", "type": "zones/us-west2-b/diskTypes/DISK_TYPE" "sourceSnapshot": "projects/project-a/global/snapshots/snapshot-1" }
Replace the following:
- DISK_TYPE: The type of persistent disk. Either
pd-standard
,pd-ssd
, orpd-balanced
.
- DISK_TYPE: The type of persistent disk. Either
Attach the new disk to the instance
instance-1
inproject-b
. The disk must be in the same zone as the instance. Construct aPOST
request to theinstances.attachDisk
method, and include the URL to the zonal persistent disk that you just created.POST https://compute.googleapis.com/compute/v1/projects/project-b/zones/us-west2-b/instances/instance-1/attachDisk { "source": "/compute/v1/projects/project-b/zones/us-west2-b/disks/disk-2" }
Mount the disk. See steps 5-7 of Formatting and mounting a persistent disk.
To get a list of snapshots available to you, see Listing snapshots.
Restoring snapshots
To restore the contents of a snapshot to a new disk, you have the following options:
Create a new persistent disk from a snapshot: Create a new disk and select the snapshot as the Source.
Create a new instance from a snapshot: When you create a new instance, you can use snapshots to create boot disks and data disks for that instance. In the event of an outage, you can quickly recover your data by restoring one or more snapshots to the new persistent disks on a new instance. For more information, see Creating an instance from snapshots.
Creating a new persistent disk from a snapshot
The new persistent disk must be at least the same size as the original source disk for the snapshot. If you create a persistent disk that is larger than the original source disk for the snapshot, you must resize the file system on that persistent disk to include the additional disk space.
You can create a new zonal persistent disk from a given snapshot at most once every ten minutes. If you want to issue a burst of requests to snapshot your disks, you can issue at most 6 requests in 60 minutes. This limit does not apply when creating regional persistent disks from a snapshot. For more information, see Snapshot frequency limits.
Depending on your operating system and file system type, you might need to use a different file system resizing tool. For more information, see your operating system documentation.
Console
In the Google Cloud Console, go to the Snapshots page.
Find the name of the snapshot that you want to restore.
Go to the Disks page.
Click Create new disk.
Specify the following configuration parameters:
- A name for the disk.
- A type for the disk.
- Optionally, you can override the default region and zone selection. You can select any region and zone, regardless of the storage location of the source snapshot.
Under Source type, click Snapshot.
Select the name of the snapshot to restore.
Optional: Select a size in gigabytes, for the disk. This number must be equal to or larger than the original source disk for the snapshot. If you do not specify this value, the new disk is the same size as the original source disk for the snapshot.
Click Create to create the disk.
gcloud
Use the
gcloud compute snapshots list
command command to find the name of the snapshot you want to restore:gcloud compute snapshots list
Use the
gcloud compute snapshots describe
command command to find the size of the snapshot you want to restore:gcloud compute snapshots describe SNAPSHOT_NAME
Replace SNAPSHOT_NAME with the name of the snapshot being restored.
Use the
gcloud compute disks create
command command to create a new regional or zonal persistent disk from your snapshot. If you need an SSD persistent disk for additional throughput or IOPS, include the--type
flag and specifypd-ssd
.gcloud compute disks create DISK_NAME \ --size=DISK_SIZE \ --source-snapshot=SNAPSHOT_NAME \ --type=DISK_TYPE
Replace the following:
- DISK_NAME: the name of the new disk.
- DISK_SIZE: the size, in gigabytes, of the new disk. This number must be equal to or larger than the original source disk for the snapshot.
- SNAPSHOT_NAME: the name of the snapshot being restored.
- DISK_TYPE: the type
of the persistent disk. For example,
pd-ssd
.
API
Construct a
GET
request tosnapshots.list
to display the list of snapshots in your project.GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/snapshots
Replace PROJECT_ID with your project ID.
Construct a
POST
request to create a zonal persistent disk using thedisks.insert
method. Include thename
,sizeGb
, andtype
properties. To restore a disk using a snapshot, you must include thesourceSnapshot
property.POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks { "name": "DISK_NAME", "sizeGb": "DISK_SIZE", "type": "zones/ZONE/diskTypes/DISK_TYPE" "sourceSnapshot": "SNAPSHOT_NAME" }
Replace the following:
- PROJECT_ID: your project ID.
- ZONE the zone where your instance and new disk are located.
- DISK_NAME: the name of the new disk.
- DISK_SIZE: the size, in gigabytes, of the new disk.
- DISK_TYPE: the type
of the persistent disk. For example,
pd-ssd
. - SNAPSHOT_NAME: the source snapshot for the disk you are restoring.
Optionally, you can then attach the new disk to an existing instance.
Console
- Go to the VM instances page.
- Click the name of the instance where you want to restore your non-boot disk.
- At the top of the instance details page, click Edit.
- Under Additional disks, click Attach existing disk.
- Select the name of the new disk made from your snapshot.
- Click Done to attach the disk.
- At the bottom of the instance details page, click Save to apply your changes to the instance.
gcloud
Use the
gcloud compute instances attach-disk
command to attach your persistent disk to an instance:gcloud compute instances attach-disk INSTANCE_NAME \ --disk DISK_NAME
where:
- INSTANCE_NAME is the name of the instance.
- DISK_NAME is the name of the disk made from your snapshot.
API
Construct a
POST
request to theinstances.attachDisk
method, and include the URL to the zonal persistent disk that you just created from your snapshot.POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/attachDisk { "source": "/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME" }
where:
- PROJECT_ID is your project ID.
- ZONE is the zone where your instance and new disk are located.
- INSTANCE_NAME is the name of the instance where you are adding the new persistent disk.
- DISK_NAME is the name of the new disk.
After you create and attach a new disk to an instance, you must mount the disk so that the operating system can use the available storage space.
Deleting a snapshot
Compute Engine uses incremental snapshots so that each snapshot contains only the data that has changed since the previous snapshot. Because subsequent snapshots might require information stored in a previous snapshot, keep in mind that deleting a snapshot does not necessarily delete all the data on the snapshot. To learn more about snapshot deletion, see Snapshot deletion.
To delete a snapshot:
Console
- Go to the Snapshots page in the Google Cloud Console.
- Select one or more snapshots that you want to delete.
- At the top of the Snapshots page, click Delete.
gcloud
To delete a snapshot, use the
gcloud compute snapshots delete
command:
gcloud compute snapshots delete SNAPSHOT_NAME
where SNAPSHOT_NAME is the name of the snapshot being deleted.
API
Make a DELETE
request to
compute.snapshots.delete
method
to delete snapshots.
DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/snapshots/SNAPSHOT_NAME
where:
- PROJECT_ID is the name of your project.
- SNAPSHOT_NAME is the name of the snapshot to delete.
Deleting multiple snapshots based on a filter
Compute Engine uses incremental snapshots so that each snapshot contains only the data that has changed since the previous snapshot. Because subsequent snapshots might require information stored in a previous snapshot, deleting a snapshot does not necessarily delete all the data on the snapshot. To learn more about snapshot deletion, see Snapshot deletion.
The following sections describe how to delete a number of snapshots based on a
given filter. For more information on gcloud
tool filters, see gcloud
tool topic filters.
To delete the snapshots, use a combination of the gcloud compute snapshots list
command
with a given filter, and the gcloud compute snapshots delete
command
with xargs
:
gcloud compute snapshots list --filter="EXPRESSION" --uri |
xargs gcloud compute snapshots delete
Replace EXPRESSION
with a gcloud
topic filter.
For example, --filter="creationTimestamp>'2021-01-01'"
deletes all snapshots
created after 2021-01-01.
Listing snapshots
gcloud
To see a list of snapshots available to you in a particular project, use the
gcloud compute snapshots list
command.
gcloud compute snapshots list --project PROJECT_ID
where PROJECT_ID is the ID of the project.
API
To see a list of snapshot available to you in a particular project, make a
GET
request to the snapshots.list
method:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/snapshots
where PROJECT_ID is the ID of the project.
Viewing information about a snapshot
To list information about a particular snapshot, such as the creation time,
size, and source disk, use the
gcloud compute snapshots describe
command:
gcloud compute snapshots describe SNAPSHOT_NAME
where SNAPSHOT_NAME is the name of the snapshot.
What's next
- Learn how to create a snapshot schedule for persistent disks.
- Learn how to create a disk image from your snapshot.