Add delta snapshot restoration script

This commit is contained in:
Alex D. 2025-01-15 12:22:28 +00:00
parent 66a463edd6
commit 6f88b06cf8
Signed by: caskd
GPG Key ID: F92BA85F61F4C173

67
btrfs/merge-delta-file Executable file
View File

@ -0,0 +1,67 @@
#!/bin/sh
#
# BTRFS delta snapshots restoration
# CC0 ~caskd
#
# This recursively goes to the initial full backup and restores every step to the requested source
set -e
: ${SRC:?'Source not defined'}
: ${SIZE:=1G}
DATANAME="data"
PARENTNAME="parent"
LOOPBACKNAME="restore"
TMPPATTERN="btrfs-restore.XXXXXXXXXX"
msg() {
echo "$1" >&2
}
cleanup_err() {
cleanup
rm -v "$LOOPBACKNAME"
}
cleanup() {
# Disable traps and cleanup
trap '' INT TERM EXIT HUP
umount -v "$TMPDIR"
rmdir "$TMPDIR"
}
decompress() {
zstd -d
}
delta_recieve() {
local CURRENT="$1"
local DEST="$2"
if [ -L "$CURRENT/$PARENTNAME" ]; then
local PARENT="$(realpath "$CURRENT/$PARENTNAME")"
if ! delta_recieve "$PARENT" "$DEST"; then
msg "Cannot receive $(basename "$PARENT") > $(basename "$CURRENT")"
return
fi
fi
if ! < "$CURRENT"/"$DATANAME" decompress | btrfs -v receive "$DEST"; then
msg "Failed to receive $(basename "$CURRENT")"
return
fi
}
trap cleanup_err INT TERM EXIT HUP
fallocate -l "$SIZE" "$LOOPBACKNAME" || return
LOOPBACK="$(losetup --show -f "$LOOPBACKNAME")" || return
echo "$LOOPBACK"
mkfs.btrfs "$LOOPBACK" || return
TMPDIR="$(mktemp -d "$TMPPATTERN")" || return
echo "$TMPDIR"
mount -t btrfs "$LOOPBACK" "$TMPDIR" || return
delta_recieve "$SRC" "$TMPDIR" || return
cleanup