Sub topic 13
Backing up with EBS snapshots
This chapter assumes you know what an EBS volume is, how it attaches to an instance in one Availability Zone, and how to run basic AWS CLI commands. It covers how snapshots work, how to create and restore them, and the mistakes that most often cause trouble.
What a snapshot is
An EBS snapshot is a point-in-time copy of an EBS volume. AWS stores it in Amazon S3 behind the scenes, in storage that you do not see in your own buckets. Because of this, a snapshot is durable across the Availability Zones of a Region. That differs from the volume itself, which lives in a single Availability Zone.
Snapshots are incremental. The first snapshot of a volume copies all written blocks. Later snapshots of the same volume store only the blocks that changed since the previous snapshot. Each snapshot still behaves like a full backup. If you delete an earlier snapshot, AWS keeps any blocks that later snapshots still need, so the remaining snapshots stay restorable.
Creating a snapshot
You create a snapshot with one command. The snapshot starts immediately and is captured as of the moment you issue the request.
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description "tutorial-web data volume backup"The command returns right away with a state of pending. The data is copied in the background while the volume stays in use. The snapshot becomes usable for restore when its state is completed. You can check progress and wait for completion like this:
aws ec2 describe-snapshots \
--snapshot-ids snap-0123456789abcdef0 \
--query 'Snapshots[].[SnapshotId,State,Progress]' \
--output table
aws ec2 wait snapshot-completed --snapshot-ids snap-0123456789abcdef0Consistency
A snapshot captures only the blocks that have reached the volume. Data that an application still holds in memory, or that the operating system has not yet flushed to disk, is not included. The snapshot is therefore crash-consistent, as if the server lost power at that moment. Many file systems and databases recover from that state, but some do not recover cleanly.
For an application-consistent backup, do one of the following before you snapshot:
- Stop the application, or put the database into a backup mode.
- Flush file system buffers and freeze writes, for example with
syncandfsfreeze. - Unmount the volume, or stop the instance, for a data volume you can take offline.
You only need the quiet period while the snapshot is being initiated. Once it is in the pending state, you can resume normal writes.
Tagging snapshots
Snapshots do not inherit tags from the volume by default. Without tags, a growing set of snapshots is hard to identify and clean up. Add tags at creation time:
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description "tutorial-web data volume backup" \
--tag-specifications 'ResourceType=snapshot,Tags=[{Key=Name,Value=tutorial-web-data},{Key=Retention,Value=30d}]'Restoring from a snapshot
You never restore "into" a snapshot. You create a new volume from it. The new volume can go in any Availability Zone in the same Region, which is also how you move data between zones.
aws ec2 create-volume \
--snapshot-id snap-0123456789abcdef0 \
--availability-zone us-east-1b \
--volume-type gp3
aws ec2 attach-volume \
--volume-id vol-0fedcba9876543210 \
--instance-id i-0123456789abcdef0 \
--device /dev/sdfThe new volume is available at once, but blocks load lazily from S3 the first time you read them. Early reads can be slower than steady-state performance. For a data volume where this matters, read all blocks once after the restore (for example with fio or dd) to initialize it. Fast snapshot restore is a paid feature that avoids this delay in selected Availability Zones.
After attaching, mount the volume on the instance. Do not run mkfs on a volume restored from a snapshot, because that would erase the data you just restored. Check for a file system first:
lsblk -f
sudo mkdir -p /data
sudo mount /dev/nvme1n1 /dataOn Nitro-based instances, the device appears as an NVMe name such as /dev/nvme1n1, not the name you passed to attach-volume. Confirm with lsblk before you mount.
Snapshots, AMIs, and the root volume
An AMI backed by EBS is built from a snapshot of the root volume plus launch metadata. When you create an image from an instance, AWS makes snapshots of its attached volumes. You cannot delete a snapshot that a registered AMI uses. Deregister the AMI first, then delete the snapshot.
Copying and sharing
Snapshots are Regional. To keep a backup in another Region for disaster recovery, copy it:
aws ec2 copy-snapshot \
--source-region us-east-1 \
--source-snapshot-id snap-0123456789abcdef0 \
--region eu-west-1 \
--description "DR copy of tutorial-web data"You run the command in the destination Region. A copy is a separate snapshot with its own ID and its own storage charge, plus data transfer charges between Regions. You can also share a snapshot with specific AWS accounts. Never make a snapshot public unless you intend everyone to read its contents.
Encryption
Snapshots of an encrypted volume are encrypted, and volumes created from them are encrypted too. A snapshot of an unencrypted volume is unencrypted. When you copy a snapshot you can enable encryption or choose a different KMS key. This is the standard way to convert an unencrypted volume to an encrypted one: snapshot it, copy with encryption on, and create a new volume from the copy. Sharing an encrypted snapshot also requires sharing access to the KMS key.
Automating backups
Manual snapshots are easy to forget. Two managed options remove the schedule from your hands:
- Amazon Data Lifecycle Manager creates and deletes snapshots on a schedule for volumes or instances that match tags.
- AWS Backup applies backup plans across many services, including EBS, with retention rules and central reporting.
Either approach also handles expiry, which controls snapshot cost over time.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Restored volume will not attach | Volume and instance are in different Availability Zones | Create the volume in the instance's zone |
| `mkfs` wiped restored data | Formatted a volume that already had a file system | Check with `lsblk -f` before formatting |
| Application fails to start from restore | Snapshot was only crash-consistent | Quiesce the application before snapshotting |
| Cannot delete a snapshot | A registered AMI references it | Deregister the AMI, then delete |
| Unexpected storage bill | Old snapshots never expired | Tag them and use Data Lifecycle Manager |
| Slow first reads after restore | Blocks load lazily from S3 | Pre-read the volume or use fast snapshot restore |
Deleting snapshots
Delete a snapshot with aws ec2 delete-snapshot --snapshot-id snap-0123456789abcdef0. Because of incremental storage, deleting one snapshot may free little space if later snapshots depend on its blocks. Deleting a volume does not delete its snapshots, so include snapshots in your cleanup.
Exercise
An application writes to a data volume in us-east-1a. You take a snapshot, and later need the data on an instance in us-east-1b. Describe the steps, and name one reason the application might fail to start from the restored data even though the snapshot completed.
Show answer
Create a new volume from the snapshot with --availability-zone us-east-1b, attach it to the instance in that zone, and mount it without running mkfs. The application might fail because the snapshot was only crash-consistent: data the application or operating system had not yet flushed to disk was not captured. Quiescing the application, or flushing and freezing the file system before the snapshot starts, avoids this.
Exercise
You delete the oldest of three snapshots of the same volume. Can you still restore the newest snapshot, and why?
Show answer
Yes. Snapshots are incremental, but AWS keeps any blocks from the deleted snapshot that later snapshots still reference, so every remaining snapshot stays fully restorable.