≡ Menu

How to Create Filesystem Snapshots using Snapper Command on Linux

Snapper is a Linux command-line tool to create and manage snapshots of your filesystems.

Using snapper command, you can create read-only snapshots. You can use these snapshots to restore specific files or all files during any disaster situation.

You can also use it to compare multiple snapshots and revert back to a specific old snapshots.

Snapper is supported only on btrfs (B-tree file systems copy-on-write), ext4 filesystems and thin-provisioned LVM based logical volumes.

When a snapshot is taken using the snapper command, it will reside in the same filesystem so there should be enough free space available and regular FS cleanup may be required.

If you are interested in using a rsync based snapshot, you should also check-out the rsnapshot utility that we discussed earlier.

Install Snapper Utility

You can either download the snapper binary for various distributions and install it, or download the source code and compile it yourself.

For example, you can download the snapper rpm’s from the SUSE SLES11 SP2 repository.

# rpm -ivh snapper-0.1.6-2.1.x86_64.rpm

The following are depdencies for the snapper package. When you use yum, or other package management tools, all the depdencies will be installed automatically.

  • libsnapper-devel-0.1.6-2.1.x86_64.rpm
  • pam_snapper-0.1.6-2.1.x86_64.rpm
  • snapper-debuginfo-0.1.6-2.1.x86_64.rpm
  • snapper-debugsource-0.1.6-2.1.x86_64.rpm

Create btrfs Filesystem

Since btrfs is supported filesystem from SLES11 SP2, You can use btrfs to create your logical volume or use btrfs-convert commands to convert your existing ext3 file system to btrfs.

Execute the following commands to create a new btrfs filesystem, If you do not have btrfs program installed then use zypper install btrfsprogs to install it.

# lvcreate -L 8G -n snapvol vglocal
Logical volume "snapvol" created

# mkfs.btrfs /dev/vglocal/snapvol

# mount /dev/vglocal/snapvol /snapmount

Create Thin-provisioned LVM

If you want to create a thin-provisioned LVM, use the lvcreate command to do the following.

# lvcreate --thin vglocal/vgthinpool --size 20G
  Rounding up size to full physical extent 32.00 MiB
  Logical volume "vgthinpool" create

# lvcreate --thin vglocal/vgthinpool --virtualsize 8G --name lvthin_snap
  Logical volume "lvthin_snap" created

# lvs
  LV          VG        Attr      LSize   Pool       Origin Data%  Move Log Copy%  Convert
  opt         vglocal -wi-ao---   2.73g
  tmp         vglocal -wi-ao---   2.73g
  usr_local   vglocal -wi-ao---   2.73g
  var         vglocal -wi-ao---   2.73g
  lvthin_snap vglocal  Vwi-a-tz-   8.00g vgthinpool          0.00
  vgthinpool  vglocal  twi-a-tz-  20.00g                     0.00

# mkfs.ext3 /dev/vglocal/lvthin_snap

# mkdir /snapmount

# mount /dev/vglocal/lvthin_snap /snapmount

Create Snapper Config File

To create the config file using snapper command, use the “snapper -c” command as shown below.

Syntax on btrfs:

snapper –c  create-config

On btrfs, you’ll just specify the config file name, and the mountpoint as shown below.

snapper -c snapconfig create-config /snapmount

Syntax on thin-provisioned LVM:

snapper –c  create-config --fstype="lvm(xfs)"

On thin-provisioned LVM, apart from specifying the config filename, and mountpoint, you should also specify the filesystem type using –fstype as shown below:

snapper -c snapconfig1 create-config --fstype="lvm(xfs)" /snapmount1

View and Delete Snapper Config Files

After creating the config files, you will see the .snapshots directory created under /snapmount directory.

You’ll also notice the config file is created under /etc/snapper/configs/snapconfig. Information about all the subvolume that is configured for snapshot will be stored in this file.

The log file used for troubleshooting purposes is located under /var/log/snapper.log

To view all the config files, execute the following snapper command:

# snapper list-configs
Config      	| 	Subvolume
------------+------------
snapconfig  	| 	/snapmount         ? btrfs filesystem
snapconfig1 	| 	/snapmount1       ? Thin provisioned filesystem

To delete a config file, use the following syntax:

snapper –c  delete-config

For example, the following command deletes the config file snapconfig under /etc/snapper/configs directory.

# snapper -c snapconfig delete-config

Create a snapshot using Snapper

Inorder to create a snapshot of the filesystem, use the following snapper command syntax:

snapper –config  create –description "description of the snapshot"

For example, the following will take a new snapshot.

# snapper --config snapconfig create --description "Snapshot taken on 02-24-0354"

Once you take a snapshot, view the snapshot information as shown below:

# snapper --config snapconfig list
Type   | # | Pre # | Date                     | User | Cleanup | Description                  | Userdata
-------+---+-------+--------------------------+------+---------+------------------------------+---------
single | 0 |       |                          | root |         | current                      |
single | 1 |       | Mon Feb 24 15:57:00 2014 | root |         | Snapshot taken on 02-24-0354 |

