From 746f634a90fc9a81d2159b6f6c0b62970a60b289 Mon Sep 17 00:00:00 2001 From: Tero Huttunen Date: Fri, 2 Oct 2026 13:36:42 +0300 Subject: [PATCH] Dir handling fixes --- README.md | 13 ++++++++----- zfs-move-over-ssh.sh | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index be5d416..3a3b8bd 100644 --- a/README.md +++ b/README.md @@ -237,11 +237,14 @@ Options: - 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 verification, or use `--destroy-source-snapshots` once the workflow is trusted. -- The remote command is `zfs receive -uF`. `-u` leaves received filesystems - unmounted. `-F` can roll back the destination filesystem and destroy newer - snapshots or changes that conflict with the incoming snapshot. Do not point - the script at a destination containing data you need unless you have reviewed - the dry-run output and have a current backup. +- The remote command is `zfs receive -u` (no `-F`). `-u` leaves received + filesystems unmounted. Filesystems that already exist on the destination + (including the destination pool root) are skipped and never modified or + rolled back; existing data on the new server is not deleted. To keep paths + 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, completed destination filesystems remain received. Re-running with the same source can create another timestamped snapshot; inspect both source and diff --git a/zfs-move-over-ssh.sh b/zfs-move-over-ssh.sh index 35a77f8..4d918af 100644 --- a/zfs-move-over-ssh.sh +++ b/zfs-move-over-ssh.sh @@ -196,50 +196,67 @@ send_progress() { ls = s; lb = b; seen = 1; render(); next } { printf "\033[3A\033[J%s\n\n\n\n", $0 } - END { if (est > 0) b = est; render() } + END { render() } ' >&4 3>&- } prior_weight=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" for filesystem in "${FILESYSTEMS[@]}"; do destination="$(map_destination "$filesystem")" snapshot="${filesystem}@${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" run zfs snapshot "$snapshot" if $DRY_RUN; then 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 else if remote_snapshot_exists "$remote_snapshot"; then remove_remote_snapshot "$remote_snapshot" fi if $UI; then - index_now=$((index_now + 1)) ssh_err="$(mktemp)" send_status=0 { zfs send -p -P -v "$snapshot" 2>&1 1>&3 3>&- \ | send_progress "$index_now" "${#FILESYSTEMS[@]}" "$filesystem" \ "$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=$? if ((send_status != 0)); then printf '\nTransfer of %s failed (exit %d):\n' "$filesystem" "$send_status" >&2 cat "$ssh_err" >&2 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 rm -f "$ssh_err" prior_weight=$((prior_weight + ${WEIGHT[$filesystem]:-0})) 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 @@ -248,4 +265,15 @@ for filesystem in "${FILESYSTEMS[@]}"; do fi 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' \ No newline at end of file