In dem aktuellen "Fedora KDE 42" hat Mastertac gefragt, wie genau ich mit Debian BTRFS-Snapshot bei jedem Update automatisch anlege. Ich benutzte dafür kein Timeshift, sondern eigene, auf btrbk basierende Skripts.
btrbk ( https://github.com/digint/btrbk ) ist ein Kommandozeilen-Tool zur Verwaltung von BTRFS-Snaphosts und Backups. Es ist selber ein einziges, wenn auch ziemlich langes Perl-Skript. Es sollte als Paket für alle größeren Distributionen verfügbar sein.
Die folgende Beschreibung ist nicht als Anleitung gedacht! Ich schreibe nur auf, wie ich es vor 2 Jahren für mich eingerichtet habe!
Ich habe meine Kommandos, die ich zunächst in einer VM getestet hatte, zu zwei Shell-Skripts destilliert, die man als Root ausführen müsste, möglichst kurz nach einer Debian-Installation. Voraussetzung ist natürlich, dass die Rootpartition mit BTRFS als Dateisystem formatiert ist, was man im Debian-Installer einstellen kann.
In einer VM würde der Debian-Installer die Root-Partition auf /dev/vda3 legen und ein Subvolume @rootfs anlegen. Um später Snapshots außerhalb des im laufenden Systems sichtbaren Root-Filesystems in /mnt/anlegen zu können wird noch folgender Eintrag in /etc/fstab angelegt, hier z.B. mit /dev/vda3 als Rootpartition:
/dev/vda3 /mnt/btr_pool btrfs defaults,subvolid=5 0 0
Die Snapshots werden dann später nach /mnt/btr_pool/@snapshots geschrieben. Die btrbk-Konfiguration wird nach /etc/btrbk.conf geschrieben. Darin wird definiert, dass Snapshots von @rootfs in einem Subvolume @snapshots geschrieben werden. In APT wird noch ein Hook eingerichtet, der das btrbk-Skript vor jedem Update aufruft. Read-only Snapshots sind in der Regel nicht bootbar, weil Linux erwartet, dass es in bestimmte Verzeichnisse z.B. Logdateien schreiben kann. Theoretisch könnte man für jedes dieser Verzeichnisse einen eigenes schreibbares Subvolume anlegen, wie es andere Tools auch machen. Ich halte so eine Lösung für viel zu fragil.
Stattdessen erzeuge ich bei Bedarf aus einem ro-Snaphsot einen rw-Snapshot. Man sollte wenigstens einen rw-Snapshot bereithalten, von dem man im Notfall booten kann (und neuere bootbare Snapshots erstellen kann).
#!/bin/bash
# Bitte Name des ersten Snapshots und der Root-Partition anpassen:
MyFirstBootableSnapshot=@deb12-backup1
RootDevice=$(mount | grep btrfs | awk '$3 == "/" && $5 == "btrfs" { print $1 }')
if [ -z "$RootDevice" ] ; then
>&2 echo "System partition has no BTRFS filesystem"
exit 1
fi
apt install btrbk
if ! fgrep -q /mnt/btr_pool /etc/fstab ; then
cat >> /etc/fstab << END
$RootDevice /mnt/btr_pool btrfs defaults,subvolid=5 0 0
END
fi
mkdir -p /mnt/btr_pool
mount /mnt/btr_pool
systemctl daemon-reload
btrfs sub create /mnt/btr_pool/@snapshots
# Konfiguriere btrbk:
cat > /etc/btrbk.conf << END
volume /mnt/btr_pool
# Create snapshots in /mnt/btr_pool/@snapshots
snapshot_dir @snapshots
snapshot_preserve_min 2d
snapshot_preserve 14d
subvolume @rootfs
snapshot_name d12
END
# Hook für dpkg, um einen Snapshot des Root-Filesystems automatisch vor einem Update anzustoßen:
cat > /etc/apt/apt.conf.d/70btrbk << END
// create a btrfs snapshot before (un)installing packages
Dpkg::Pre-Invoke {"/usr/bin/btrbk run @rootfs";};
END
# Erzeuge ersten ro-Snapshot:
btrbk run @rootfs
LatestSnapshotDir=$(btrbk list latest --format col:h:SNAPSHOT_SUBVOLUME @rootfs)
LatestSnapshot=${LatestSnapshotDir#/mnt/btr_pool/*}
# Erzeuge ersten bootbaren rw-Snapshot:
cd /mnt/btr_pool
btrfs subvol snapshot ${LatestSnapshot} ${MyFirstBootableSnapshot}
sed -i "s/subvol=@rootfs/subvol=${MyFirstBootableSnapshot}/" "${MyFirstBootableSnapshot}/etc/fstab"
Display More
Ein schreibbarer Snapshot des Root-Filesystems muss natürlich noch in grub eingetragen werden. Hierzu gibt es ein weiteres Projekt auf github: Antynea/grub-btrfs Zumindest vor 2 Jahren gab es das noch nicht als fertiges .deb-Paket. Hier bin ich einfach der Beschreibung auf der Projektseite gefolgt:
#!/bin/bash
## Hier Userid eintragen, unter der das Projekt geclont wurde:
USER=joachim
apt install git inotify-tools
su - $USER << END
mkdir -p ~/Projekte
cd ~/Projekte
git clone https://github.com/Antynea/grub-btrfs.git
cd ~/Projekte/grub-btrfsd
END
(cd /home/$USER/Projekte/grub-btrfsd && make install)
sed -i -e 's!^#?GRUB_BTRFS_IGNORE_SPECIFIC_PATH=!GRUB_BTRFS_IGNORE_SPECIFIC_PATH=("@" "@rootfs")!' \
-e 's!^#?GRUB_BTRFS_IGNORE_PREFIX_PATH=!GRUB_BTRFS_IGNORE_PREFIX_PATH=("@snapshots")!' \
/etc/default/grub-btrfs/config
update-grub
sed -i -e 's!^ExecStart=!ExecStart=/usr/bin/grub-btrfsd --syslog /mnt/btr_pool/' \
etc/systemd/system/grub-btrfsd.service
systemctl enable --now grub-btrfsd
systemctl daemon-reload
Display More
Ich benutzte btrbk auch für Backups meiner Home-Partition in einer davon unabhängigen Konfiguration. Das ist im Wesentlichen eine Kombination von btrbk und rsync.
Auf der btrbk-Projektseite gibt es auch ein Beispiel für diese Art des Backups.