nixos-config/modules/borgHook.sh
2024-08-27 23:41:20 +02:00

31 lines
851 B
Bash
Executable file

#!/usr/bin/env 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