Seamless Migration of AWS EC2 EBS Volumes for Storage Expansion
Identify Existing Storage Configuration
Begin by inspecting the currant block devices to distinguish between the existing volume and the newly attached EBS drive.
lsblk
Assume the current data volume is identified as nvme0n1 and is mounted at /srv/project_data. The new volume appears as nvme1n1 and is currently unmounted.
Prepare the New EBS Volume
Initialize the new disk by creating a partition table and defining a partition.
sudo fdisk /dev/nvme1n1
Within the interactive prompt, create a new GPT label and a primary partition accepting default values for sector start and size:
Command (m for help): g
Command (m for help): n
Partition number (1-128, default 1): [Enter]
First sector (2048-..., default 2048): [Enter]
Last sector, +sectors or +size{K,M,G,T,P} (2048-..., default ...): [Enter]
Command (m for help): w
Once the partition table is written, format the new partition with the ext4 filesystem.
sudo mkfs -t ext4 /dev/nvme1n1p1
Data Synchronization Strategy
Create a temporary mount point to access the new volume and attach it.
sudo mkdir -p /mnt/temp_migration
sudo mount /dev/nvme1n1p1 /mnt/temp_migration
Use rsync to copy data from the old volume to the new one. This tool supports resumption if the process is interrupted. It is advisable to run this within a persistent session like tmux or screen to prevent termination upon disconnect.
sudo rsync -av --progress /srv/project_data/ /mnt/temp_migration/
Exclude specific cache or log directories if necessary by adding the --exclude flag.
Swapping Mount Points
Before swapping, ensure no processes are actively writing to the old mount point.
sudo lsof +f -- /srv/project_data
Terminate any active processes holding locks on the directory. Using fuser can simplify killing processes accessing the mount.
sudo fuser -km /srv/project_data
Unmount both the original and temporary directories.
sudo umount /srv/project_data
sudo umount /mnt/temp_migration
Mount the new partition to the original path. This ensures application configurations referencing /srv/project_data remain valid without modification.
sudo mount /dev/nvme1n1p1 /srv/project_data
Verify the application starts correctly using the new storage backend.
Configure Persistent Mounting
To ensure the new volume mounts automatically after a reboot, retrieve the UUID of the new partition.
sudo blkid /dev/nvme1n1p1
Edit the filesystem table configuration.
sudo vim /etc/fstab
Add an entry using the retrieved UUID, specifying the mount point and filesystem type. Include nofail to prevent boot issues if the volume is detached.
UUID=82b5c5d0-1234-5678-90ab-cdef12345678 /srv/project_data ext4 defaults,nofail 0 2