How to create automated btrfs snapshots with snapper

If you're running the btrfs file system on an external data drive or a production machine, learn how to automate the process of taking snapshots with the help of snapper.

Image: Jack Wallen

Does your data center rely on one or more Linux machines that take advantage of the powerful btrfs file system? If so, you probably already know how to take a snapshot of an existing btrfs subvolume. But you may not know how to automate this process.

If you're the creative sort, you can always roll your own script to automate the snapshot process, or you can use a very simple command called snapper that will take care of the heavy lifting. With this handy tool, you can set up regular, scheduled snapshots of all your btrfs subvolumes. Let's do just that.vI'll demonstrate how on a Ubuntu 16.04 machine with an external, btrfs-formatted drive with pre-existing subvolumes.

SEE: Why data center managers shouldn't overlook object storage backends

Installing snapper

Because snapper can be found in the standard repositories, there is only one command necessary for installation:

sudo apt-get install snapper

You now have the necessary tools to create scheduled btrfs snapshots.

Creating the config file

The first step is to create a config file for your subvolume. You have to create a config file for every subvolume you want backed up; in other words, if you have multiple sub-volumes, you'll have to do this for each one.

To create the config file, run the snapper command like so:

  1. Open a terminal window.
  2. Gain administrative access with the command sudo su.
  3. Issue the command snapper -c CONFIG create-config /PATH/TO/SUBVOLUME.

CONFIG will be the name of the new configuration file, and /PATH/TO/SUBVOLUME is the actual path to your subvolume. You need to issue this command for every subvolume that requires automatic snapshotting.

The above command will create a configuration file based on the included template /etc/snapper/config-templates/default. You can create your own templates, save them in the same directory, and base a new snapshot on the custom template by following these steps.

  1. Open a terminal window.
  2. Gain administrative access with the command sudo su.
  3. Issue the command snapper -c CONFIG create-config -t CUSTOM_DEFAULTS /PATH/TO/SUBVOLUME.

CONFIG will be the name of the new configuration file, CUSTOM_DEFAULTS is the name of your new template, and /PATH/TO/SUBVOLUME is the actual path to the btrfs subvolume.

Edit your config file

The snapper config files are in /etc/snapper/configs. There are a number of options that can be customized within that file, but there is only one line that must be configured for hourly snapshots. Within the config file, you'll see this line:


When that option is set to "yes," it will create hourly snapshots for your subvolume. This will kick off as soon as you issue the snapper command to create the config file. If you want to check to see if your hourly snapshot has been created, issue the command (making sure to have administrator access) snapper -c CONFIG list (CONFIG is the name of the configuration file). When you issue the command, you'll see how many snapshots have been taken, the time they were taken, and a few more bits of information (Figure A).

Figure A

Figure A
Image: Jack Wallen
The config snapshot been taken twice, and the sub2 snapshot has run once.

Each snapshot is retained within a hidden directory inside of the subvolume. So, if you create a snapshot for sub2, the snapshot will be found in sub2/.snapshots.

Reverting to a snapshot

Unfortunately, the snapper tool does not have the ability to revert a current subvolume to a snapshot. In order to do that, you have to manually copy that snapshot in place of the subvolume.

This is working only on an external data drive--the process for rolling back a snapshot on a running environment is quite different, and we'll address that process in an upcoming post.

Do even more with snapper

We have only scratched the surface of the snapper tool. For more information about how to get the most out of this handy btrfs snapshot tool, issue the command man snapper and read, in detail, what all it can do for you.

Also see