ADD ui for estimates of migration

This commit is contained in:
2026-10-02 12:33:45 +03:00
parent 17fbf98411
commit a013ec6131
+102 -21
View File
@@ -40,13 +40,24 @@ RSYNC_EXCLUDES=(
MOUNTED_POINTS=() MOUNTED_POINTS=()
SSH_CONTROL_PATHS=() SSH_CONTROL_PATHS=()
UI_ACTIVE=0
UI_NAMES=()
UI_PID=""
STATUS_DIR=""
MAIN_LOG="/dev/null"
CUR_NAS=""; CUR_SHARE=""; CUR_POS=""
usage() { usage() {
grep '^#' "$0" | sed -n '2,20p' | sed 's/^# \{0,1\}//' grep '^#' "$0" | sed -n '2,20p' | sed 's/^# \{0,1\}//'
exit 1 exit 1
} }
log() { printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"; } log() {
die() { log "ERROR: $*" >&2; exit 1; } local m
m="$(printf '[%s] %s' "$(date '+%Y-%m-%d %H:%M:%S')" "$*")"
if [ "$UI_ACTIVE" -eq 1 ]; then echo "$m" >> "$MAIN_LOG"; else echo "$m"; fi
}
die() { UI_ACTIVE=0; log "ERROR: $*" >&2; exit 1; }
cleanup() { cleanup() {
for mp in "${MOUNTED_POINTS[@]:-}"; do for mp in "${MOUNTED_POINTS[@]:-}"; do
@@ -55,6 +66,8 @@ cleanup() {
rmdir "$mp" 2>/dev/null || true rmdir "$mp" 2>/dev/null || true
done done
close_ssh_mux close_ssh_mux
[ -n "$UI_PID" ] && kill "$UI_PID" 2>/dev/null || true
[ -n "$STATUS_DIR" ] && rm -rf "$STATUS_DIR"
} }
trap cleanup EXIT trap cleanup EXIT
@@ -101,45 +114,42 @@ run_rsync() {
[ "$DRY_RUN" -eq 1 ] && flags+=(--dry-run) [ "$DRY_RUN" -eq 1 ] && flags+=(--dry-run)
log "rsync: $src -> $dst" log "rsync: $src -> $dst"
# Plain output when not on a terminal, parallel jobs share the screen, or dry-run. # Plain output unless the dashboard is running (needs a terminal, no dry-run).
if [ ! -t 1 ] || [ "$JOBS" -gt 1 ] || [ "$DRY_RUN" -eq 1 ]; then if [ "$UI_ACTIVE" -ne 1 ]; then
rsync "${flags[@]}" --info=progress2 "$src" "$dst" 2>&1 | tee -a "$logfile" rsync "${flags[@]}" --info=progress2 "$src" "$dst" 2>&1 | tee -a "$logfile"
return return
fi fi
log "scanning $src ..." log "scanning $src ..."
echo scanning > "${STATUS_DIR}/${CUR_NAS}.state"
local total_bytes local total_bytes
total_bytes="$(rsync "${flags[@]}" --dry-run --stats "$src" "$dst" 2>/dev/null \ total_bytes="$(rsync "${flags[@]}" --dry-run --stats "$src" "$dst" 2>/dev/null \
| awk -F': ' '/^Total transferred file size/{gsub(/[^0-9]/,"",$2); print $2}')" | awk -F': ' '/^Total transferred file size/{gsub(/[^0-9]/,"",$2); print $2}')"
total_bytes="${total_bytes:-0}" total_bytes="${total_bytes:-0}"
echo copying > "${STATUS_DIR}/${CUR_NAS}.state"
rsync "${flags[@]}" --progress "$src" "$dst" 2>&1 | progress_ui "$total_bytes" "$logfile" rsync "${flags[@]}" --progress "$src" "$dst" 2>&1 | progress_ui "$total_bytes" "$logfile"
} }
# Reads rsync --progress output; draws current-file bar, total bar and the rolling filename. # Parses rsync --progress output and writes the NAS status file the dashboard reads.
progress_ui() { progress_ui() {
local total="$1" logfile="$2" cols local total="$1" logfile="$2"
cols="$(tput cols 2>/dev/null || echo 80)" awk -v total="$total" -v logf="$logfile" -v sf="${STATUS_DIR}/${CUR_NAS}.status" \
awk -v total="$total" -v logf="$logfile" -v cols="$cols" ' -v share="$CUR_SHARE" -v pos="$CUR_POS" '
function human(b, u, i) { function human(b, u, i) {
split("B KB MB GB TB", u, " "); i = 1 split("B KB MB GB TB", u, " "); i = 1
while (b >= 1024 && i < 5) { b /= 1024; i++ } while (b >= 1024 && i < 5) { b /= 1024; i++ }
return sprintf("%.1f%s", b, u[i]) return sprintf("%.1f%s", b, u[i])
} }
function bar(p, w, n, s, i) { function render( tp, tmp, n) {
if (p > 100) p = 100
n = int(p * w / 100); s = ""
for (i = 0; i < w; i++) s = s (i < n ? "#" : "-")
return "[" s "]"
}
function render( tp, shown) {
tp = (total > 0) ? (done + curb) * 100 / total : 0 tp = (total > 0) ? (done + curb) * 100 / total : 0
shown = substr(name, 1, cols - 4) n = name; gsub(/\|/, "/", n)
printf "\033[3A\r\033[2K File %s %3d%% %s\n", bar(cp, 30), cp, rate tmp = sf ".tmp"
printf "\033[2K Total %s %3d%% %s / %s, %d files\n", bar(tp, 30), tp, human(done + curb), human(total), files printf "%s|%s|%d|%s|%d|%s|%s|%d|%s\n", share, pos, cp, rate, tp, human(done + curb), human(total), files, n > tmp
printf "\033[2K > %s\n", shown close(tmp)
system("mv -f \047" tmp "\047 \047" sf "\047")
} }
BEGIN { RS = "[\r\n]"; printf "\n\n\n" } BEGIN { RS = "[\r\n]" }
/^[ \t]+[0-9,]+[ \t]+[0-9]+%/ { /^[ \t]+[0-9,]+[ \t]+[0-9]+%/ {
b = $1; gsub(",", "", b); curb = b + 0; cp = $2 + 0; rate = $3 b = $1; gsub(",", "", b); curb = b + 0; cp = $2 + 0; rate = $3
if (cp >= 100 && !counted) { done += curb; counted = 1; curb = 0 } if (cp >= 100 && !counted) { done += curb; counted = 1; curb = 0 }
@@ -151,6 +161,35 @@ progress_ui() {
' '
} }
ui_bar() {
local p="$1" w=30 n i s=""
[ "$p" -gt 100 ] && p=100
n=$((p * w / 100))
for ((i = 0; i < w; i++)); do [ "$i" -lt "$n" ] && s+="#" || s+="-"; done
printf '[%s]' "$s"
}
# Draws one 4-line block per NAS, overwriting the previous frame.
render_dashboard() {
local nm share pos cp rate tp dn total files fname st cols
cols="$(tput cols 2>/dev/null || echo 80)"
printf '\033[%dA' "$(( ${#UI_NAMES[@]} * 4 ))"
for nm in "${UI_NAMES[@]}"; do
share="-"; pos=""; cp=0; rate=""; tp=0; dn="0.0B"; total="0.0B"; files=0; fname=""; st="waiting"
[ -f "${STATUS_DIR}/${nm}.status" ] \
&& { IFS='|' read -r share pos cp rate tp dn total files fname < "${STATUS_DIR}/${nm}.status" || true; }
[ -f "${STATUS_DIR}/${nm}.state" ] && st="$(cat "${STATUS_DIR}/${nm}.state")"
printf '\033[2K\r\033[1m%s\033[0m share %s %s (%s)\n' "$nm" "$share" "$pos" "$st"
printf '\033[2K File %s %3d%% %s\n' "$(ui_bar "${cp:-0}")" "${cp:-0}" "$rate"
printf '\033[2K Total %s %3d%% %s / %s, %s files\n' "$(ui_bar "${tp:-0}")" "${tp:-0}" "$dn" "$total" "${files:-0}"
printf '\033[2K > %s\n' "${fname:0:$((cols - 6))}"
done
}
dashboard_loop() {
while :; do render_dashboard; sleep 1; done
}
# ---------- SSH method ---------- # ---------- SSH method ----------
# Shared connection options so every ssh/rsync call for a NAS reuses one # Shared connection options so every ssh/rsync call for a NAS reuses one
@@ -205,8 +244,10 @@ sync_ssh() {
[ "${#shares[@]}" -eq 0 ] && { log "[$name] no shares found/selected"; return; } [ "${#shares[@]}" -eq 0 ] && { log "[$name] no shares found/selected"; return; }
local _i=0
for share in "${shares[@]}"; do for share in "${shares[@]}"; do
[ "$LIST_ONLY" -eq 1 ] && { log "[$name] share: $share"; continue; } [ "$LIST_ONLY" -eq 1 ] && { log "[$name] share: $share"; continue; }
_i=$((_i + 1)); CUR_SHARE="$share"; CUR_POS="${_i}/${#shares[@]}"
local dest="${DEST_ROOT}/${name}/${share}" local dest="${DEST_ROOT}/${name}/${share}"
local logfile="${logdir}/${name}_${share}.log" local logfile="${logdir}/${name}_${share}.log"
mkdir -p "$dest" mkdir -p "$dest"
@@ -256,8 +297,10 @@ sync_smb() {
IFS=',' read -r -a shares <<< "$shares_csv" IFS=',' read -r -a shares <<< "$shares_csv"
fi fi
local _i=0
for share in "${shares[@]}"; do for share in "${shares[@]}"; do
[ "$LIST_ONLY" -eq 1 ] && { log "[$name] share: $share"; continue; } [ "$LIST_ONLY" -eq 1 ] && { log "[$name] share: $share"; continue; }
_i=$((_i + 1)); CUR_SHARE="$share"; CUR_POS="${_i}/${#shares[@]}"
local mountpoint; mountpoint="$(mktemp -d "/mnt/.qnap_${name}_XXXXXX")" local mountpoint; mountpoint="$(mktemp -d "/mnt/.qnap_${name}_XXXXXX")"
MOUNTED_POINTS+=("$mountpoint") MOUNTED_POINTS+=("$mountpoint")
if ! mount -t cifs "//${host}/${share}" "$mountpoint" \ if ! mount -t cifs "//${host}/${share}" "$mountpoint" \
@@ -296,8 +339,10 @@ sync_nfs() {
IFS=',' read -r -a shares <<< "$shares_csv" IFS=',' read -r -a shares <<< "$shares_csv"
fi fi
local _i=0
for share in "${shares[@]}"; do for share in "${shares[@]}"; do
[ "$LIST_ONLY" -eq 1 ] && { log "[$name] share: $share"; continue; } [ "$LIST_ONLY" -eq 1 ] && { log "[$name] share: $share"; continue; }
_i=$((_i + 1)); CUR_SHARE="$share"; CUR_POS="${_i}/${#shares[@]}"
local mountpoint; mountpoint="$(mktemp -d "/mnt/.qnap_${name}_XXXXXX")" local mountpoint; mountpoint="$(mktemp -d "/mnt/.qnap_${name}_XXXXXX")"
MOUNTED_POINTS+=("$mountpoint") MOUNTED_POINTS+=("$mountpoint")
if ! mount -t nfs -o ro "${host}:/share/${share}" "$mountpoint" 2>/dev/null \ if ! mount -t nfs -o ro "${host}:/share/${share}" "$mountpoint" 2>/dev/null \
@@ -340,7 +385,30 @@ mkdir -p "$LOG_DIR" "$DEST_ROOT"
log "Starting migration. dest=$DEST_ROOT dry_run=$DRY_RUN list_only=$LIST_ONLY" log "Starting migration. dest=$DEST_ROOT dry_run=$DRY_RUN list_only=$LIST_ONLY"
# Dashboard needs a terminal, no dry/list run, and no interactive SMB password prompt.
if [ -t 1 ] && [ "$DRY_RUN" -eq 0 ] && [ "$LIST_ONLY" -eq 0 ]; then
ui_ok=1
while IFS= read -r -u 4 line || [ -n "$line" ]; do
line="${line%%#*}"; line="${line//$'\r'/}"
[ -z "${line// /}" ] && continue
read -r name method host user auth shares <<< "$line"
[ -n "$name" ] || continue
[ -n "$ONLY_NAME" ] && [ "$ONLY_NAME" != "$name" ] && continue
[ "$method" = "smb" ] && [ "$auth" = "-" ] && ui_ok=0
UI_NAMES+=("$name")
done 4< "$CONFIG_FILE"
if [ "$ui_ok" -eq 1 ] && [ "${#UI_NAMES[@]}" -gt 0 ]; then
STATUS_DIR="$(mktemp -d)"
MAIN_LOG="${LOG_DIR}/migrate.log"
UI_ACTIVE=1
for _ in "${UI_NAMES[@]}"; do printf '\n\n\n\n'; done
dashboard_loop &
UI_PID=$!
fi
fi
running=0 running=0
JOB_PIDS=()
while IFS= read -r -u 3 line || [ -n "$line" ]; do while IFS= read -r -u 3 line || [ -n "$line" ]; do
line="${line%%#*}" line="${line%%#*}"
line="${line//$'\r'/}" line="${line//$'\r'/}"
@@ -350,13 +418,16 @@ while IFS= read -r -u 3 line || [ -n "$line" ]; do
[ -n "$ONLY_NAME" ] && [ "$ONLY_NAME" != "$name" ] && continue [ -n "$ONLY_NAME" ] && [ "$ONLY_NAME" != "$name" ] && continue
( (
CUR_NAS="$name"
case "$method" in case "$method" in
ssh) sync_ssh "$name" "$host" "$user" "$auth" "$shares" "$LOG_DIR" ;; ssh) sync_ssh "$name" "$host" "$user" "$auth" "$shares" "$LOG_DIR" ;;
smb) sync_smb "$name" "$host" "$user" "$auth" "$shares" "$LOG_DIR" ;; smb) sync_smb "$name" "$host" "$user" "$auth" "$shares" "$LOG_DIR" ;;
nfs) sync_nfs "$name" "$host" "$shares" "$LOG_DIR" ;; nfs) sync_nfs "$name" "$host" "$shares" "$LOG_DIR" ;;
*) log "[$name] unknown method '$method', skipping" ;; *) log "[$name] unknown method '$method', skipping" ;;
esac esac
[ "$UI_ACTIVE" -eq 1 ] && echo finished > "${STATUS_DIR}/${name}.state"
) & ) &
JOB_PIDS+=($!)
running=$((running + 1)) running=$((running + 1))
if [ "$running" -ge "$JOBS" ]; then if [ "$running" -ge "$JOBS" ]; then
@@ -365,5 +436,15 @@ while IFS= read -r -u 3 line || [ -n "$line" ]; do
fi fi
done 3< "$CONFIG_FILE" done 3< "$CONFIG_FILE"
wait for pid in "${JOB_PIDS[@]:-}"; do
[ -n "$pid" ] && { wait "$pid" || true; }
done
if [ "$UI_ACTIVE" -eq 1 ]; then
kill "$UI_PID" 2>/dev/null || true
wait "$UI_PID" 2>/dev/null || true
UI_PID=""
render_dashboard
UI_ACTIVE=0
fi
log "Migration complete. Logs in $LOG_DIR" log "Migration complete. Logs in $LOG_DIR"