# Use Snapper to Automate Btrfs Snapshots
Hello, and welcome to today's video on using Snapper on Oracle Linux 8.
This video aims to install, configure, and use snapper to create, compare, and automate
snapshots.
So let us get started.
Quickly stated, snapper is a utility for managing BTRFS snapshots.
It allows for snapshot creation and deletion while also enabling users to compare the difference
in snapshots and revert changes at the file level.
Before we get started with snapper, if you are not familiar with or need a refresher
using BTRFS, check out our other BTRFS videos for details.
[ silence while showing ... "Get Started with the Btrfs File System" and "Btrfs Subvolumes
and Snapshots" ]
Now, time to schedule the creation of a few snapshots.
Begin by connecting to a system with existing BTRFS subvolumes.
The system we are using is a preconfigured Oracle Linux 8 instance on Oracle Cloud Infrastructure.
Next, we need to install Snapper.
For Oracle Linux 8, the Snapper utility is in the `ol8_UEKR6` repository, which our system
has enabled by default.
Using DNF, install snapper by running `sudo dnf install -y snapper`.
Since our instance of Oracle Linux 8 has `enforcing` mode enabled by default for SELinux, we need
to address an SELinux policy constraint.
The default SELinux policy settings prevent snapper from accessing the BTRFS subvolumes
located at `/data`.
Correct the SELinux access to our resources by creating a new policy or set snapperd_t
to a permissive domain.
For this environment, we set snapper to a permissive domain by running `sudo semanage
permissive -a snapperd_t`.
Using this approach, we still use the power and security of SELinux.
If we later decide to reverse this change to SELinux, run `sudo semanage permissive
-d snapperd_t`.
Once installed and SELinux access granted, we create a snapper configuration file that
expects an existing BTRFS subvolume as a parameter.
Type `sudo snapper -c dir1 create-config -f btrfs /data/dir1`.
When run, this command does the following:
adds a `dir1` entry in the `/etc/sysconfig/snapper` file
generates a configuration file `/etc/snapper/configs/dir1` and
places a `.snapshots` directory in the specified subvolume for the snapper generated snapshots.
Next, repeat this step for each subvolume that requires automatic snapshots.
Since our system has two subvolumes, we run `sudo snapper -c dir2 create-config -f btrfs
/data/dir2` for the second one.
With the configuration's created, get a listing using `sudo snapper list-configs`.
Run a directory listing and notice that Snapper places all the config files in `/etc/snapper/configs.
Next, we open one of the configuration files and review the default settings.
Using vi, open the file `/etc/snapper/configs/dir1` and look for the line `TIMELINE_CREATE="yes"`.
Having this value set to `yes` and the snapper systemd timer unit enabled allows periodic
snapshot events to trigger automatically.
Setting this value to `no` will disable these periodic snapshots for the specific configuration
and subvolume.
The other values to adjust are those beginning with `TIMELINE_LIMIT`.
The Snapper timeline configuration keeps ten hourly, ten daily, ten monthly, and ten yearly
snapshots by default and uses the `TIMELINE_LIMIT` values to prune the number of snapshots during
cleanup.
If we did not want to retain any snapshots for a given value of `TIMELINE_LIMIT`, then
set the value to `0`, which turns off that specific timeline.
With our review complete, close the configuration file.
Complete the configuration of automatic snapshots by enabling the systemd timer.
Run `sudo systemctl enable --now snapper-timeline.timer`.
Snapshots will begin in an hour, provided the `TIMELINE_LIMIT_HOURLY` value was not
disabled.
For more information on the Snapper configuration, see the `snapper-config` manual page.
Besides the automatic snapshots, Snapper also allows for manual snapshots.
When using the manual approach, the snapper utility can create three types of snapshots,
the `pre`, the `post`, and the `single`.
The `pre` and `post` should be done in pairs and ran before and after modifications to
a subvolume's state.
The `single` snapshot is also used to record a subvolume state but is not associated with
any other snapshots.
They are primarily helpful for periodic backup purposes.
As a quick practice, create a `single` snapshot by running `sudo snapper -c dir1 create --description
"test single"`.
If the subvolume modifications may need rolling back, use the `pre` and `post` types.
To demonstrate this, start by creating a `pre` snapshot with `sudo snapper -c dir2 create
-t pre -p`.
Note the number of the snapshot printed, as we will use it later in the `post` snapshot
command.
Now modify the subvolume by adding several new files with `sudo touch /data/dir2/file{4,5}.txt`.
Get a new directory listing using `tree /data`, which shows the added text files.
Then create the `post` snapshot by running `sudo snapper -c dir2 create -t post --pre-number
`.
The index number used corresponds to the `pre` snapshot number we mentioned earlier.
Another way to accomplish the same task is to use the `--command` flag.
In this senerio, create the `pre/post` snapshots by running `sudo snapper -c dir1 create --command
'fallocate -l 2G /data/dir1/file6.img'`.
Running this command causes Snapper to wrap the requested action in a set of `pre/post`
snapshots.
This feature does not appear too significant when used with a single fallocate command,
but imagine using it while running a script that makes hundreds of modifications to the
subvolume.
If there is a mistake during that script, having the ability to roll back would be beneficial.
So now we have snapshots generated.
What next?
First, let us see what snapshots exist based on the configurations we did.
To get a list of existing snapshots for a given subvolume, run `sudo snapper -c dir2
list`.
Then get a list of files and directories that were added, removed, or modified between snapshots.
Type `sudo snapper -c dir2 status ..`
The snapper command further allows us to determine the difference in the files' contents in the
snapshots using the `diff` option.
Getting a `diff` is possible by default on the entire snapshot or referencing a single
file.
For a single file, modify one of the subvolume files by adding another line of text using
`echo "Hello hello hello" | sudo tee -a /data/dir2/file3.txt`.
Then create a new snapshot with `sudo snapper -c dir2 create --description "file3 changed"`.
Get a listing of the current snapshots by running `sudo snapper -c dir2 list`.
Take note of the new snapshot number.
Finally, get the `diff` of this change by running `sudo snapper -c dir2 diff <original
snapshot index>.. `.
The last feature we cover is the ability to delete snapshots.
Depending on how many snapshots you need to delete, Snapper gives several options.
For a single snapshot, run `sudo snapper -c dir1 delete `.
If we wanted to delete multiple snapshots, add another snapshot id to the command separated
by a space.
Or we can delete an entire range of snapshots by specifying the starting snapshot id, then
a dash, and then the ending snapshot id.
Beyond what we showed today, snapper can do much more, so continue to experiment and put
your newfound skills to use.
For additional information, see the snapper manual page or the upstream Snapper project
page at snapper.io.
That concludes this video.
Thank you for watching, and enjoy using Oracle Linux 8.
Please check our additional information on Oracle Linux 8 in the Oracle Linux 8 Documentation
or Oracle Linux Learning Library.