Create snapshots to periodically backup data from these types of 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
-
Set up authentication.
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
Terraform
To use the Terraform samples on this page from a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your Google Account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
Go
To use the Go samples on this page from a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your Google Account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
Java
To use the Java samples on this page from a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your Google Account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
Node.js
To use the Node.js samples on this page from a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your Google Account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
Python
To use the Python samples on this page from a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your Google Account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
-
Prepare for creating snapshots
To prepare for creating Persistent Disk or Hyperdisk snapshots, do the following:
Review Best practices for Compute Engine disk snapshots to prepare your disk for snapshotting.
If you want to customize the default storage location for all your new snapshots, update the snapshot settings for your project (Preview). Google Cloud maintains a predefined default storage location value until you update the snapshot settings for the first time. This predefined location is the nearest multi-region to the source disk.
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.
Create a snapshot of Persistent Disk volume
The instructions for creating snapshots of Persistent Disk volumes are different, depending on whether you are creating a snapshot of zonal Persistent Disk or regional Persistent Disk.
Create a snapshot of a zonal Persistent Disk volume
Console
-
In the Google Cloud console, go to the VM instances page.
Go to VM instances
The remaining steps will appear automatically in the Google Cloud console. - Select the project that contains your VM instances.
- In the Name column, click the name of the VM that has the persistent disk to back up.
-
In
Storage :- To back up the boot disk, in the Boot disk section, click the Name of the boot disk.
- To back up an attached persistent disk, in Additional disks, click the Name of the attached persistent disk.
-
Click
Create snapshot . -
In Name, enter a unique name to help identify the purpose of the snapshot, for example:
boot-disk-snapshotattached-persistent-disk-snapshot
-
In Type, the default is a regular snapshot, which is best for long-term back up and
disaster recovery.
Choose Archive snapshot for more cost-efficient data retention.
-
In the Location section, choose your snapshot storage location. The predefined or customized default location defined in your snapshot settings is automatically selected. Optionally, you can override the snapshot settings and store your snapshots in a custom storage location by doing the following:
-
Choose the type of storage location that you want for your snapshot.
-
Choose Multi-regional
for higher availability at a higher cost.
- Choose Regional snapshots for more control over the physical location of your data at a lower cost.
-
Choose Multi-regional
for higher availability at a higher cost.
-
In the Select location field, select the 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.
-
- To create a manual snapshot, click Create.
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Create your snapshot using the storage location policy defined by your snapshot settings (Preview) or using an alternative storage location of your choice. For more information, see Choose your snapshot storage location.
To create a snapshot of a zonal Persistent Disk volume in the predefined or customized default location configured in your snapshot settings, use the
gcloud compute snapshots createcommand.gcloud compute snapshots create SNAPSHOT_NAME \ --source-disk SOURCE_DISK \ --snapshot-type SNAPSHOT_TYPE \ --source-disk-zone SOURCE_DISK_ZONEAlternatively, to override the snapshot settings and create a snapshot in a custom storage location, include the
--storage-locationflag to indicate where to store your snapshot:gcloud compute snapshots create SNAPSHOT_NAME \ --source-disk SOURCE_DISK \ --source-disk-zone SOURCE_DISK_ZONE \ --storage-location STORAGE_LOCATION \ --snapshot-type SNAPSHOT_TYPE
Replace the following:
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_DISK: The name of the zonal Persistent Disk volume from which you want to create a snapshot.
- SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created. Choose Archive for more cost-efficient data retention.
- SOURCE_DISK_ZONE: The zone of the zonal Persistent Disk volume from which you want to create a snapshot.
- STORAGE_LOCATION: For custom storage locations, this 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.
Use the
--storage-locationflag only when you want to override the predefined or customized default storage location configured in your snapshot settings.
The gcloud CLI waits until the operation returns a status of
READYorFAILED, or reaches the maximum timeout and returns the last known details of the snapshot.
Terraform
To create a snapshot of the zonal Persistent Disk volume, use the
google_compute_snapshot resource.
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
Go
Go
Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Java
Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Node.js
Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Node.js API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Python
Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
REST
Create your snapshot in the storage location policy defined by your snapshot settings (Preview) or using an alternative storage location of your choice. For more information, see Choose your snapshot storage location.
To create your snapshot in the predefined or customized default location configured in your snapshot settings, make a
POSTrequest to thesnapshots.insertmethod:POST https://compute.googleapis.com/compute/v1/projects/DESTINATION_PROJECT_ID/global/snapshots { "name": SNAPSHOT_NAME "sourceDisk": "projects/SOURCE_PROJECT_ID/zones/SOURCE_ZONE/disks/SOURCE_DISK_NAME "snapshotType": SNAPSHOT_TYPE }Replace the following:
- DESTINATION_PROJECT_ID: The ID of project in which you want to create the snapshot.
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_PROJECT_ID: The ID of the source disk project.
- SOURCE_ZONE: The zone of the source disk.
- SOURCE_DISK_NAME: The name of the Persistent Disk volume from which you want to create a snapshot.
- SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created.
Alternatively, to override the snapshot settings and create a snapshot in a custom storage location, make a
POSTrequest to thesnapshots.insertmethod and include thestorageLocationsproperty in your request:POST https://compute.googleapis.com/compute/v1/projects/DESTINATION_PROJECT_ID/global/snapshots { "name": SNAPSHOT_NAME "sourceDisk": "projects/SOURCE_PROJECT_ID/zones/SOURCE_ZONE/disks/SOURCE_DISK_NAME "snapshotType": SNAPSHOT_TYPE "storageLocations": STORAGE_LOCATION }Replace the following:
- DESTINATION_PROJECT_ID: The ID of project in which you want to create the snapshot.
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_PROJECT_ID: The ID of the source disk project.
- SOURCE_ZONE: The zone of the source disk.
- SOURCE_DISK_NAME: The name of the Persistent Disk volume from which you want to create a snapshot.
- SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created.
STORAGE_LOCATION: Optional. The Cloud Storage multi-region or the Cloud Storage region where you want to store your snapshot. You can specify only one storage location.
Use the
storageLocationsparameter only when you want to override the predefined or customized default storage location configured in your snapshot settings.
Go
Go
Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Java
Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Node.js
Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Node.js API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Python
Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Create a snapshot of a regional Persistent Disk volume
You can create snapshots of your regional Persistent Disk volume in one of the following ways:
- By using the data on the source disk. To use this snapshot creation method, you must have a synced zonal replica available.
- By using the replica recovery checkpoint of a degraded disk. You can create snapshots from a checkpoint only by using the Google Cloud CLI or the Compute Engine REST API.
Required roles
To get the permissions that you need to create a regional Persistent Disk snapshot, ask your administrator to grant you the following IAM roles:
-
To create a regional Persistent Disk snapshot:
Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1) on the project
For more information about granting roles, see Manage access.
These predefined roles contain the permissions required to create a regional Persistent Disk snapshot. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to create a regional Persistent Disk snapshot:
-
To create a regional Persistent Disk snapshot using the data on the disk:
-
on the projectcompute.snapshots.create -
on the source VMcompute.instances.useReadOnly -
on the diskcompute.disks.createSnapshot
-
-
To create a regional Persistent Disk snapshot from a replica recovery checkpoint:
-
on the projectcompute.snapshots.create -
on the diskcompute.disks.createSnapshot
-
You might also be able to get these permissions with custom roles or other predefined roles.
After you prepare the disk, you can create a snapshot. When creating a snapshot of a regional Persistent Disk, you must specify 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.
Select a Snapshot type. The default is a regular snapshot, which is best for long-term back up and disaster recovery.
Choose Archive snapshot for more cost-efficient data retention.
Optionally, enter a Description of the snapshot.
Under Source disk, select an existing disk from which you want to create a snapshot.
In the Location section, choose your snapshot storage location. The predefined or customized default location defined in your snapshot settings is automatically selected. Optionally, you can override the snapshot settings and store your snapshots in a custom storage location by doing the following:
Choose the type of storage location that you want for your snapshot.
- Choose Multi-regional for higher availability at a higher cost.
- Choose Regional snapshots for more control over the physical location of your data at a lower cost.
In the Select location field, select the 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
You can create a snapshot either by using the source disk's data or its replica recovery checkpoint.
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
From disk data
You can create a snapshot from your disk's data using the storage location policy defined by your snapshot settings (Preview) or using an alternative storage location of your choice. For more information, see Choose your snapshot storage location.
gcloud compute snapshots create SNAPSHOT_NAME \
--source-disk SOURCE_DISK \
--source-disk-region=SOURCE_DISK_REGION \
--snapshot-type=SNAPSHOT_TYPE
Alternatively, you can override the snapshot settings and store your snapshots in a custom storage location.
gcloud compute snapshots create SNAPSHOT_NAME \ --source-disk SOURCE_DISK \ --source-disk-region=SOURCE_DISK_REGION \ --storage-location STORAGE_LOCATION \ --snapshot-type SNAPSHOT_TYPEReplace the following:
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_DISK: The name of the regional Persistent Disk volume from which you want to create a snapshot.
- SOURCE_DISK_REGION: The region of the regional Persistent Disk volume from which you want to create a snapshot.
- SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created.
STORAGE_LOCATION: The Cloud Storage multi-region or the Cloud Storage region where you want to store your snapshot. You can specify only one storage location.
Use the
--storage-locationflag only when you want to override the predefined or customized default storage location configured in your snapshot settings.SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created.
From checkpoint
Alternatively, you can create your snapshot using the replica recovery checkpoint (Preview) of a degraded disk . Your snapshot gets created as long as your incomplete replica is available.
To create a snapshot using the replica recovery checkpoint, use the
gcloud compute snapshots create command
.
Include the --source-disk-for-recovery-checkpoint flag to
specify that you want to create the snapshot using a replica recovery
checkpoint. Exclude the --source-disk and
--source-disk-region parameters.
gcloud compute snapshots create SNAPSHOT_NAME \
--source-disk-for-recovery-checkpoint SOURCE_DISK \
--source-disk-for-recovery-checkpoint-region SOURCE_REGION \
--storage-location STORAGE_LOCATION \
--snapshot-type SNAPSHOT_TYPE
Replace the following:
DESTINATION_PROJECT_ID: The ID of project in which you want to create the snapshot.SNAPSHOT_NAME: A name for the snapshot.SOURCE_DISK: The name or full path of the source disk that you want to use to create the snapshot. To specify the full path of a source disk, use the following syntax:projects/SOURCE_PROJECT_ID/regions/SOURCE_REGION/disks/SOURCE_DISK_NAME
If you specify the full path to the source disk, you can exclude the
--source-disk-for-recovery-checkpoint-regionflag. If you specify only the disk's name, then you must include this flag.To create a snapshot from the recovery checkpoint of a source disk in a different project, you must specify the full path to the source disk.
SOURCE_PROJECT_ID: The project ID of the source disk whose checkpoint you want to use to create the snapshot.SOURCE_REGION: The region of the source disk whose checkpoint you want to use to create the snapshot.SOURCE_DISK_NAME: The name of the source disk whose checkpoint you want to use to create the snapshot.STORAGE_LOCATION: Optional. The Cloud Storagemulti-region or the Cloud Storageregion where you want to store your snapshot. You can specify only one storage location.
Use the--storage-locationflag only if you want to override the predefined or customized default storage location configured in your snapshot settings.SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created.
You can use replica recovery checkpoint to create a snapshot only on degraded disks. If you try to create a snapshot from a replica recovery checkpoint when the device is fully replicated, you see the following error message:
The device is fully replicated and should not create snapshots out of a recovery checkpoint. Please create regular snapshots instead.
The Google Cloud CLI waits until the operation returns a status of
READY, FAILED, or reaches the maximum timeout and returns the last
known details of the snapshot.
Terraform
The Terraform provider for Google Cloud doesn't support creating a snapshot of a regional Persistent Disk volume. To track this limitation, see the issue on GitHub.
Go
Go
Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Java
Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Node.js
Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Node.js API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Python
Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
REST
You can create a snapshot either by using the source disk's data or its replica recovery checkpoint.
From disk data
You can create a snapshot from your disk's data using the storage location policy defined by your snapshot settings (Preview) or using an alternative storage location of your choice. For more information, see Choose your snapshot storage location.
To create a snapshot in the predefined or customized default location configured in your snapshot settings (Preview), make a
POSTrequest to thesnapshots.insertmethod.POST https://compute.googleapis.com/compute/v1/projects/DESTINATION_PROJECT_ID/global/snapshots { "name": SNAPSHOT_NAME "sourceDisk": "projects/SOURCE_PROJECT_ID/regions/SOURCE_REGION/disks/SOURCE_DISK_NAME "snapshotType": SNAPSHOT_TYPE }Replace the following:
- DESTINATION_PROJECT_ID: The ID of project in which you want to create the snapshot.
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_PROJECT_ID: The ID of the source disk project.
- SOURCE_REGION: The region of the source disk.
- SOURCE_DISK_NAME: The name of the Persistent Disk volume from which you want to create a snapshot.
- SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created.
Alternatively, to override the snapshot settings and create a snapshot in a custom storage location, make a
POSTrequest to thesnapshots.insertmethod and include thestorageLocationsproperty in your request.:POST https://compute.googleapis.com/compute/v1/projects/DESTINATION_PROJECT_ID/global/snapshots { "name": SNAPSHOT_NAME "sourceDisk": "projects/SOURCE_PROJECT_ID/regions/SOURCE_REGION/disks/SOURCE_DISK_NAME "snapshotType": SNAPSHOT_TYPE "storageLocations": STORAGE_LOCATION }Replace the following:
- DESTINATION_PROJECT_ID: The ID of project in which you want to create the snapshot.
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_PROJECT_ID: The ID of the source disk project.
- SOURCE_REGION: The region of the source disk.
- SOURCE_DISK_NAME: The name of the Persistent Disk volume from which you want to create a snapshot.
STORAGE_LOCATION: Optional. The Cloud Storage multi-region or the Cloud Storage region where you want to store your snapshot. You can specify only one storage location.
Use the
storageLocationsparameter only when you want to override the predefined or customized default storage location configured in your snapshot settings.SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created.
From checkpoint
Alternatively, you can create your snapshot using the replica recovery checkpoint of a degraded disk. Your snapshot gets created as long as your incomplete replica is available.
To create a snapshot using the replica recovery checkpoint, make a
POST request to the
snapshots.insert method.
Exclude the sourceDisk parameter and instead include the
sourceDiskForRecoveryCheckpoint parameter to specify that
you want to create the snapshot using the checkpoint.
POST https://compute.googleapis.com/compute/v1/projects/DESTINATION_PROJECT_ID/global/snapshots
{
"name": "SNAPSHOT_NAME"
"sourceDiskForRecoveryCheckpoint": "projects/SOURCE_PROJECT_ID/regions/SOURCE_REGION/disks/SOURCE_DISK_NAME"
"storageLocations": "STORAGE_LOCATION"
"snapshotType": "SNAPSHOT_TYPE"
}
Replace the following:
DESTINATION_PROJECT_ID: The ID of project in which you want to create the snapshot.SNAPSHOT_NAME: A name for the snapshot.SOURCE_DISK: The name or full path of the source disk that you want to use to create the snapshot. To specify the full path of a source disk, use the following syntax:projects/SOURCE_PROJECT_ID/regions/SOURCE_REGION/disks/SOURCE_DISK_NAME
If you specify the full path to the source disk, you can exclude the
--source-disk-for-recovery-checkpoint-regionflag. If you specify only the disk's name, then you must include this flag.To create a snapshot from the recovery checkpoint of a source disk in a different project, you must specify the full path to the source disk.
SOURCE_PROJECT_ID: The project ID of the source disk whose checkpoint you want to use to create the snapshot.SOURCE_REGION: The region of the source disk whose checkpoint you want to use to create the snapshot.SOURCE_DISK_NAME: The name of the source disk whose checkpoint you want to use to create the snapshot.STORAGE_LOCATION: Optional. The Cloud Storagemulti-region or the Cloud Storageregion where you want to store your snapshot. You can specify only one storage location.
Use thestorageLocationsparameter only if you want to override the predefined or customized default storage location configured in your snapshot settings.SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created.
You can use replica recovery checkpoint to create a snapshot only on degraded disks. If you try to create a snapshot from a replica recovery checkpoint when the device is fully replicated, you see the following error message:
The device is fully replicated and should not create snapshots out of a recovery checkpoint. Please create regular snapshots instead.
Go
Go
Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Java
Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Node.js
Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Node.js API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Python
Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Create a snapshot of a Hyperdisk
You can create your snapshot using the storage location policy defined by snapshot settings (Preview) or using a custom storage location. You must specify a snapshot name. The name must be 1-63 characters long, and comply with RFC 1035.
Console
In the Google Cloud console, go to the VM instances page.
Select the project that contains your VM instances.
In the Name column, click the name of the VM that has the Hyperdisk to back up.
In Storage, in Additional disks, click the Name of the attached Hyperdisk.
Click Create snapshot.
In Name, enter a unique name to help identify the purpose of the snapshot, for example
hyperdisk-data-snapshot.In Type, the default is a regular snapshot, which is best for long-term back up and disaster recovery.
In the Location section, choose your snapshot storage location. The predefined or customized default location defined in your snapshot settings is automatically selected. Optionally, you can override the snapshot settings and store your snapshots in a custom storage location by doing the following:
Choose the type of storage location that you want for your snapshot.
- Choose Multi-regional for higher availability at a higher cost.
- Choose Regional snapshots for more control over the physical location of your data at a lower cost.
In the Select location field, select the 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.
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Create your snapshot using the storage location policy defined by snapshot settings (Preview) or using a custom storage location. For more information, see Choose your snapshot storage location.
To create a snapshot of a Hyperdisk in the predefined or customized default location configured in your snapshot settings, use the
gcloud compute snapshots createcommand.To override your snapshot settings and create a snapshot in a custom storage location, include the
--storage-locationflag to indicate where to store your snapshot:gcloud compute snapshots create SNAPSHOT_NAME \ --source-disk SOURCE_DISK \ --source-disk-zone SOURCE_DISK_ZONE \ --storage-location STORAGE_LOCATION \ --snapshot-type SNAPSHOT_TYPE
Replace the following:
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_DISK: The name of the Hyperdisk from which you want to create a snapshot.
- SOURCE_DISK_ZONE: The zone of the Hyperdisk from which you want to create a snapshot.
- STORAGE_LOCATION: Optional. The Cloud Storage multi-region or the Cloud Storage region where you want to store your snapshot. You can specify only one storage location.
Use the
--storage-locationparameter only when you want to override the predefined or customized default storage location configured in your snapshot settings. - SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created. Choose archive snapshots for more cost-efficient data retention.
The gcloud CLI waits until the operation returns a status of
READYorFAILED, or reaches the maximum timeout and returns the last known details of the snapshot.
REST
Create your snapshot using the storage location policy defined by your snapshot settings (Preview) or using an alternative storage location of your choice. For more information, see Choose your snapshot storage location.
To create your snapshot in the predefined or customized default storage location configured in your snapshot settings (Preview), make a
POSTrequest to thesnapshots.insertmethod.To override the snapshot settings and create a snapshot in a custom storage location, include the
storageLocationsproperty in your request.POST https://compute.googleapis.com/compute/v1/projects/DESTINATION_PROJECT_ID/global/snapshots { "name": SNAPSHOT_NAME "sourceDisk": "projects/SOURCE_PROJECT_ID/zones/SOURCE_ZONE/disks/SOURCE_DISK_NAME "snapshotType": SNAPSHOT_TYPE "storageLocations": STORAGE_LOCATION }Replace the following:
- DESTINATION_PROJECT_ID: The ID of project in which you want to create the snapshot.
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_PROJECT_ID: The ID of the source disk project.
- SOURCE_ZONE: The zone of the source disk.
- SOURCE_DISK_NAME: The name of the Hyperdisk from which you want to create a snapshot.
- SNAPSHOT_TYPE: The snapshot type, either STANDARD or ARCHIVE. If a snapshot type is not specified, a STANDARD snapshot is created. Choose archive snapshots for more cost-efficient data retention.
STORAGE_LOCATION: Optional. The Cloud Storage multi-region or the Cloud Storage region where you want to store your snapshot. You can specify only one storage location.
Use the
storageLocationsparameter only when you want to override the predefined or customized default storage location configured in your snapshot settings.
Delete a snapshot
Compute Engine uses differential 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
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
To delete a snapshot, use the
gcloud compute snapshots deletecommand:gcloud compute snapshots delete SNAPSHOT_NAME
Replace SNAPSHOT_NAME with the name of the snapshot being deleted.
REST
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
Replace the following:
- PROJECT_ID is the name of your project.
- SNAPSHOT_NAME is the name of the snapshot to delete.
Go
Java
Node.js
Python
REST
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
Replace the following:
- PROJECT_ID is the name of your project.
- SNAPSHOT_NAME is the name of the snapshot to delete.
Delete multiple snapshots based on a filter
Compute Engine uses differential 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 CLI filters, see gcloud CLI 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.
List snapshots
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
To see a list of snapshots available to you in a particular project, use the
gcloud compute snapshots listcommand.gcloud compute snapshots list --project PROJECT_ID
where PROJECT_ID is the ID of the project.
REST
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.
View 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.
Share snapshot data across projects in the same organization
To move data from a disk in one project to a disk in a different project within the same organization, use the following process:gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Create a disk snapshot in the destination project by using the
gcloud compute snapshots createcommand. For example, to create a snapshot of zonal Persistent Disk, use the following command:gcloud compute snapshots create SNAPSHOT_NAME \ --source-disk https://www.googleapis.com/compute/v1/projects/SOURCE_PROJECT_ID/zones/ZONE/disks/SOURCE_DISK_NAME \ --project DESTINATION_PROJECT_IDReplace the following:
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_PROJECT_ID: The ID of the source disk project.
- ZONE: The zone of the source disk.
- SOURCE_DISK_NAME: The name of the source disk.
- DESTINATION_PROJECT_ID: The ID of the destination project for the new snapshot.
In the destination project, create a zonal or regional Persistent Disk or a Hyperdisk volume that's based on the snapshot by using the
gcloud compute disks createcommand:gcloud compute disks create DISK_NAME \ --source-snapshot SNAPSHOT_NAME \ --project DESTINATION_PROJECT_IDReplace the following:
- DISK_NAME: A name for the new disk.
- SNAPSHOT_NAME: The name of the snapshot.
- DESTINATION_PROJECT_ID: The ID of the destination project for the new disk.
REST
Create a disk snapshot in the destination project by calling the
snapshots.insertmethod. For example, to take a snapshot of zonal Persistent Disk, make the following request:POST https://compute.googleapis.com/compute/v1/projects/DESTINATION_PROJECT_ID/global/snapshots { "name": SNAPSHOT_NAME "sourceDisk": "projects/SOURCE_PROJECT_ID/zones/SOURCE_ZONE/disks/SOURCE_DISK_NAME }Replace the following:
- DESTINATION_PROJECT_ID: The ID of the destination project for the new snapshot.
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_PROJECT_ID: The ID of the source disk project.
- SOURCE_ZONE: The zone of the source disk.
- SOURCE_DISK_NAME: The name of the source disk.
In the destination project, create a zonal or regional Persistent Disk or a Hyperdisk volume that's based on the snapshot by using the zonal
disks.insertmethod or the regionalregionDisks.insertmethod. For example, to create a zonal disk, make the following request:POST https://compute.googleapis.com/compute/v1/projects/DESTINATION_PROJECT_ID/zones/DESTINATION_ZONE/disks { "name": DISK_NAME "sourceSnapshot": SNAPSHOT_NAME }Replace the following:
- DESTINATION_PROJECT_ID: The ID of the destination project for the new disk.
- DISK_NAME: A name for the new disk.
- SNAPSHOT_NAME: The name of the snapshot.
To get a list of snapshots available to you, see Listing snapshots.
Share a snapshot across projects in different organizations
To share a snapshot from a project in one organization to a different project in another organization, use the following process:
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
In the source project, create a disk that's based on the snapshot by using the
gcloud compute disks createcommand:gcloud compute disks create DISK_NAME \ --source-snapshot SNAPSHOT_NAME \ --project SOURCE_PROJECT_ID \ --zone ZONEReplace the following:
- DISK_NAME: a name for the new disk.
- SNAPSHOT_NAME: The name of the snapshot.
- SOURCE_PROJECT_ID: The ID of the source project.
- ZONE: Zone in which new disk will be created.
This disk is a temporary disk that is created only to copy the snapshot across organizations. You can delete this disk after the snapshot copy completes.
In the destination project, create a snapshot by using the
gcloud compute snapshots createcommand. For example, to create a snapshot using the zonal disk created in step 1, use the following command:gcloud compute snapshots create SNAPSHOT_NAME \ --source-disk https://www.googleapis.com/compute/v1/projects/SOURCE_PROJECT_ID/zones/ZONE/disks/SOURCE_DISK_NAME \ --project DESTINATION_PROJECT_IDReplace the following:
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_PROJECT_ID: The ID of the project that contains the source disk.
- ZONE: The zone of the source disk.
- SOURCE_DISK_NAME: The name of the disk created in step 1.
DESTINATION_PROJECT_ID: The ID of the destination project for the new snapshot.
To get a list of snapshots available to you, see Listing snapshots.
Delete the temporary disk created in step 1 using
gcloud compute disks deletecommand.
REST
In the source project, create a zonal or regional disk that's based on the snapshot by using the zonal
disks.insertmethod. For example, to create a zonal disk, make the following request:POST https://compute.googleapis.com/compute/v1/projects/SOURCE_PROJECT_ID/zones/ZONE/disks { "name": DISK_NAME "sourceSnapshot": SNAPSHOT_NAME }Replace the following:
- SOURCE_PROJECT_ID: The ID of the source project for the new disk.
- ZONE: Zone for new disk.
- DISK_NAME: A name for the new disk.
- SNAPSHOT_NAME: The name of the snapshot.
This disk is a temporary disk that is created only to copy the snapshot across organizations.
Create a snapshot in the destination project by calling the
snapshots.insertmethod. For example, to take a snapshot of zonal Persistent Disk, make the following request:POST https://compute.googleapis.com/compute/v1/projects/DESTINATION_PROJECT_ID/global/snapshots { "name": SNAPSHOT_NAME "sourceDisk": "projects/SOURCE_PROJECT_ID/zones/SOURCE_ZONE/disks/SOURCE_DISK_NAME }Replace the following:
- DESTINATION_PROJECT_ID: The ID of the destination project for the new snapshot.
- SNAPSHOT_NAME: A name for the snapshot.
- SOURCE_PROJECT_ID: The ID of the project that contains the source disk.
- SOURCE_ZONE: The zone of the source disk.
- SOURCE_DISK_NAME: The name of the source disk.
Delete the temporary disk created in step 1 using
disks.deletemethod.
What's next
- Learn how to create a snapshot schedule for Persistent Disk volumes.
- Learn how to create a disk image from your snapshot.