Sub topic 10
Snapshots: incremental backups of volumes
This chapter assumes you can create and attach an EBS volume, use the AWS CLI, and know what an Availability Zone is. It covers what a snapshot is, how incremental storage works, and the errors people commonly hit.
What a snapshot is
A snapshot is a point-in-time copy of an EBS volume. AWS stores it in Amazon S3, in storage that AWS manages. You do not see it as an S3 object in your own buckets. A snapshot is regional, so it is not tied to the Availability Zone of its source volume. That makes snapshots the way to move data between Availability Zones or Regions.
How incremental backups work
The first snapshot of a volume copies all blocks that contain data. Later snapshots of the same volume save only the blocks that changed since the previous snapshot. Unchanged blocks are shared with earlier snapshots by reference.
Each snapshot still behaves like a full backup when you restore it. If you delete an older snapshot, AWS keeps any blocks that newer snapshots still need. You can therefore delete any snapshot in a chain without breaking the others.
Creating a snapshot
The following commands take a snapshot and then create a new volume from it in a different Availability Zone.
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 snapshot starts in the pending state and moves to completed. You can use the volume while the snapshot is pending. The snapshot captures the data as it was when you issued the command. Check progress with aws ec2 describe-snapshots --snapshot-ids snap-0123456789abcdef0.
Consistency
A snapshot captures only data that has reached the volume. Data cached in memory by the operating system or an application is not included. For a consistent backup, choose one of these approaches:
- Stop the application, flush writes, and take the snapshot.
- Freeze the file system with
fsfreeze, take the snapshot, then unfreeze it. - Unmount the volume, or stop the instance, before the snapshot.
- Use a multi-volume snapshot of all volumes on an instance when an application spans several volumes.
Common errors
- Assuming a snapshot is a full copy that costs full price. You pay for the unique blocks stored across the chain, not the volume size for each snapshot.
- Snapshotting a busy database with no quiescing. The restore may need crash recovery, or the data may be inconsistent across files.
- Creating the restored volume in the wrong place. The new volume must be in the Availability Zone of the instance that will use it.
- Expecting a restored volume to be fast immediately. Blocks load lazily from S3 on first access. The chapter on restoring and initializing volumes covers this.
- Deleting snapshots and expecting all storage cost to vanish. Blocks that newer snapshots still use stay stored.
- Forgetting that snapshots of encrypted volumes are encrypted. Volumes created from them are encrypted too.
Sharing and copying
You can copy a snapshot to another Region with aws ec2 copy-snapshot, run from the destination Region. You can also share a snapshot with other AWS accounts. Encrypted snapshots can be shared only if you also share the KMS key they use, and snapshots encrypted with the default AWS managed key cannot be shared.
Exercise
A volume has 100 GiB of data. You take a first snapshot, change 5 GiB, and take a second snapshot. Roughly how much new data does the second snapshot store, and what happens if you delete the first snapshot?
Show answer
The second snapshot stores about 5 GiB, the changed blocks. If you delete the first snapshot, AWS keeps the blocks that the second snapshot still references, so the second snapshot can still restore the full 100 GiB volume.
Exercise
You need a copy of an instance's data volume in us-east-1b, but the volume lives in us-east-1a. How do you do it?
Show answer
Create a snapshot of the volume, then create a new volume from that snapshot with --availability-zone us-east-1b.