Take 2nd Snapshot for Comparision

For testing purpose, I nullified the testfile1 under /snapmount directory.

# cat /dev/null > testfile1

# ls -ltr
-rw-r--r-- 1 root root 11 Feb 24 11:28 testfile2
-rw-r--r-- 1 root root 43 Feb 24 11:28 testfile3
drwxr-x--- 1 root root  2 Feb 24 15:57 .snapshots
-rw-r--r-- 1 root root  0 Feb 24 16:25 testfile1

After the above change, let us take another snapshot.

# snapper --config snapconfig create --description "Snapshot taken on 02-24-0427"

As you see below, now we have two snapshots.

# snapper --config snapconfig list
Type   | # | Pre # | Date                     | User | Cleanup | Description                  | Userdata
-------+---+-------+--------------------------+------+---------+------------------------------+---------
single | 0 |       |                          | root |         | current                      |
single | 1 |       | Mon Feb 24 15:57:00 2014 | root |         | Snapshot taken on 02-24-0354 |
single | 2 |       | Mon Feb 24 16:27:48 2014 | root |         | Snapshot taken on 02-24-0427 |

Compare 1st and 2nd Snapshot

Now, let us compare both the snapshots.

The below command compares the snapshot#1 with the snapshot#2.

# snapper -c snapconfig status 1..2
c.... /snapmount/testfile1

In the output:

  • “c” on the output indicates that the content has been modified.
  • “+” indicates that newl files are added to the directory.
  • “-” indicates there are files that have been deleted.

Take Multiple Snapshots and Compare Output

I created multiple test snapshots with few files added, few files removed and few content changes.

# snapper --config snapconfig list
Type   | # | Pre # | Date                     | User | Cleanup | Description                  | Userdata
-------+---+-------+--------------------------+------+---------+------------------------------+---------
single | 0 |       |                          | root |         | current                      |
single | 1 |       | Mon Feb 24 15:57:00 2014 | root |         | Snapshot taken on 02-24-0354 |
single | 2 |       | Mon Feb 24 16:27:48 2014 | root |         | Snapshot taken on 02-24-0427 |
single | 3 |       | Mon Feb 24 16:37:53 2014 | root |         | Snapshot taken on 02-24-0437 |
single | 4 |       | Mon Feb 24 16:38:17 2014 | root |         | Snapshot taken on 02-24-0440 |

The following output list the files that are added, modified and deleted.

# snapper -c snapconfig status 4..1
-.... /snapmount/a
-.... /snapmount/b
-.... /snapmount/c
c.... /snapmount/testfile1
+.... /snapmount/testfile2

View the Difference Between Snapshots

Now to view the specific content difference in the file between snapshot#1 and snapshot#4, you can use the following command.

# snapper -c snapconfig diff 4..1 /snapmount/testfile1
--- /snapmount/.snapshots/4/snapshot/testfile1  2014-02-24 16:25:44.416490642 -0500
+++ /snapmount/.snapshots/1/snapshot/testfile1  2014-02-24 11:27:35.000000000 -0500
@@ -0,0 +1 @@
+This is a test file

The output is in the typical format of a diff command output.

Restore a Specific File from a Snapshot

Once you’ve seen the differences between snapshots, and you know what specific file you want to restore, you can restore it as explained here.

Before the restore, we don’t have the testfile2 in this list.

# ls -ltr
-rw-r--r-- 1 root root 43 Feb 24 11:28 testfile3
-rw-r--r-- 1 root root  0 Feb 24 16:25 testfile1
drwxr-x--- 1 root root 10 Feb 24 16:45 .snapshots

For example, to restore a single file from the snapshot, i.e /snapmount/testfile2 (the file that was deleted) from snapshot#1 use the below command:

# snapper -c snapconfig -v undochange 1..4 /snapmount/testfile2
create:1 modify:0 delete:0
creating /snapmount/testfile2

After the restore, we see the testfile2 in the list.

# ls -ltr
-rw-r--r-- 1 root root 43 Feb 24 11:28 testfile3
-rw-r--r-- 1 root root  0 Feb 24 16:25 testfile1
drwxr-x--- 1 root root 10 Feb 24 16:45 .snapshots
-rw-r--r-- 1 root root 11 Feb 24 16:55 testfile2

Restore All Files from a Snapshot

To restore all the files from the snapshot, do the following:

Now, let us restore all the files from a particular snapshot. Notice how this is deleting few files, creating a file, and modifying a file.

# snapper -c snapconfig -v undochange 1..4
create:1 modify:1 delete:3
deleting /snapmount/c
deleting /snapmount/b
deleting /snapmount/a
modifying /snapmount/testfile1
creating /snapmount/testfile2
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Stephan March 13, 2014, 5:19 am

    What about doing a similar article showing the snapshot command combined with tank send/receive in ZFS?

  • Reno January 21, 2015, 5:00 am

    Good article, thank you! 🙂