Supplementing btrfs-snap With apt-btrfs-snapshot

| Comments

I’ve been a very happy user of btrfs-snap for quite a while now. It is great for taking snapshots of a volume at regular intervals. In my opinion, this is a bit wasteful and cumbersome for the root volume. I don’t tend to change things on the root volume every hour, or even every day.

Ubuntu 11.10 now has apt-btrfs-snapshot available in the universe repository. This is an awesome little script that hooks into apt to create a snapshot right before packages are installed or upgraded.

The only thing I wasn’t too happy about was that apt-btrfs-snapshot doesn’t do any cleanup of old snapshots, so things were getting cluttered pretty quickly:

Screenshot of some btrfs snapshots

Cleaning things up a bit

I threw together my own little snapshot cleanup script to hook into apt. It automatically removes all but the five most recent snapshots. That should be enough history to keep me safe.

Screenshot of some btrfs snapshots, after clean up

Still using btrfs-snap as a safety net

I’m still using btrfs-snap on my root volume, but I’m not keeping anywhere near as much history as I used to. I’m only keeping a few hourly snapshots and one weekly snapshot. I’ll probably end up dropping the weekly snapshot.

Screenshot of btrfs snapshots of my root file system

apt-btrfs-snapshot-cleanup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
#! /bin/bash

DEVICE=/dev/sda2
KEEP=5
MP=`mktemp -d`

mount $DEVICE $MP

ls -dr $MP/@apt-snapshot-* | tail -n +$KEEP | while read snap ; do
  btrfs subvolume delete $snap
done

umount $MP

I also created a file called /etc/apt/apt.conf.d/81-btrfs-snapshot-cleanup containing this line:

apt-btrfs-snapshot-cleanup.sh
1
DPkg::Pre-Invoke {"if [ -x /usr/bin/apt-btrfs-snapshot ] && apt-btrfs-snapshot supported; then /home/wonko/bin/sbin/apt-btrfs-snapshot-cleanup.sh; fi "; };

This will make apt run this script right after it takes a snapshot. Just tweak that line to match the location of your apt-btrfs-snapshot-cleanup.sh script.

Comments