This commit is contained in:
2026-10-02 12:45:03 +03:00
parent 6e5f2ddde8
commit cf246d1329
2 changed files with 336 additions and 251 deletions
+75 -1
View File
@@ -134,6 +134,70 @@ remove_remote_snapshot() {
mapfile -t FILESYSTEMS < <(zfs list -H -o name -t filesystem -r "$SOURCE_POOL")
((${#FILESYSTEMS[@]} > 0)) || die "no filesystems found in $SOURCE_POOL"
# Progress UI needs a terminal and a real transfer.
UI=false
if ! $DRY_RUN && [[ -t 1 ]]; then
UI=true
exec 4>&1
fi
# Per-filesystem weights so the overall bar reflects data size, not dataset count.
declare -A WEIGHT=()
TOTAL_WEIGHT=0
if $UI; then
while IFS=$'\t' read -r fs_name fs_weight; do
[[ "$fs_weight" =~ ^[0-9]+$ ]] || fs_weight=0
WEIGHT["$fs_name"]=$fs_weight
TOTAL_WEIGHT=$((TOTAL_WEIGHT + fs_weight))
done < <(zfs list -Hp -o name,logicalreferenced -t filesystem -r "$SOURCE_POOL")
if ((TOTAL_WEIGHT == 0)); then
for fs_name in "${FILESYSTEMS[@]}"; do WEIGHT["$fs_name"]=1; done
TOTAL_WEIGHT=${#FILESYSTEMS[@]}
fi
fi
# Reads `zfs send -P -v` output on stdin and redraws a 3-line block on fd 4.
send_progress() {
local idx="$1" count="$2" name="$3" prior_w="$4" cur_w="$5" total_w="$6"
awk -F'\t' -v idx="$idx" -v count="$count" -v name="$name" \
-v prior_w="$prior_w" -v cur_w="$cur_w" -v total_w="$total_w" '
function human(b, u, i) {
split("B KB MB GB TB PB", u, " "); i = 1
while (b >= 1024 && i < 6) { b /= 1024; i++ }
return sprintf("%.1f%s", b, u[i])
}
function bar(p, n, s, i) {
if (p > 100) p = 100
n = int(p * 30 / 100); s = ""
for (i = 0; i < 30; i++) s = s (i < n ? "#" : "-")
return "[" s "]"
}
function hms(s) { return sprintf("%d:%02d:%02d", s / 3600, (s % 3600) / 60, s % 60) }
function render( pct, op, eta) {
pct = (est > 0) ? b * 100 / est : 0
if (pct > 100) pct = 100
op = (total_w > 0) ? (prior_w + cur_w * pct / 100) * 100 / total_w : 0
eta = (rate > 0 && est > b) ? " ETA " hms((est - b) / rate) : ""
printf "\033[3A\r\033[2K [%d/%d] %s\n", idx, count, name
printf "\033[2K Dataset %s %3d%% %s / %s %s/s%s\n", bar(pct), pct, human(b), human(est), human(rate), eta
printf "\033[2K Overall %s %3d%%\n", bar(op), op
}
BEGIN { printf "\n\n\n" }
$1 == "size" { est = $2 + 0; render(); next }
$1 == "full" || $1 == "incremental" { next }
$1 ~ /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/ {
split($1, t, ":"); s = t[1] * 3600 + t[2] * 60 + t[3]; b = $2 + 0
if (seen && s > ls) { inst = (b - lb) / (s - ls); rate = rate ? 0.7 * rate + 0.3 * inst : inst }
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() }
' >&4 3>&-
}
prior_weight=0
index_now=0
printf 'Migration snapshot: %s\n' "$SNAPSHOT_NAME"
for filesystem in "${FILESYSTEMS[@]}"; do
destination="$(map_destination "$filesystem")"
@@ -151,7 +215,17 @@ for filesystem in "${FILESYSTEMS[@]}"; do
if remote_snapshot_exists "$remote_snapshot"; then
remove_remote_snapshot "$remote_snapshot"
fi
zfs send -p "$snapshot" | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'"
if $UI; then
index_now=$((index_now + 1))
{
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'"
prior_weight=$((prior_weight + ${WEIGHT[$filesystem]:-0}))
else
zfs send -p "$snapshot" | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'"
fi
fi
if $DESTROY_SOURCE_SNAPSHOTS; then