Never Ending Security

It starts all here

How to Create Filesystem Snapshots using Snapper on Linux

How to Create Filesystem Snapshots using Snapper

Snapper is a tool for Linux filesystem snapshot management. Apart from the obvious creation and deletion of snapshots, it can compare snapshots and revert differences between snapshots. In simple terms, this allows root and non-root users to view older versions of files and revert changes.

The features include:

  • Manually create snapshots
  • Automatically create snapshots, e.g. with YaST and zypp
  • Automatically create timeline of snapshots
  • Show and revert changes between snapshots
  • Works with btrfs, ext4 and thin-provisioned LVM volumes
  • Supports Access Control Lists and Extended Attributes
  • Automatic cleanup of old snapshots
  • Command line interface
  • D-Bus interface
  • PAM module to create snapshots during login and logout

snapper documentation

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.

# rpm -ivh snapper-x.x.x.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
  • pam_snapper
  • snapper-debuginfo
  • snapper-debugsource

Create btrfs Filesystem

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 Aug 24 15:57:00 2014 | root |         | Snapshot taken on 08-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 Aug 24 11:28 testfile2
-rw-r--r-- 1 root root 43 Aug 24 11:28 testfile3
drwxr-x--- 1 root root  2 Aug 24 15:57 .snapshots
-rw-r--r-- 1 root root  0 Aug 24 16:25 testfile1

After the above change, let us take another snapshot.

# snapper --config snapconfig create --description "Snapshot taken on 08-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 Aug 24 15:57:00 2014 | root |         | Snapshot taken on 08-24-0354 |
single | 2 |       | Mon Aug 24 16:27:48 2014 | root |         | Snapshot taken on 08-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 Aug 24 15:57:00 2014 | root |         | Snapshot taken on 08-24-0354 |
single | 2 |       | Mon Aug 24 16:27:48 2014 | root |         | Snapshot taken on 08-24-0427 |
single | 3 |       | Mon Aug 24 16:37:53 2014 | root |         | Snapshot taken on 08-24-0437 |
single | 4 |       | Mon Aug 24 16:38:17 2014 | root |         | Snapshot taken on 08-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-08-24 16:25:44.416490642 -0500
+++ /snapmount/.snapshots/1/snapshot/testfile1  2014-08-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 Aug 24 11:28 testfile3
-rw-r--r-- 1 root root  0 Aug 24 16:25 testfile1
drwxr-x--- 1 root root 10 Aug 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 Aug 24 11:28 testfile3
-rw-r--r-- 1 root root  0 Aug 24 16:25 testfile1
drwxr-x--- 1 root root 10 Aug 24 16:45 .snapshots
-rw-r--r-- 1 root root 11 Aug 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

More information can be found at: https://github.com/openSUSE/snapper and at: http://snapper.io/documentation.html

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s