How to create snapshot of LVM thin volumes using snapper command

Snapper is a command-line utility in Linux used to create and manage snapshots of LVM thin volumes. It can create, delete, and compare snapshots and revert changes done between snapshots. Snapper also allows for the easy creation and management of snapshots for Btrfs.

Installing snapper

Use the yum command to install the snapper software package.

# yum install snapper
...
Transaction Summary ============================================================= 
Install 1 Package (+3 Dependent packages)
Total download size: 499 k Installed size: 1.5 M
Is this ok [y/d/N]: y
...
Complete!

Use the rpm –ql command to view the files provided by the snapper package.

# rpm –ql snapper
/etc/cron.daily/snapper 
/etc/cron.hourly/snapper 
/etc/dbus-1/system.d/org.opensus.Snapper.conf 
/etc/logrotate.d/snapper
/usr/bin/snapper
/usr/sbin/snapperd /usr/share/dbus-1/system-services/org.opensus.Snapper.service 
...
Note the two cron snapper files.
– By default, snapper sets up a cron.hourly job to create snapshots in the .snapshots subdirectory of the volume and a cron.daily job to clean up old snapshots.
– You can edit the configuration file to disable or change this behavior.

1. Creating a Thinly Provisioned Logical Volume

Use the lvcreate command to create a thin provisioned volume named – thinvol1. Note that this volume needs to be mounted before we create the snapshot using the snapper command.

# vgs
VG      #PV #LV  #SN  Attr     VSize      VFree
myvolg  1    0    0   wz--n-   1020.00m   1020.00m

Create a thin pool:

# lvcreate –v –L 100m –T myvolg/mythinpool 
...
  Logical volume “mythinpool” created

Create thinly provisioned volume :

# lvcreate –v –V 200m –T myvolg/mythinpool –n thinvol1 
...
  Logical volume “thinvol1” created

Create a file system on the thin volume.

# mkfs.ext4 /dev/myvolg/thinvol1
...
Writing superblocks and filesystem accounting information: done

Mount the file system:

# mkdir /myvol1
# mount /dev/myvolg/thinvol1 /myvol1

2. Creating snapper configuration file

To create a snapshot using snapper, a configuration file is required for the LVM thin volume or Btrfs subvolume. The LVM and Btrfs volumes must also have a mounted file system. Use the create-config command to create the configuration file. The following example creates a configuration file named myvol_snap for an LVM ext4 file system mounted on /myvol1:

# snapper –c myvol1_snap create-config –f "lvm(ext4)" /myvol1

– This command adds an entry to /etc/sysconfig/snapper.
– This command creates the /etc/snapper/configs/myvol1_snap configuration file.
– This command creates a .snapshots directory in the /myvol1 directory.

# cat /etc/sysconfig/snapper 
... 
SNAPPER_CONFIGS=”myvol1_snap”
# cat /etc/snapper/configs/myvol1_snap ...
# subvolume to snapshot SUBVOLUME=”/myvol1”
# filesystem type
FSTYPE=”lvm(ext4)”
...
# start comparing pre- and post-snapshot in background after... BACKGROUND_COMPARISON=”yes”
# run daily number cleanup
NUMBER_CLEANUP=”yes”
...
# create hourly snapshots
TIMELINE_CREATE=”yes”
# cleanup hourly snapshots after some time
TIMELINE_CLEANUP=”yes”
...
# cleanup empty pre-post-pairs
EMPTY_PRE_POST_CLEANUP=”yes”
...

Snapshots of the /myvol1 file system are stored in the .snapshots subdirectory.

# ls –la /myvol1
...
drwxr-x--- ... .snapshots
...

Types of snapshots

There are three types of snapshots that you can create by using snapper:
pre: Use to record the state of a volume before a modification. Pre snapshots should always have a corresponding post snapshot.
post: Use to record the state of a volume after a modification.
single: These snapshots have no special relationship to other snapshots.

3. Creating snapshot

Create some test file in the /myvol1 directory.

# echo "This is a test file" > /myvol1/test_file

Use the snapper create -t pre to create a pre snapshot of the volume defined by the myvol1_snap configuration file. Include the –p option to display the number of the snapshot being created. (In this example, the pre snapshot number is 1)

# snapper –c myvol1_snap create –t pre –p
1

The snapshots are stored by snapshot number in the .snapshots subdirectory of the volume. View the contents of the /myvol1/.snapshots directory.

# ls –l /myvol1/.snapshots 
...
drwxr-xr-x ... 1

View the contents of the /myvol1/.snapshots/1 directory.

# ls –l /myvol1/.snapshots/1 
...
-rw-------    ...     info.xml 
drwxr-x---    ...     snapshot

Now modify the content of /myvol1 directory by removing the test_file.

# rm /myvol1/test_file

Use the snapper create -t post to create a post snapshot of the volume defined by the myvol1_snap configuration file. Include the –pre-num 1 option to associate this post snapshot with the pre snapshot 1. Include the –p option to display the number of the snapshot being created.

# snapper –c myvol1_snap create –t post --pre-num 1 –p 
2

You would see a new directory names 2 created in the .snapshots directory.

# ls –l /myvol1/.snapshots/2 
...
-rw------- ... filelist-1.txt 
-rw------- ... info.xml 
drwxr-x--- ... snapshot

The filelist-1.txt contains the file that was deleted after the pre snapshot and before the post snapshot.

# cat /myvol1/.snapshots/2/filelist-1.txt 
-..... /test_file

Comparing pre and post snapshots

Use the snapper status command to display the files and directories that have been added, removed, or modified between pre snapshot 1 and post snapshot 2.

# snapper –c myvol1_snap status 1..2
-..... /test_file

Use the snapper diff command to display the differences between the contents of the files in pre snapshot 1 and post snapshot 2.

# snapper –c myvol1_snap diff 1..2
Binary files /myvol1/.snapshots/1/snapshot/test_file and /myvol1/.snapshots/2/snapshot/test_file differ

Use the snapper list command to list the snapshots that exist for volume defined by the myvol1_snap configuration file.

# snapper –c myvol1_snap list
Type  | # | Pre # | Date | User | Cleanup |Description| Userdata
------+---+-------+------+------+---------+------------+--------
single| 0 |       | ...  | root |         |  Current  |
pre   | 1 |       | ...  | root |         |       |
post  | 2 |   1   | ...  | root |         |           |

reverse the changes from post snapshot to pre shanshot

To undo the changes from post snapshot to pre snapshot i.e. to restore the test_file use the snapper undochange command.

# snapper –c myvol1_snap undochange 1..2 
create:1 modify:0 delete:0

Verify

# ls /myvol1
lost+found
test_file
Related Post