How to Create and Manage LVM Snapshots in Linux

The Power of LVM Snapshots

When performing risky system upgrades or database migrations on a Linux server, taking a traditional file-based backup can take hours. If your storage uses the Logical Volume Manager (LVM), you can create an LVM Snapshot in less than a second. A snapshot captures the exact state of a logical volume at a specific point in time. It uses a “copy-on-write” mechanism, meaning it only consumes disk space when changes are made to the original volume, making it an incredibly efficient rollback solution.

Step 1: Check Available Free Space

LVM snapshots must be created within the same Volume Group (VG) as the original logical volume, and that Volume Group must have unallocated free space. Check your volume group capacity using the vgs command:

sudo vgs

Look at the VFree column. You need enough free space to accommodate all the data changes (writes) that will occur while the snapshot exists. Typically, 10% to 20% of the original volume’s size is sufficient for a short-term snapshot.

Step 2: Create the Snapshot

Let’s assume your original logical volume is located at /dev/vg_data/lv_sql, and you want to allocate 5GB for the snapshot. Run the following command:

sudo lvcreate --size 5G --snapshot --name snap_sql_backup /dev/vg_data/lv_sql

The snapshot is created instantly. The original volume (lv_sql) remains online and fully accessible to your applications without any downtime.

Step 3: Monitor Snapshot Usage

As time passes and changes are written to the original volume, the snapshot’s 5GB allocation will begin to fill up. Warning: If a snapshot reaches 100% capacity, it becomes corrupted and useless. You must monitor its size using the lvs command:

sudo lvs

Look at the Data% column for your snapshot. If it gets dangerously close to 100%, you can extend it on the fly (e.g., sudo lvextend -L +2G /dev/vg_data/snap_sql_backup).

Step 4: Roll Back (Merge) the Snapshot

If your system upgrade fails and you need to revert the database exactly to the moment you took the snapshot, you will perform a merge operation.

First, unmount the original volume (you cannot merge a mounted filesystem):

sudo umount /dev/vg_data/lv_sql

Next, initiate the merge:

sudo lvconvert --merge /dev/vg_data/snap_sql_backup

The merge will overwrite the original volume with the snapshot data. Once complete, the snapshot volume is automatically deleted. You can then remount the original volume, and your data is fully restored.

Step 5: Delete the Snapshot Manually

If your system upgrade was successful and you no longer need the snapshot, you must manually delete it to stop it from consuming storage space and system I/O resources during normal operations.

sudo lvremove /dev/vg_data/snap_sql_backup

Confirm the deletion by typing y. Your LVM environment is now clean.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.