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.
About this post
- Posted
- Archived
- Edited
- Score
- 27 · 100% up
- Kind
- Text
- ID
- 49cs2a
12 comments
*cough*
snapper*cough*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:
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.
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.
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.
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?
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 usingrootflags=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_snapas 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 dolsat 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 withbtrfs 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 usingbtrfs subvolume set-default. I don't know how that works.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.
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).
https://wiki.archlinux.org/index.php/Snapper#Wrapping_upgrades_in_pre-post_snapshots