Master LVM Snapshots: Create, Restore, and Merge Efficiently
This guide explains how LVM snapshots work, how to create them with copy‑on‑write, restore individual files or entire volumes, and merge snapshots back into the original logical volume, providing a practical backup solution without service interruption.
LVM Snapshot Overview
LVM provides snapshot functionality that allows you to back up a filesystem using copy‑on‑write (COW) technology, which is more efficient than traditional backup methods because it does not require stopping services.
When a snapshot is created, LVM only creates hard links to the original inodes; unchanged data remains shared, while modified blocks are copied to the snapshot area before being overwritten.
Creating an LVM Snapshot
First, check the current volume group status to ensure enough free space. In the example, the volume group nickvg has 55 GB free, allowing a snapshot of the logical volume nicklv00 (15 GB).
sudo lvcreate -L 15G --snapshot --name nicksnap00 nickvg/nicklv00The snapshot appears as a special logical volume; mounting it shows the same data as the original at the moment of creation.
Restoring Individual Files
Mount the snapshot, locate the old version of the file, and copy it back to the original location using cp.
sudo mount /dev/mapper/nickvg-nicksnap00 /home/nick/bakAfter verifying the file content, overwrite the modified file in the source volume.
Restoring an Entire Volume
Mount the snapshot, export its contents to a temporary location (e.g., a tar archive), then format the original volume and extract the backup.
sudo umount /home/nick/bak</code>
<code>sudo lvremove nickvg/nicksnap00</code>
<code>sudo umount /home/doc</code>
<code>sudo mkfs.ext4 /dev/nickvg/nicklv00</code>
<code>sudo mount /dev/mapper/nickvg-nicklv00 /home/doc</code>
<code>sudo tar -xf /home/nick/backup/lvm.tar.gz -C /home/docMerging a Snapshot
Instead of the manual restore steps, you can merge the snapshot back into the original LV using lvconvert --merge. After unmounting both the source and snapshot volumes, run: sudo lvconvert --merge nickvg/nicksnap00 The merge automatically deletes the snapshot LV. Remount the original volume and verify that the data matches the snapshot point.
sudo mount /dev/mapper/nickvg-nicklv00 /home/doc
cat /home/doc/hello.txtUsing Snapshots for Test Environments
Snapshots can be repeatedly created and mounted to provide isolated test environments; after testing, simply unmount and delete the snapshot, then create a new one as needed.
Conclusion
LVM snapshots are an advanced yet practical feature for Linux storage management, offering efficient backup, selective restoration, and easy testing workflows.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
