Dir handling fixes

This commit is contained in:
2026-10-02 13:36:42 +03:00
parent c69f79ef28
commit 746f634a90
2 changed files with 42 additions and 11 deletions
+8 -5
View File
@@ -237,11 +237,14 @@ Options:
- By default, the generated local snapshots remain on the source. This allows a - By default, the generated local snapshots remain on the source. This allows a
failed run to be inspected or sent again. Remove them manually only after failed run to be inspected or sent again. Remove them manually only after
verification, or use `--destroy-source-snapshots` once the workflow is trusted. verification, or use `--destroy-source-snapshots` once the workflow is trusted.
- The remote command is `zfs receive -uF`. `-u` leaves received filesystems - The remote command is `zfs receive -u` (no `-F`). `-u` leaves received
unmounted. `-F` can roll back the destination filesystem and destroy newer filesystems unmounted. Filesystems that already exist on the destination
snapshots or changes that conflict with the incoming snapshot. Do not point (including the destination pool root) are skipped and never modified or
the script at a destination containing data you need unless you have reviewed rolled back; existing data on the new server is not deleted. To keep paths
the dry-run output and have a current backup. identical, pass the same name as the source pool, for example
`bash zfs-move-over-ssh.sh stor0 root@new-server stor0`, so `stor0/Documents`
arrives as `stor0/Documents`. Skipped and failed filesystems are listed at
the end, and the script continues after a failure.
- The script processes one filesystem at a time. If it stops partway through, - The script processes one filesystem at a time. If it stops partway through,
completed destination filesystems remain received. Re-running with the same completed destination filesystems remain received. Re-running with the same
source can create another timestamped snapshot; inspect both source and source can create another timestamped snapshot; inspect both source and
+34 -6
View File
@@ -196,50 +196,67 @@ send_progress() {
ls = s; lb = b; seen = 1; render(); next ls = s; lb = b; seen = 1; render(); next
} }
{ printf "\033[3A\033[J%s\n\n\n\n", $0 } { printf "\033[3A\033[J%s\n\n\n\n", $0 }
END { if (est > 0) b = est; render() } END { render() }
' >&4 3>&- ' >&4 3>&-
} }
prior_weight=0 prior_weight=0
index_now=0 index_now=0
FAILED=()
SKIPPED=()
remote_dataset_exists() {
ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" \
"sudo -n zfs list -H -o name -t filesystem '$1' >/dev/null 2>&1"
}
printf 'Migration snapshot: %s\n' "$SNAPSHOT_NAME" printf 'Migration snapshot: %s\n' "$SNAPSHOT_NAME"
for filesystem in "${FILESYSTEMS[@]}"; do for filesystem in "${FILESYSTEMS[@]}"; do
destination="$(map_destination "$filesystem")" destination="$(map_destination "$filesystem")"
snapshot="${filesystem}@${SNAPSHOT_NAME}" snapshot="${filesystem}@${SNAPSHOT_NAME}"
remote_snapshot="${destination}@${SNAPSHOT_NAME}" remote_snapshot="${destination}@${SNAPSHOT_NAME}"
index_now=$((index_now + 1))
# Never touch datasets that already exist on the destination (no -F, no rollback).
if ! $DRY_RUN && remote_dataset_exists "$destination"; then
printf 'Skipping %s: %s already exists on %s\n' "$filesystem" "$destination" "$REMOTE_HOST"
SKIPPED+=("$filesystem")
prior_weight=$((prior_weight + ${WEIGHT[$filesystem]:-0}))
continue
fi
printf 'Transferring %s to %s:%s\n' "$filesystem" "$REMOTE_HOST" "$destination" printf 'Transferring %s to %s:%s\n' "$filesystem" "$REMOTE_HOST" "$destination"
run zfs snapshot "$snapshot" run zfs snapshot "$snapshot"
if $DRY_RUN; then if $DRY_RUN; then
printf '+ zfs send -p %q | ssh' "$snapshot" >&2 printf '+ zfs send -p %q | ssh' "$snapshot" >&2
printf ' %q' "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'" >&2 printf ' %q' "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -u '$destination'" >&2
printf '\n' >&2 printf '\n' >&2
else else
if remote_snapshot_exists "$remote_snapshot"; then if remote_snapshot_exists "$remote_snapshot"; then
remove_remote_snapshot "$remote_snapshot" remove_remote_snapshot "$remote_snapshot"
fi fi
if $UI; then if $UI; then
index_now=$((index_now + 1))
ssh_err="$(mktemp)" ssh_err="$(mktemp)"
send_status=0 send_status=0
{ {
zfs send -p -P -v "$snapshot" 2>&1 1>&3 3>&- \ zfs send -p -P -v "$snapshot" 2>&1 1>&3 3>&- \
| send_progress "$index_now" "${#FILESYSTEMS[@]}" "$filesystem" \ | send_progress "$index_now" "${#FILESYSTEMS[@]}" "$filesystem" \
"$prior_weight" "${WEIGHT[$filesystem]:-0}" "$TOTAL_WEIGHT" "$prior_weight" "${WEIGHT[$filesystem]:-0}" "$TOTAL_WEIGHT"
} 3>&1 | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'" 2>"$ssh_err" \ } 3>&1 | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -u '$destination'" 2>"$ssh_err" \
|| send_status=$? || send_status=$?
if ((send_status != 0)); then if ((send_status != 0)); then
printf '\nTransfer of %s failed (exit %d):\n' "$filesystem" "$send_status" >&2 printf '\nTransfer of %s failed (exit %d):\n' "$filesystem" "$send_status" >&2
cat "$ssh_err" >&2 cat "$ssh_err" >&2
rm -f "$ssh_err" rm -f "$ssh_err"
die "send/receive failed for $filesystem; re-run with --no-ui for plain output" FAILED+=("$filesystem")
prior_weight=$((prior_weight + ${WEIGHT[$filesystem]:-0}))
continue
fi fi
rm -f "$ssh_err" rm -f "$ssh_err"
prior_weight=$((prior_weight + ${WEIGHT[$filesystem]:-0})) prior_weight=$((prior_weight + ${WEIGHT[$filesystem]:-0}))
else else
zfs send -p "$snapshot" | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'" zfs send -p "$snapshot" | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -u '$destination'"
fi fi
fi fi
@@ -248,4 +265,15 @@ for filesystem in "${FILESYSTEMS[@]}"; do
fi fi
done done
if ((${#SKIPPED[@]} > 0)); then
printf 'Skipped %d filesystem(s) that already exist on the destination:\n' "${#SKIPPED[@]}"
printf ' %s\n' "${SKIPPED[@]}"
fi
if ((${#FAILED[@]} > 0)); then
printf 'Finished with %d failed filesystem(s):\n' "${#FAILED[@]}" >&2
printf ' %s\n' "${FAILED[@]}" >&2
exit 1
fi
printf 'Completed. Local source filesystems were not destroyed.\n' printf 'Completed. Local source filesystems were not destroyed.\n'