btrfs, linux, OYB software, shell

Schedule BTRFS snapshots with btrfs-snp

To complement the last BTRFS tool btrfs-du, I would like to share a simple script for creating BTRFS snapshots that plays well with cron and systemd timers. This allows us to easily automate snapshot creation.

I was inspired by btrfs-snap by Birger Monsen.

Usage

btrfs-snp can be run manually, or summoned from cron. Invoke without arguments to see usage details.

Snapshot of /home

Tagged snapshot of root

Tagged snapshot of root, but keep maximum 10 snapshots

An example of running from cron: create and keep an hourly snapshot for one day, a daily snapshot for one week, a weekly snapshot for one month, and a monthly snapshot for one year.

Don’t forget to run

After some hours, you will see snapshots populating

Installation

Get the script and make it executable. You can do this in two lines, but better inspect it first. Don’t trust anyone blindly.

If you want to run it from cron, you might have to intall it first because some distributions have already completely replaced it by systemd timers. This was the case for me in Arch Linux.

In my case, I installed cronie

cronie logs the output to the system log by default, but you can set an email system if you want old style cron mails.

Also, note that you can use chronic if you only want logging to occur only if something goes wrong.

Details

There are a dizillion scripts to schedule snapshots, but I could not decide for one.

I didn’t want to use the available python scripts because in my eyes they are overcomplicated. This is a simple task, and I like simple code that I can read easily.

btrfs-snap was my favourite for several reasons: it uses the right tool for the job, which is bash, or maybe python. Also, I liked the idea of preventing snapshots from being created too often to simplify cron usage.

I liked the idea, but not the implementation. btrfs-snap has too much code for what it does, and several considered bad practices in the coding style. Also, some of the options just don’t work.

The idea of using transid is good, but in my testing, the transid changes by checking it, so doesn’t quite work. Also, btrfs-snap doesn’t detect changes outside of the mountpoint root, so it refuses to snapshot even when there have been changes.

I started writing a pull request, but I ended up changing all the code so I just started from zero.

Hope it helps!

Code

Author: nachoparker

Humbly sharing things that I find useful [ github dockerhub ]

5 Comments on “Schedule BTRFS snapshots with btrfs-snp

  1. Hi I’m trying to move to using btrfs-snp. Previously I was using btrfs-snap but it no longer works for my kernel,

    I like the idea of using cron to schedule this. But I stuck a problem, Unlike btrfs-snap I cant snapshot the contents of a subvolume within mounting it

    i.e /mnt is my only btrfs mount point and the snapshots work but I cant specify a subvolume to snapshot that isnt mounted like /mnt/Data/Doumentation. as these are subvolumes of /mnt.

    Do I have to mount each subvolume to be able to snapshot them? or is there a recursive option?

    Thanks
    Tristan

    I hope this makes sense,

    1. Mmm I haven’t encountered that situation before. I don’t know what btrfs-snap does, probably just mount it automatically. If someone else has this use case, you can open a feature requst on github, or send a PR.

Leave a Reply

Your email address will not be published. Required fields are marked *