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