Sub topic 10
Snapshots: incremental backups of volumes
A snapshot is a point-in-time copy of an EBS volume. AWS stores it in Amazon S3, in storage that AWS manages for you. You can't see it as an object in your own buckets. This chapter explains how snapshots store data, how to create and copy them, and what consistency they give you. It assumes you can create and mount a volume (covered earlier in this tutorial) and use the AWS CLI. Restoring and initializing volumes, encryption, and automated schedules each get their own chapter, so they only appear briefly here.
How snapshots store data
The first snapshot of a volume copies all the blocks that have data. Each later snapshot of the same volume stores only the blocks that changed since the previous snapshot. For unchanged blocks, it keeps references to data that earlier snapshots already hold.
Every snapshot is still a complete, independent restore point. You can restore any snapshot without first restoring the ones before it. This works because EBS tracks which snapshot owns each block, not a chain of "differences to apply in order."
This has a practical consequence when you delete snapshots. If you delete snapshot 1 above, EBS does not discard blocks A and C, because snapshots 2 and 3 still need them. It removes only the data that no remaining snapshot references. Deleting any snapshot in the middle of a series is therefore safe. You can still restore every other snapshot completely.
Because of this design, the cost of a series of snapshots depends on how much data changes, not on how many snapshots you keep. A volume with a low change rate is cheap to snapshot often.
Scope and properties
- A snapshot lives in a Region, not an Availability Zone. You can restore it into any AZ in that Region. This is the standard way to move data between AZs.
- A snapshot of an encrypted volume is encrypted with the same KMS key. A snapshot of an unencrypted volume is unencrypted unless you copy it with encryption turned on.
- The volume stays attached and usable while the snapshot is in progress. Reads and writes continue, and performance can drop slightly on some volume types while the snapshot runs.
- The snapshot captures data that had been written to the volume when you started it. Writes after that point are not included, even if the snapshot is still in the
pendingstate. - Snapshots are billed per GB-month for the data they store. The incremental design means the billed size of one snapshot is not its full volume size.
Creating a snapshot
The following commands create a snapshot, wait for it to finish, and restore it into a different AZ. The first command snapshots the volume. The second creates a volume from the snapshot in us-east-1b.
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description 'data-vol nightly' \
--tag-specifications 'ResourceType=snapshot,Tags=[{Key=Name,Value=data-vol-snap}]'
aws ec2 create-volume \
--snapshot-id snap-0123456789abcdef0 \
--availability-zone us-east-1b \
--volume-type gp3The call returns right away with the state pending. The snapshot is usable only when its state is completed. The describe-snapshots command shows state and progress, and the wait command blocks until it finishes:
aws ec2 describe-snapshots \
--snapshot-ids snap-0123456789abcdef0 \
--query 'Snapshots[0].[State,Progress,VolumeSize]'
aws ec2 wait snapshot-completed \
--snapshot-ids snap-0123456789abcdef0A snapshot can end in error. In that case, create a new one. A completed state means the data is durable. A pending snapshot is not yet a safe backup.
Crash consistency and application consistency
A snapshot records the blocks on the volume. It does not know what your application held in memory. By default the result is crash-consistent. It looks as if the instance lost power at that moment. File systems with journaling, such as XFS and ext4, recover from that state. Databases with write-ahead logs usually recover as well. Data that the application had not yet flushed to the volume is missing.
For a stronger guarantee, do the following before you start the snapshot:
- Flush the application's buffers or pause writes. Many databases have a command for this.
- Run
sudo xfs_freeze -f /datato freeze the file system and stop writes. - Call
create-snapshot, and wait until the call returns. - Run
sudo xfs_freeze -u /datato unfreeze.
You only need to freeze until the snapshot has started, not until it completes. The snapshot point is fixed when the API call is accepted, so the freeze lasts seconds. On Windows, Volume Shadow Copy Service plays a similar role, and AWS provides tooling for it. For databases, a native backup or a replica is often simpler than freezing.
Multi-volume snapshots
An application that spans several volumes, such as a database with separate data and log volumes, needs all snapshots taken at the same instant. If you snapshot them one after another, the data volume and the log volume may not match. Use create-snapshots with an instance as the source. It creates crash-consistent snapshots of the instance's volumes at the same point in time:
aws ec2 create-snapshots \
--instance-specification InstanceId=i-0123456789abcdef0,ExcludeBootVolume=true \
--description 'db volumes' \
--copy-tags-from-source volumeExcludeBootVolume=true skips the root volume, and --copy-tags-from-source volume copies each volume's tags to its snapshot. A snapshot of the root volume of a running instance is also crash-consistent, which is usually adequate for creating an AMI.
Copying, sharing and archiving
You can copy a snapshot to another Region, for disaster recovery or to launch the same data in a new Region. The command runs in the destination Region and names the source Region:
aws ec2 copy-snapshot \
--region us-west-2 \
--source-region us-east-1 \
--source-snapshot-id snap-0123456789abcdef0 \
--description 'DR copy' \
--encryptedA copy is a new snapshot. The first copy to a Region transfers all of the data, and later copies of snapshots from the same volume transfer incrementally. Cross-Region copies incur data transfer charges. A copy can also change encryption. This is how you turn an unencrypted snapshot into an encrypted one.
You can share a snapshot with specific AWS accounts by changing its permissions. Sharing an encrypted snapshot also requires sharing access to its KMS key, and you can't share one that uses the default AWS managed key.
Two features reduce cost and risk for long-lived backups:
- Snapshot Archive is a lower-cost storage tier for snapshots you rarely restore. Archiving stores a full copy of the snapshot, not the incremental blocks. It has a minimum retention of 90 days, and restoring from it takes hours. Archive snapshots that you'll keep for months and almost never use.
- Recycle Bin keeps deleted snapshots for a retention period you define, so an accidental delete can be undone. You enable it with retention rules.
Deleting snapshots and common errors
Delete a snapshot with aws ec2 delete-snapshot --snapshot-id snap-0123456789abcdef0. Expect these errors:
- Snapshot is in use by an AMI. You can't delete a snapshot that backs a registered AMI. Deregister the AMI first, then delete the snapshot.
- Snapshot size looks wrong. The storage billed is the unique blocks, not the volume size. Deleting one snapshot may free little space, because later snapshots still reference its blocks.
- Restored data is stale. The snapshot has only the data present when you started it. If you started it before an important write, that write is not in it.
- Copy fails for encrypted snapshots. The caller needs permission to use the KMS key in both source and destination.
Exercise
You take snapshot 1 of a 100 GiB volume holding 40 GiB of data. You change 5 GiB and take snapshot 2. Then you delete snapshot 1. Can you still restore snapshot 2 in full, and why?
Show answer
Yes. Snapshot 2 stores the 5 GiB of changed blocks and references the other blocks held by snapshot 1. When you delete snapshot 1, EBS keeps every block that snapshot 2 still references and removes only the data nobody references. Snapshot 2 remains a complete restore point.
Exercise
A database uses separate data and log volumes. Which command takes snapshots that match each other, and what consistency level do they have?
Show answer
Use aws ec2 create-snapshots with --instance-specification naming the instance. It snapshots the instance's volumes at the same point in time. The snapshots are crash-consistent, so for application consistency you must flush or freeze the application first.