diff --git a/migrate.sh b/migrate.sh index f08f51e..06f219f 100644 --- a/migrate.sh +++ b/migrate.sh @@ -40,13 +40,24 @@ RSYNC_EXCLUDES=( MOUNTED_POINTS=() SSH_CONTROL_PATHS=() +UI_ACTIVE=0 +UI_NAMES=() +UI_PID="" +STATUS_DIR="" +MAIN_LOG="/dev/null" +CUR_NAS=""; CUR_SHARE=""; CUR_POS="" + usage() { grep '^#' "$0" | sed -n '2,20p' | sed 's/^# \{0,1\}//' exit 1 } -log() { printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"; } -die() { log "ERROR: $*" >&2; exit 1; } +log() { + 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() { for mp in "${MOUNTED_POINTS[@]:-}"; do @@ -55,6 +66,8 @@ cleanup() { rmdir "$mp" 2>/dev/null || true done close_ssh_mux + [ -n "$UI_PID" ] && kill "$UI_PID" 2>/dev/null || true + [ -n "$STATUS_DIR" ] && rm -rf "$STATUS_DIR" } trap cleanup EXIT @@ -101,45 +114,42 @@ run_rsync() { [ "$DRY_RUN" -eq 1 ] && flags+=(--dry-run) log "rsync: $src -> $dst" - # Plain output when not on a terminal, parallel jobs share the screen, or dry-run. - if [ ! -t 1 ] || [ "$JOBS" -gt 1 ] || [ "$DRY_RUN" -eq 1 ]; then + # Plain output unless the dashboard is running (needs a terminal, no dry-run). + if [ "$UI_ACTIVE" -ne 1 ]; then rsync "${flags[@]}" --info=progress2 "$src" "$dst" 2>&1 | tee -a "$logfile" return fi log "scanning $src ..." + echo scanning > "${STATUS_DIR}/${CUR_NAS}.state" local total_bytes total_bytes="$(rsync "${flags[@]}" --dry-run --stats "$src" "$dst" 2>/dev/null \ | awk -F': ' '/^Total transferred file size/{gsub(/[^0-9]/,"",$2); print $2}')" total_bytes="${total_bytes:-0}" + echo copying > "${STATUS_DIR}/${CUR_NAS}.state" 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() { - local total="$1" logfile="$2" cols - cols="$(tput cols 2>/dev/null || echo 80)" - awk -v total="$total" -v logf="$logfile" -v cols="$cols" ' + local total="$1" logfile="$2" + awk -v total="$total" -v logf="$logfile" -v sf="${STATUS_DIR}/${CUR_NAS}.status" \ + -v share="$CUR_SHARE" -v pos="$CUR_POS" ' function human(b, u, i) { split("B KB MB GB TB", u, " "); i = 1 while (b >= 1024 && i < 5) { b /= 1024; i++ } return sprintf("%.1f%s", b, u[i]) } - function bar(p, w, n, s, i) { - 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) { + function render( tp, tmp, n) { tp = (total > 0) ? (done + curb) * 100 / total : 0 - shown = substr(name, 1, cols - 4) - printf "\033[3A\r\033[2K File %s %3d%% %s\n", bar(cp, 30), cp, rate - printf "\033[2K Total %s %3d%% %s / %s, %d files\n", bar(tp, 30), tp, human(done + curb), human(total), files - printf "\033[2K > %s\n", shown + n = name; gsub(/\|/, "/", n) + tmp = sf ".tmp" + printf "%s|%s|%d|%s|%d|%s|%s|%d|%s\n", share, pos, cp, rate, tp, human(done + curb), human(total), files, n > tmp + 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]+%/ { b = $1; gsub(",", "", b); curb = b + 0; cp = $2 + 0; rate = $3 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 ---------- # 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; } + local _i=0 for share in "${shares[@]}"; do [ "$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 logfile="${logdir}/${name}_${share}.log" mkdir -p "$dest" @@ -256,8 +297,10 @@ sync_smb() { IFS=',' read -r -a shares <<< "$shares_csv" fi + local _i=0 for share in "${shares[@]}"; do [ "$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")" MOUNTED_POINTS+=("$mountpoint") if ! mount -t cifs "//${host}/${share}" "$mountpoint" \ @@ -296,8 +339,10 @@ sync_nfs() { IFS=',' read -r -a shares <<< "$shares_csv" fi + local _i=0 for share in "${shares[@]}"; do [ "$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")" MOUNTED_POINTS+=("$mountpoint") 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" +# 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 +JOB_PIDS=() while IFS= read -r -u 3 line || [ -n "$line" ]; do line="${line%%#*}" line="${line//$'\r'/}" @@ -350,13 +418,16 @@ while IFS= read -r -u 3 line || [ -n "$line" ]; do [ -n "$ONLY_NAME" ] && [ "$ONLY_NAME" != "$name" ] && continue ( + CUR_NAS="$name" case "$method" in ssh) sync_ssh "$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" ;; *) log "[$name] unknown method '$method', skipping" ;; esac + [ "$UI_ACTIVE" -eq 1 ] && echo finished > "${STATUS_DIR}/${name}.state" ) & + JOB_PIDS+=($!) running=$((running + 1)) if [ "$running" -ge "$JOBS" ]; then @@ -365,5 +436,15 @@ while IFS= read -r -u 3 line || [ -n "$line" ]; do fi 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"