Schnapps for Arch Linux

I love schnapps for creating snapshots.

I have thought to use it for my Arch Linux Laptop too.

First you have to move the rootfs to @

btrfs subvolume snapshot / /@

Now edit /@/etc/fstab and set subvol=@
Example:

UUID=<long-uuid> / btrfs rw,relatime,ssd,space_cache,subvol=@

mount our new root filesystem somewhere, mount the relevant fileystems (dev, sys, proc, boot if you have it), chroot to it and update grub:

mount -o subvol=@ /dev/mapper/lvm-root /media/temporary
cd /media/temporary
mount -o bind /dev  dev
mount -o bind /sys  sys
mount -o bind /proc proc
mount -o bind /boot boot
chroot .
grub-mkconfig -o /boot/grub/grub.cfg
exit

Get schnapps

wget https://gitlab.labs.nic.cz/turris/misc/raw/master/schnapps/schnapps.sh

move it to /usr/local/bin and edit it

mv schnapps.sh /media/temporary/usr/local/bin/schnapps
vim /media/temporary/usr/local/bin/schnapps

Change ROOTDEV to your root device
Example:

ROOTDEV="/dev/mapper/lvm-root"

Save, quit and reboot

You can now mount the actual root somewhere to remove all its contents aside of the @ subvolume.
Example:

mount /dev/mapper/lvm-root /mnt/btrfs-root

Now you can use schnapps on Arch Linux as you do on your beloved Turris :slight_smile:

I hope this post isn’t to off topic for this forum.

2 Likes

Trigger Schnapps before and after pacman installs/upgrades/removes packages

/usr/share/libalpm/hooks/preupdate-schnapps.hook

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

[Action]
Description=Create Snapshot pre update
When = PreTransaction
Exec = /usr/local/bin/schnapps create -t pre "Automatic pre-update snapshot"

And /usr/share/libalpm/hooks/postupdate-schnapps.hook

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

[Action]
Description=Create Snapshot post update
When = PostTransaction
Exec = /usr/local/bin/schnapps create -t post "Automatic post-update snapshot"

Really cool

And now to the Timers (replacing cron on Arch).

/usr/lib/systemd/system/schnapps-cleanup.service

[Unit]
Description=Cleanup Schnapps snapshots

[Service]
Type=oneshot
ExecStart=/usr/local/bin/schnapps cleanup
IOSchedulingClass=idle

/usr/lib/systmd/system/schnapps-cleanup.timer

[Unit]
Description=Daily Schnapps Cleanup

[Timer]
OnCalendar=daily
AccuracySec=12h
Persistent=true
Unit=schnapps-cleanup.service

[Install]
WantedBy=timers.target

/user/lib/systemd/system/schnapps-hourly.service

[Unit]
Description=Create hourly Schnapps snapshot

[Service]
Type=oneshot
ExecStart=/usr/local/bin/schnapps create -t time "Hourly Schnappsshot"
IOSchedulingClass=idle

/usr/lib/systemd/system/schnapps-hourly.timer

[Unit]
Description=Create hourly Schnappsshot

[Timer]
OnBootSec=30min
OnUnitActiveSec=1h
Unit=schnapps-hourly.service
Persistent=true

[Install]
WantedBy=timers.target

This runs cleanup twice a day and every hour takes a snapshot

Configure retention paramters in /etc/schnapps

Create /etc/schnapps and set the desired values:

KEEP_MAX_SINGLE=-1
KEEP_MAX_TIME=5
KEEP_MAX_UPDATER=10
KEEP_MAX_ROLLBACK=3

-1 means infinite (do not delete) which is good for manually created snapshots

Grub BTRFS

List snapshots in GRUB for easy rollback

Change /usr/local/bin/schnapps

After the line

echo "Snapshot number $NUMBER created"

Add the line

grub-mkconfig -o /boot/grub/grub.cfg

This way every time a snapshot is created it will be selectable from GRUB on boot.

EDIT:

On removal of a snapshot the list of available snapshots has to be updated too.

So after the line

echo "Snapshot $NUMBER deleted."

also add

grub-mkconfig -o /boot/grub/grub.cfg