made borg work with lvm

This commit is contained in:
mc-fucker 2024-08-27 22:47:38 +02:00
parent 9515c6087b
commit 5ac5c11823
2 changed files with 34 additions and 5 deletions

View file

@ -1,6 +1,7 @@
{ config, pkgs, ... }:
let
backuppath = "/backup/snapshot";
borgHook = toString ./borgHook.sh;
in
{
services.borgbackup.jobs.default = {
@ -27,11 +28,8 @@ in
environment.BORG_RSH = "ssh -o 'StrictHostKeyChecking=no' -i /root/.ssh/id_borg";
extraCreateArgs = "--verbose --stats";
startAt = "00:10";
preHook = ''if [[ -d ${backuppath} ]]; then
btrfs subvolume delete ${backuppath}
fi
btrfs subvolume snapshot / ${backuppath}''; #create snapshot for consistent filesystem
postCreate = "btrfs subvolume delete ${backuppath}"; #delete snapshot again
preHook = "${borgHook} pre ${backuppath}"; #create snapshot for consistent filesystem
postCreate = "${borgHook} post ${backuppath}"; #delete snapshot again
};
systemd.services.borgbackup-job-default = {

31
modules/borgHook.sh Executable file
View file

@ -0,0 +1,31 @@
#!/run/current-system/sw/bin/bash
hooktype="$1"
backuppath="$2"
fstype="$(df --output=fstype / | sed 1d)"
function dellvm {
umount ${backuppath}
lvremove -y "$1"
}
function delbtrfs {
btrfs subvolume delete ${backuppath}
}
if [[ "$fstype" == "btrfs" ]]; then
if [[ -d ${backuppath} ]]; then
delbtrfs
fi
if [[ "$hooktype" == "pre" ]]; then
btrfs subvolume snapshot / ${backuppath}
fi
else
snapname=nixos_snap
volume_group="$(mount | grep 'on / ' | cut -f 1 -d' ')"
vgname="$(lvs --noheadings "$volume_group" -o vg_name | tr -d ' ')"
snappath="/dev/mapper/${vgname}-${snapname}"
if [[ -b "$snappath" ]]; then
dellvm "$snappath"
fi
if [[ "$hooktype" == "pre" ]]; then
lvcreate -l50%FREE -s -n "$snapname" "$volume_group"
mount "$snappath" -o ro /backup/snapshot
fi
fi