Creating Persistent Disk Snapshots

Use snapshots to back up data from your persistent disks. Snapshots are different from images, which are used primarily to create instances or configure instance templates. Snapshots are useful for periodic backup of your persistent disks. You can create snapshots from persistent disks even while they are attached to running instances.

Snapshots are differential, so you can create regular snapshots on a persistent disk faster and at a much lower cost than if you regularly created a full image of the disk. Differential snapshots work in the following manner:

  • The first successful snapshot of a persistent disk is a full snapshot that contains all the data on the persistent disk.
  • The second snapshot only contains any new data or modified data since the first snapshot. Data that hasn't changed since snapshot 1 isn't included. Instead, snapshot 2 contains references to snapshot 1 for any unchanged data.
  • Snapshot 3 contains any new or changed data since snapshot 2 but won't contain any unchanged data from snapshot 1 or 2. Instead, snapshot 3 contains references to blocks in snapshot 1 and snapshot 2 for any unchanged data.

This repeats for all subsequent snapshots of the persistent disk.

The diagram below attempts to illustrate this process:

Diagram describing how to create a snapshot

Compute Engine stores multiple copies of each snapshot redundantly across multiple locations with automatic checksums to ensure the integrity of your data. You cannot share snapshots across projects.

To see a list of snapshots available to a project, run the following command:

gcloud compute snapshots list

To list information about a particular snapshot, use the gcloud compute snapshots describe command:

gcloud compute snapshots describe example-snapshot

Before you begin

Create a snapshot

Before you create a persistent disk snapshot, make sure that you are taking a snapshot that is consistent with the desired state of your persistent disk. If you take a snapshot of your persistent disk in a state where application data is cached without actually being written to disk, it might force a disk check or cause data loss. To help with this, make sure that your disk buffers are flushed before you take your snapshot. For example, if your operating system is writing data to the persistent disk, it is possible that your disk buffers are not yet cleared. Additionally, taking snapshots from persistent disks with log-structured file systems like ext4 is more reliable.

For instructions on how to create snapshots from disks that are encrypted with your own encryption keys, see Creating a snapshot from an encrypted disk.

For instructions on how to create a Windows persistent disk snapshot, see Creating a Windows persistent disk snapshot.

Prepare the persistent disk for a snapshot

You can create a snapshot of a persistent disk even while your applications write data to the disk. However, it is best to sync your file system to flush the disk buffers before you create a snapshot. To sync your file system:

  1. SSH into your instance.
  2. Run an application flush to disk if necessary. For example, MySQL has a FLUSH statement. Other applications might have similar processes.
  3. Stop your applications from writing to your persistent disk.
  4. Run sudo sync.
  5. Run sudo fsfreeze -f example-disk_location.
  6. Create your snapshot.
  7. Verify that the snapshot is in an UPLOADING or READY state.
  8. Run sudo fsfreeze -u example-disk_location.

If you skip this step, only data that is successfully flushed to disk by the application will be included in the snapshot. The application experiences this scenario as if it was a sudden power outage.

For a more detailed discussion of how to prepare your machine for snapshotting, see our blog post on snapshotting.

Unmount the filesystem

Alternatively, you can unmount the filesystem before you take a snapshot. This is the safest and most reliable way to that ensure your disk buffers are cleared, but it is not as convenient as simply flushing the disk buffers.

  1. SSH into your instance.
  2. Stop any applications that are reading or writing data to the persistent disk.
  3. Run sudo umount example-disk_location.

Create your snapshot

Console


  1. Go to the Create a new snapshot page.
  2. Enter a snapshot name and select an existing disk from which you want to make a snapshot.
  3. Click Create to create the snapshot.

gcloud


To create your snapshot, use the gcloud compute disks snapshot command:

gcloud compute disks snapshot [DISK_NAME]

gcloud 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


Make a POST request to the createSnapshot method:

POST https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/disks/[DISK_NAME]/createSnapshot

where:

  • [PROJECT_ID] is your project ID.
  • [ZONE] is the zone where your instance and new disk are located.
  • [DISK_NAME] is the name of the new disk.

Restoring a snapshot to a larger size

