Master LVM Snapshots: Create, Restore, and Merge Without Downtime
This guide explains how LVM snapshots work, how to create them using copy‑on‑write, restore individual files or entire volumes, merge snapshots back into the original LV, and leverage snapshots for testing environments, all without stopping services.
LVM Snapshot Overview
LVM provides snapshot functionality for logical volumes, allowing you to back up a filesystem without stopping services. Snapshots use copy‑on‑write (COW) technology, which is more efficient than traditional backup methods. When a snapshot is created, only hard links to the original inodes are made; unchanged data remains shared, and only modified blocks are copied to the snapshot area.
How Snapshots Work
A snapshot is essentially a special logical volume that shares data blocks with the original LV until those blocks are modified. The snapshot reserves space in the volume group (VG) to store changed blocks. Important points:
The VG must have free space reserved for the snapshot.
The snapshot and the original LV must reside in the same VG.
Creating an LVM Snapshot
First, check the current volume sizes. In the example, the data LV nicklv00 is 15 GB in VG nickvg, which has 55 GB free.
sudo lvcreate -L 15G --snapshot --name nicksnap00 nickvg/nicklv00This command creates a COW snapshot named nicksnap00. The snapshot initially contains no data of its own but shares all blocks with the source LV.
Restoring Individual Files
Mount the snapshot LV, locate the old version of the file, and copy it over the modified file:
sudo mount /dev/mapper/nickvg-nicksnap00 /mnt/snap
cp /mnt/snap/home/doc/hello.txt /home/doc/hello.txtRestoring an Entire Volume
When many files have changed, export the snapshot contents to a separate volume, then restore:
mkdir /home/nick/backup
tar -czf /home/nick/backup/lvm.tar.gz -C /mnt/snap .After unmounting the original LV and recreating its filesystem, extract the backup:
sudo umount /home/doc
sudo mkfs.ext4 /dev/nickvg/nicklv00
sudo mount /dev/mapper/nickvg-nicklv00 /home/doc
sudo tar -xf /home/nick/backup/lvm.tar.gz -C /home/docMerging a Snapshot
Instead of the manual restore, you can merge the snapshot back into the original LV using lvconvert --merge:
sudo umount /home/doc
sudo lvconvert --merge nickvg/nicksnap00The merge automatically removes the snapshot LV after completion.
Using Snapshots for Test Environments
Snapshots can quickly provide isolated test environments: create a snapshot, mount it as a test LV, perform experiments, then unmount and delete the snapshot. Repeat as needed.
Summary
LVM snapshots are an advanced yet practical feature for backup, restoration, and testing, enabling efficient copy‑on‑write backups without service interruption.
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.
