54 lines
1017 B
Bash
Executable File
54 lines
1017 B
Bash
Executable File
#!/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="$SNAPPATH/prev"
|
|
CUR="$SNAPPATH/cur"
|
|
TMP="$SNAPPATH/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 || return 1
|
|
|
|
# Mark snapshot transmission as complete
|
|
[ -L "$CUR" ] && \
|
|
mv -v "$CUR" "$PREV" >&2
|
|
mv -v "$TMP" "$CUR" >&2
|