In Attach a Persistent EBS Volume to an Instance we discussed the reliability of EBS volumes, but no matter how reliable they may be, you still want to make sure you have a good backup strategy. That’s where snapshots come in. A snapshot allows you to make a copy of your EBS volume at an instant in time and save that data to S3, with the whole 11 nines of durability that it provides.
Example 2-9. Snapshot an EBS Volume
>>> import boto >>> ec2 = boto.connect_ec2() >>> reservations = ec2.get_all_instances(filters={'paws' : None}) >>> instance = reservations[0].instances[0] >>> volumes = ec2.get_all_volumes(filters={'attachment.instance-id' : instance.id}) >>> volumes [Volume:vol-990e5af3, Volume:vol-bdf7a2d7] >>> for v in volumes: ....: print v.attach_data.device ....: /dev/sdh /dev/sda1 >>> snaps = [v.create_snapshot() for v in volumes] >>> snaps [Snapshot:snap-42a60b21, Snapshot:snap-40a60b23] >>>
Find the instance whose volumes we want to snapshot. This
assumes it is the instance we started in an earlier recipe and is
tagged with the label paws
.
Even though we have attached only one EBS volume to this instance, our query returns two volumes. That’s because this is an EBS-backed AMI and one of those volumes represents the root volume of the instance.
We can loop through each of the volumes to print out the device name associated with each to determine which is the root volume.
Use a list comprehension in Python to create a list of snapshots, one for each volume.
The snapshot command makes a copy of the state of the volume at
an instant in time. It’s up to you to make sure that it’s the
right instant in time. For example, if you have a
database that is writing data to the volume, you may need to
temporarily halt the database so you get consistent information on the
volume at the time you perform the snapshot. Some people format their
volumes with xfs
file system and then freeze the file
system before running the snapshot and unfreeze immediately after. The
details of all of this are very application-specific, but it’s
important to validate the integrity of your backups by restoring some
snapshots (as shown in Restore a Volume from a Snapshot)
and testing them.