diff --git a/migrate.sh b/migrate.sh index 108255a..487ccb4 100644 --- a/migrate.sh +++ b/migrate.sh @@ -120,36 +120,45 @@ run_rsync() { 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}" + # Size scan runs alongside the transfer so large shares start copying right away. + local totfile="${STATUS_DIR}/${CUR_NAS}.total" scan_pid + rm -f "$totfile" + ( + rsync "${flags[@]}" --dry-run --stats "$src" "$dst" 2>/dev/null "${totfile}.tmp" \ + && mv -f "${totfile}.tmp" "$totfile" + ) & + scan_pid=$! echo copying > "${STATUS_DIR}/${CUR_NAS}.state" - rsync "${flags[@]}" --progress "$src" "$dst" 2>&1 | progress_ui "$total_bytes" "$logfile" + local rc=0 + rsync "${flags[@]}" --progress "$src" "$dst" 2>&1 | progress_ui "$totfile" "$logfile" || rc=$? + kill_tree "$scan_pid" + wait "$scan_pid" 2>/dev/null || true + return "$rc" } # Parses rsync --progress output and writes the NAS status file the dashboard reads. progress_ui() { - local total="$1" logfile="$2" - awk -v total="$total" -v logf="$logfile" -v sf="${STATUS_DIR}/${CUR_NAS}.status" \ + local totfile="$1" logfile="$2" + awk -v totfile="$totfile" -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 render( tp, tmp, n) { + function render( tp, tmp, n, line) { + if (total == 0 && (getline line < totfile) > 0) total = line + 0 + close(totfile) tp = (total > 0) ? (done + curb) * 100 / total : 0 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 + printf "%s|%s|%d|%s|%d|%s|%s|%d|%s\n", share, pos, cp, rate, tp, human(done + curb), (total > 0 ? human(total) : "sizing..."), files, n > tmp close(tmp) system("mv -f \047" tmp "\047 \047" sf "\047") } - BEGIN { RS = "[\r\n]" } + BEGIN { RS = "[\r\n]"; render() } /^[ \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 } @@ -157,7 +166,7 @@ progress_ui() { } /^(sending|building|receiving|sent |total size|created)/ || $0 == "" { next } { name = $0; print name >> logf; counted = 0; curb = 0; cp = 0; files++; render() } - END { if (total > 0) { done = total; curb = 0 } cp = 100; render() } + END { if (total == 0 && (getline line < totfile) > 0) total = line + 0; if (total > 0) { done = total; curb = 0 } cp = 100; render() } ' }