
On the EC2 instances described in my earlier post about hosting my Web site on Amazon EC2 and S3, changes on the instance’s local disk were lost if the instance was terminated or failed. Persistent data needs storage that survives the instance.
Therefore, it is a good idea to store your home directory, Web document root and databases on an Amazon EBS volume, where the data does persist like in a normal networked hard drive. Another benefit of using an Amazon EBS volume as a data disk is that it separates your operating system image from your data. This way, when you upgrade from a server instance with less computing power to one with more computing power, you can reattach your data drive to it for use there.
You can create an EBS volume and attach it to your EC2 server instance using a procedure similar to the following.
First, create an EBS volume.
The 2008 EBS announcement describes support in the Elasticfox Firefox extension. In that workflow, you can:
- create an EBS volume in the same Availability Zone as the instance
- attach it to your EC2 instance
- choose an attachment device name; this example uses
/dev/sdh, whose actual name inside the operating system must be checked
Copy the data before changing its path
I use the naming convention rj-YYMMDDNN for my disk drives: YYMMDD is the date a drive enters service, and NN is its serial number for that day. This example uses /mnt/rj-09031301.
Before changing an active /home, arrange maintenance access that does not depend on that directory. Stop logins and every process that can write there; stopping Apache alone is insufficient. Databases need their own consistent backup and migration procedure. This sequence assumes /home is an ordinary directory, not a mount point or symbolic link. Identify any nested mounts or special files and plan how to preserve the ownership, permissions and links the applications need.
Confirm that the attached volume is the intended new, empty device and that its operating-system name really is /dev/sdh. Formatting destroys an existing filesystem, so skip it for a volume that already contains data. For the new empty volume in this ext3 example:
sudo mkfs.ext3 /dev/sdh &&
sudo mkdir /mnt/rj-09031301 &&
sudo mount -t ext3 /dev/sdh /mnt/rj-09031301
Stop if a command fails. Inspect the mounted filesystems and confirm that /mnt/rj-09031301 is on that EBS volume before copying. The following GNU cp command copies the entire home directory, including hidden entries, and attempts to retain ownership, permissions and links. The destination home must not already exist, even as a dangling symbolic link:
sudo test ! -e /mnt/rj-09031301/home &&
sudo test ! -L /mnt/rj-09031301/home &&
sudo cp -a /home /mnt/rj-09031301/
Keep writers stopped while checking the copy independently. Compare complete relative-path inventories, including hidden entries, file types and link targets; compare every regular file’s bytes or cryptographic hash. Check ownership, permissions and hard-link relationships separately, along with any access-control metadata the applications require. Verify /mnt/rj-09031301/home itself, the exact destination the new link will use. Resolve every difference before switching.
Once those checks pass, retain the original at an unused backup path and create the new link:
sudo test ! -e /home.backup &&
sudo test ! -L /home.backup &&
sudo mv /home /home.backup &&
sudo ln -s /mnt/rj-09031301/home /home
Before reopening writes, verify the link target and application access, and configure and test mounting the same volume at the same path after a reboot. If the switch fails, keep writers stopped, remove only the new symbolic link after confirming its identity, and rename /home.backup back to /home. After writes resume, rollback also needs to preserve any newer data. Keep the original copy until the migrated data and a restoration have been validated.
Practice checking a copy — later addition
The directory-copy workbook uses three fictional files to compare a complete copy with missing, changed or misdirected data. It leaves the source intact and prints a proposed switch; it never changes a system path. Check the destination before accepting that preview. The exercise teaches file-byte conservation; the ownership, writer coordination, mount and recovery checks above still belong to the real migration.
Shared-storage limits in 2009
An EBS volume can currently attach to only one instance at a time. I hope Amazon adds concurrent attachment so applications can share storage without maintaining a separate synchronized copy for every server. Shared block access would still require a filesystem or application designed to coordinate concurrent writers; it would not by itself provide the file-sharing behavior of a NAS. EBS snapshots can create initial volume copies, but those copies do not stay synchronized automatically.
Later development — February 14, 2020
Amazon introduced Multi-Attach for Provisioned IOPS (io1) EBS volumes, allowing a volume to attach to as many as 16 compatible Linux Nitro instances in one Availability Zone. Applications must coordinate access or use a cluster-aware filesystem. Concurrent attachment does not make an ordinary ext3 filesystem safe for several independent writers.
Related reading: Running MySQL on Amazon EC2 with Elastic Block Store, the AWS article referenced in the original post.