Creating a snapshot of a logical volume, is an easy way to create a point-in-time backup of a file system, while still allowing changes to occur to the file system. Basically, by creating a snapshot, you will get a frozen (snapshot) file system that can be backed up without having to worry about any changes to the file system.
Many applications these days allow for options to "freeze" and "thaw" the application (as in, telling the application to not make any changes to the file system while frozen, and also telling it to continue normal operations when thawed). This functionality of an application can be really useful for creating snapshot backups. One can freeze the application, create a snapshot file system (literally in just seconds), and thaw the application again, allowing the application to continue. Then, the snapshot can be backed up, and once the backup has been completed, the snapshot can be removed.
Let's give this a try.
In the following process, we'll create a file system /original, using a logical volume called originallv, in volume group "extern". We'll keep it relatively small (just 1 Gigabyte - or 1G), as it is just a test:
Next, we'll create a file system of type XFS on it, and we'll mount it.# lvcreate -L 1G -n originallv extern Logical volume "originallv" created.
At this point, we have a file system /original available, and we can start creating a snapshot of it. For the purpose of testing, first, create a couple of files in the /original file system:# mkfs.xfs /dev/mapper/extern-originallv # mkdir /original # mount /dev/mapper/extern-originallv /original # df -h | grep original /dev/mapper/extern-originallv 1014M 33M 982M 4% /original
Creating a snapshot of a logical volume is done using the "-s" option of lvcreate:# touch /original/file1 /original/file2 /original/file3 # ls /original file1 file2 file3
In the command example above, a size of 1 GB is specified (-L 1G). The snapshot logical volume doesn't have to be the same size as the original logical volume. The snapshot logical volume only needs to hold any changes to the original logical volume while the snapshot logical volume exists. So, if there are very little changes to the original logical volume, the snapshot logical volume can be quite small. It's not uncommon for the snapshot logical volume to be just 10% of the size of the original logical volume. If there are a lot of changes to the original logical volume, while the snapshot logical volume exists, you may need to specify a larger logical volume size. Please note that large databases, in which lots of changes are being made, are generally not good candidates for snapshot-style backups. You'll probably have to test in your environment if it will work for your application, and to determine what a good size will be of the snapshot logical volume.# lvcreate -s -L 1G -n originalsnapshotlv /dev/mapper/extern-originallv
The name of the snapshot logical volume in the command example above is set to originalsnapshotlv, using the -n option. And "/dev/mapper/extern-originallv" is specified to indicate what the device name is of the original logical volume.
We can now mount the snapshot:
And at this point, we can see the same files in the /snapshot folder, as in the /original folder:# mkdir /snapshot # mount -o nouuid /dev/mapper/extern-originalsnapshotlv /snapshot # df -h | grep snapshot /dev/mapper/extern-originalsnapshotlv 1014M 33M 982M 4% /snapshot
To prove that the /snapshot file system remains untouched, even when the /original file system is being changed, let's create a file in the /original file system:# ls /snapshot file1 file2 file3
As you can see, the /original file system now holds 4 files, while the /snapshot file system only holds the original 3 files. The snapshot file system remains untouched.# touch /original/file4 # ls /original file1 file2 file3 file4 # ls /snapshot file1 file2 file3
To remove the snapshot, a simple umount and lvremove will do:
So, if you want to run backups of your file systems, while ensuring no changes are being made, here's the logical order of steps that can be scripted:# umount /snapshot # lvremove -y /dev/mapper/extern-originalsnapshotlv
- Freeze the application
- Create the snapshot (lvcreate -s ...)
- Thaw the application
- Mount the snapshot (mkdir ... ; mount ...)
- Run the backup of the snapshot file system
- Remove the snapshot (umount ... ; lvremove ... ; rmdir ...)
If you found this useful, here's more on the same topic(s) in our blog:
- Removing a LVM based file system
- Dmidecode
- Spreading logical volumes over multiple disks
- Enable the FTP server
- Identifying a Disk Bottleneck Using filemon
Interested in learning more?




