Fix incremental snapshot logic.

This commit is contained in:
2025-10-22 13:41:28 +01:00
parent 65835e1ed0
commit f488b710bf

View File

@@ -124,20 +124,27 @@ in
# Find previous snapshot on sender
PREV_LOCAL=$(ls -t /persist/services@* 2>/dev/null | grep -v "^$SNAPSHOT_PATH$" | head -1 || true)
# Check what snapshots exist on the receiver
REMOTE_SNAPSHOTS=$(ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new root@${standby} \
"ls -t /persist/services-standby/services@* 2>/dev/null || true")
# Try incremental send if we have a parent, fall back to full send if parent missing on receiver
if [ -n "$PREV_LOCAL" ]; then
echo "Attempting incremental send from $(basename $PREV_LOCAL) to ${standby}"
# Decide: incremental or full send
if [ -n "$PREV_LOCAL" ] && echo "$REMOTE_SNAPSHOTS" | grep -q "$(basename "$PREV_LOCAL")"; then
# Receiver has the parent snapshot, do incremental
echo "Incremental send from $(basename $PREV_LOCAL) to ${standby}"
btrfs send -p "$PREV_LOCAL" "$SNAPSHOT_PATH" | \
# Capture both stdout and stderr to check for parent missing error
if OUTPUT=$(btrfs send -p "$PREV_LOCAL" "$SNAPSHOT_PATH" 2>&1 | \
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new root@${standby} \
"btrfs receive /persist/services-standby" 2>&1); then
echo "Incremental send completed successfully"
elif echo "$OUTPUT" | grep -q "cannot find parent subvolume"; then
echo "Parent snapshot not found on receiver, falling back to full send"
btrfs send "$SNAPSHOT_PATH" | \
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new root@${standby} \
"btrfs receive /persist/services-standby"
else
# Receiver doesn't have parent (new standby or missing snapshot), do full send
echo "Full send to ${standby} (new standby or parent snapshot not found on receiver)"
echo "ERROR: Incremental send failed: $OUTPUT"
exit 1
fi
else
# First snapshot, do full send
echo "Full send to ${standby} (first snapshot)"
btrfs send "$SNAPSHOT_PATH" | \
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new root@${standby} \
"btrfs receive /persist/services-standby"