Initial commit with btrfs delta

This commit is contained in:
Alex D. 2024-08-15 09:58:30 +00:00
commit bcd6f0f635
Signed by: caskd
GPG Key ID: F92BA85F61F4C173

47
btrfs/btrfs-subvolume-delta Executable file
View File

@ -0,0 +1,47 @@
#!/bin/sh
set -e
SRC="$1"
RECV="$2"
DSTDIR="$SRC/.snap"
CDATE="$(date '+%s')"
PREV="prev"
CUR="cur"
TMP="tmp"
msg() {
echo "$1" >&2
}
delta() {
[ -d "$(realpath "$DSTDIR/$CUR")" ] || return 1
msg "Attempting to send delta"
btrfs -v send --compressed-data -p "$DSTDIR/$CUR" "$DSTDIR/$TMP" | btrfs -v receive "$RECV"
}
full() {
msg "Attempting to send full"
btrfs -v send --compressed-data "$DSTDIR/$TMP" | btrfs -v receive "$RECV"
}
symdel() {
if [ -L "$1" ]; then
btrfs subvolume delete "$1" >&2 || true
rm -v "$1" >&2
fi
}
mkdir -vp "$DSTDIR" >&2
symdel "$DSTDIR/$TMP"
symdel "$DSTDIR/$PREV"
ln -sv "$CDATE" "$DSTDIR"/"$TMP" >&2
btrfs subvolume snapshot -r "$SRC" "$DSTDIR/$CDATE" >&2
delta || full
# Mark snapshot transmission as complete
[ -L "$DSTDIR/$CUR" ] && \
mv -v "$DSTDIR/$CUR" "$DSTDIR/$PREV" >&2
mv -v "$DSTDIR/$TMP" "$DSTDIR/$CUR" >&2