How to make automatic hourly/daily/weekly snapshots with Btrfs

0

I remember working at a company with a NetApp server in 2003, and we had a snapshot directory in our home directories, with various snapshots appearing in it. I think there were snapshots for the last minute, hour, day, week, etc. When I set up my first Btrfs filesystem, I was surprised that I couldn't find a script to automate the provision of a similar interface using Btrfs.

I can't remember exactly how the layout was set up, but I could imagine something like this:

$HOME/some_file
$HOME/.snapshot/minute/2016-04-16_07_33/some_file
                       2016-04-16_07_34/...
                       2016-04-16_07_35
                hour/2016-04-16_05_00
                     2016-04-16_06_00
                     2016-04-16_07_00
                day/2016-04-14
                    2016-04-15
                    2016-04-16
                week/2016-03-27
                     2016-04-03
                     2016-04-10

What I'm looking for is a script that I can run from cron, which will create the necessary Btrfs snapshots and mount them at the locations above. It should run once a minute. For each unit of time - minute/hour/day/week - it should keep some configurable number N of snapshots, for the last N units. In the above example, N would be 3. Older snapshots should be automatically deleted unless they are in use. "In use" could mean for instance that I have cd'ed to one of them, or bind-mounted it at another location.

Obviously it would also be nice to have simultaneous snapshots under / for the root system, or under /home/some_other_user/, and not just under my own $HOME.

Metamorphic

Posted 2016-04-16T13:56:35.560

Reputation: 325

snapper might be what you are looking for. – Martian – 2016-04-20T08:57:56.527

It might? :) I looked at the "snapper" documentation, and I don't see that it has anything like the feature I'm requesting. I'm not really sure what it simplifies, but my understanding is that it would be easier to implement a solution like the one I describe using the basic Btrfs interface, rather than trying to build it on top of snapper. – Metamorphic – 2016-05-02T21:45:36.340

The "Timeline" feature allows to automatically create and clean up old snapshots. Not sure about automounting volumes (IIUC it does so when using btrfs, and offers a CLI to see diffs in case it does not). It does not do snapshots every minute (I would consider automatic git commit on write and clean up before push if source code). And not sure it watches bind mounts and cd (I wonder how the NetApp did that, but that could be perhaps achieved by creating and mounting snapshot of a snapshot, which IMO would be easy to script.) – Martian – 2016-05-03T09:24:50.747

I now see what you're talking about. I searched for "snapper timeline feature" and got this:

https://wiki.archlinux.org/index.php/Snapper#Automatic_timeline_snapshots

There is even an example of getting 5-minute snapshots, although a bit of a hack. Thanks for the hint.

– Metamorphic – 2016-05-05T03:27:03.873

No answers