Skip to content

Create a Btrfs Snapshot just before Pacman Upgrades/Installs/Removes a Package

If you are using Btrfs, you a probably somewhat familiar with snapshots. The latest pacman has a hooks capability, so I've created one in which a snapshot is created just before Pacman completes a transaction. Here's the hook (put it at /usr/share/libalpm/hooks/snapshot.hook):

[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *

[Action]
Description = Btrfs snapshot before a transaction
Depends = btrfs-progs
When = PreTransaction
Exec = /usr/bin/dobtrfssnap

And here's the script it executes (put at /usr/bin/dobtrfssnap and make executable):

#!/bin/bash    
dirname=`date "+%FT%T"`
btrfs subvolume snapshot '/' /root/snapshots/pacman/$dirname

Also, you have to create /root/snapshots/pacman before using this.

Edited 7 Mar 2016. We only hold this version.

27 points12 comments
Open on Reddit
About this post
Posted
Archived
Edited
Score
27 · 100% up
Kind
Text
ID
49cs2a

Author

7
posts
103
comments
0
media
View profile

12 comments

*cough* snapper *cough*

6 points

I just happened to get snapper in place (only 2 years after I really wanted to get it going, but I've been busy).

I modified your executed script for my snapper setup to add it to the timeline pool as such:

'#!/bin/bash
snapper -c root create -d "Pacman Upgrade" --cleanup-algorithm timeline

snapper -c var create -d "Pacman Upgrade" --cleanup-algorithm timeline

For what it's worth I loosely followed this guide to get the initial snapper working. Perfect timing for posting this on the subreddit. Thanks.

5 points

Not to undermine OP's effort, but this is right. Snapper allows for greater flexibility with restoring and cleanup. OP should contribute this to the Snapper AUR package so everyone can easily benefit.

2 points

I've looked at snapper (and other programs like it). For me its just too many options and configuration. You should be able to create a similar hook for it though. All I need is this snapshot script as well as a backup script I have which does incremental back ups.

5 points

Now if an update breaks my system, how would I restore the snapshot so that everything is undone? I can imagine booting directly into a subvolume, but can I restore the default subvolume to it's former state?

3 points

I hope this will be understandable:

What you could do is, your "/" could really be a subvolume with a normal name in your btrfs filesystem. This name could be for example "_root".

You mount that "_root" subvolume as "/" by using a mount option subvol=_root. That's what you'd add in /etc/fstab. You add this to the kernel command line by using rootflags=subvol=_root.

You also create a subvolume "_root_snap" where you'll collect your snapshots. You mount that subvolume as "/.snapshots" or something by using subvol=_root_snap as a mount option in /etc/fstab. That's where your scripts will put snapshots. The proposed pacman hook script would for example put its snapshots in "/.snapshots/pacman/...".

To restore a snapshot, you boot into the Arch ISO and mount your btrfs filesystem without that subvol=... mount option. When you do ls at that point, what you will see is entries "_root" and "_root_snap". When you take a look inside "_root", you will see the contents of your "/". The pacman hook script's snapshots will be in "_root_snap/pacman/...". You rename or delete "_root". You snapshot one of the snapshots in "_root_snap/..." to create a new "_root".

Renaming subvolumes and moving them to different locations is done with mv. It behaves as if the subvolume would be a normal directory. Removing is done with btrfs subvolume delete.

A snapshot can be read-only or it can be writable. Your backup snapshots should probably be read-only. You snapshot a read-only snapshot to get a writable version of it.

There's also a default subvolume for a btrfs filesystem. That's what shows up as "/" if you don't use that subvol=... mount option. If you use that as your "/", you can somehow fix things up by using btrfs subvolume set-default. I don't know how that works.

6 points

Then my misunderstanding was probably that I always used the default subvolume as my "/". Could you skip booting from an iso by keeping a small "base" Arch system on another subvolume? Like having a recovery system that only has linux + btrfs-utils as subvol=_recovery? Then add a grub entry for the recovery system and restore the snapshot.
I think I'll have to try that out later.

4 points

Yes, you can boot into new subvolume.

For restoring you would need to delete old subvolume and create new snapshot (from new to old subvol).

2 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.