You can restore a non-root persistent disk snapshot to a larger size than the original snapshot, but you must run some additional commands from within the instance for the additional space to be recognized by the instance.

Depending on your operating system and filesystem type, you might need to use a different filesystem resizing tool. Please refer to your operating system documentation for more information.

First, create a new persistent disk that is larger than your snapshot:

  1. Create a new persistent disk from your non-root snapshot, using the --size flag to specify a disk size that is larger than the snapshot size:

    gcloud compute disks create example-disk \
      --source-snapshot=example-snapshot --size=600GB
    
  2. Attach your persistent disk to an instance:

    gcloud compute instances attach-disk example-instance --disk=example-disk
    

After you create the new persistent disk and attach it to your instance, resize the file system on that persistent disk.

Linux instances


On Linux instances, expand the file system on the persistent disk with the following process:

  1. Connect to the instance using SSH. For this example, go to the VM instances page and click the SSH button next the instance that has the resized disk.

  2. Extend the file system on the persistent disk. If your disk has a partition table or uses a volume management system, you must resize your partitions first. If you originally formatted the persistent disk with a single ext4 file system and no partition table, you can simply run the resize2fs command to resize the file system across the additional 50 GB of disk space.

    $ sudo resize2fs /dev/disk/by-id/google-example-disk
    

  3. Optionally, use the df -h command to verify that the file system is expanded.

    $ df -h /dev/disk/by-id/google-example-disk
    
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sdb        246G   60M  234G   1% /mnt/example-disk
    
    Note: The df command displays disk sizes in gigabyte GB units, which is a different than the gibibyte (GiB) unit that Compute Engine uses.

Windows instances


Use the Windows Disk Management tool to resize partitions on a Windows instance.

  1. Connect to the instance through RDP. For this example, go to the VM instances page and click the RDP button next the instance that has the resized disk.

  2. Right click the Windows Start button and select Disk Management to open the Disk Management tool.

    Selecting the Windows Disk Manager tool from the right-click menu on the Windows Start button.

  3. Refresh the Disk Management tool so that it recognizes the additional space on your persistent disk. At the top of the Disk Management window, click Action and select Refresh.

    Clicking the Action menu and selecting Refresh to update the persistent disk information in the Disk Management tool.

  4. On the disk that you resized, right-click on the formatted partition and select Extend Volume.

    Right-clicking the formatted portion of the disk and selecting the Extend Volume option.

  5. Follow the instructions in the Extend Volume Wizard to extend your existing partition across the resized disk space. If the existing partition is formatted in NTFS, the maximum partition size is limited by its cluster size settings.

  6. After you complete the wizard and the volume finishes formatting, check the new disk to ensure it is Online with a Healthy status.

    Viewing the list of disks that are recognized by Windows, verify that the instance is Online with a Healthy status.

You can now use the extra persistent disk space to store data.

Restoring a snapshot to a different type of disk

You can restore a snapshot to a different type of persistent disk by running the following command:

gcloud compute disks create example-disk --source-snapshot=example-snapshot --type={pd-standard|pd-ssd}

Deleting a snapshot

Compute Engine uses differential snapshots so that each snapshot contains only the data that has changed since the previous snapshot. For unchanged data, snapshots use references to the data in previous snapshots. Persistent disk snapshots charge only for the total size of the snapshot.

When you delete a snapshot, Compute Engine immediately marks the snapshot as DELETED in the system. If the snapshot has no dependent snapshots, it is deleted outright. However, if the snapshot does have dependent snapshots:

  1. Any data that is required for restoring other snapshots is moved into the next snapshot, increasing its size.
  2. Any data that is not required for restoring other snapshots is deleted. This lowers the total size of all your snapshots.
  3. The next snapshot no longer references the snapshot marked for deletion, instead referencing the snapshot before it.

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. As mentioned in the first bullet above, if any data on a snapshot that is marked for deletion is needed for restoring subsequent snapshots, that data is moved into the next corresponding snapshot. To definitively delete data from your snapshots, you should delete all snapshots.

The diagram below illustrates the process described above:

Diagram describing the
  process for deleting a snapshot

To delete a snapshot, use the gcloud compute snapshots delete command:

gcloud compute snapshots delete example-snapshot

Send feedback about...

Compute Engine Documentation