Add snapshot and move send-delta, plus cleanup

This commit is contained in:
Alex D. 2024-11-10 10:13:15 +00:00
parent f641a5445e
commit 7f69ed2446
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
3 changed files with 61 additions and 57 deletions

View File

@ -1,47 +0,0 @@
#!/bin/sh
set -e
SRC="$1"
RECV="$2"
DSTDIR="$SRC/${3:-.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

View File

@ -4,15 +4,13 @@
# CC0 ~caskd
: ${SRC:?'Source not defined'}
: ${DEST:?'Destination not defined'}
: ${NAME:?'Name not defined'}
: ${SN:?'Selection name not defined'}
: ${RT:?'Retention time not defined'}
: ${SNAPDIR:?'Snapdir not defined'}
: ${SELECTOR:='+%H'}
NAME="$(date "$SELECTOR")"
s="$SRC"
d="$DEST/$SN/$NAME"
SNAPSRC="$SRC"
SNAPDEST="$SRC/$SNAPDIR/$NAME"
mkdir -p "${d%/*}"
find "$DEST" -mindepth 1 -maxdepth 1 -type d -ctime +"$RT" -exec echo "{}/$NAME" ';' | xargs btrfs subvolume del
btrfs subvolume delete "$d"
btrfs subvolume snapshot -r "$s" "$d"
mkdir -p "${SNAPDEST%/*}"
btrfs subvolume delete "$SNAPDEST"
btrfs subvolume snapshot -r "$SNAPSRC" "$SNAPDEST"

53
btrfs/subvolume-delta Executable file
View File

@ -0,0 +1,53 @@
#!/bin/sh
#
# BTRFS delta snapshots with incomplete send handling
# CC0 ~caskd
: ${SRC:?'Source not defined'}
: ${SNAPDIR:?'Snapdir not defined'}
: ${DEST:?'Destination not defined'}
CDATE="$(date '+%s')"
SNAPSRC="$SRC"
SNAPPATH="$SNAPSRC/$SNAPDIR"
PREV="$SRC/prev"
CUR="$SRC/cur"
TMP="$SRC/tmp"
SNAPDEST="$SNAPPATH/$CDATE"
msg() {
echo "$1" >&2
}
delta() {
[ -d "$(realpath "$CUR")" ] || return 1
msg "Attempting to send delta"
btrfs -v send --compressed-data -p "$CUR" "$TMP" | btrfs -v receive "$DEST"
}
full() {
msg "Attempting to send full"
btrfs -v send --compressed-data "$TMP" | btrfs -v receive "$DEST"
}
symdel() {
if [ -L "$1" ]; then
btrfs subvolume delete "$1" >&2 || true
rm -v "$1" >&2
fi
}
mkdir -vp "$SNAPPATH" >&2
symdel "$TMP"
symdel "$PREV"
ln -sv "$CDATE" "$TMP" >&2
btrfs subvolume snapshot -r "$SNAPSRC" "$SNAPDEST" >&2
delta || full
# Mark snapshot transmission as complete
[ -L "$CUR" ] && \
mv -v "$CUR" "$PREV" >&2
mv -v "$TMP" "$CUR" >&2