diff --git a/scripts/ha/cluster-init.sh b/scripts/ha/cluster-init.sh index f222b8f..b61e593 100755 --- a/scripts/ha/cluster-init.sh +++ b/scripts/ha/cluster-init.sh @@ -240,25 +240,42 @@ log "Forcing $NODE1 to DRBD Primary for initial sync..." drbdadm primary ha-data --force log "Waiting for DRBD initial sync to complete (32 GB may take 10–20 min)..." -_sync_spin=0 +log " (monitor with: watch -n3 cat /proc/drbd)" _sync_chars=('|' '/' '-' $'\\') +_sync_last_pct="" +_sync_iter=0 +_is_tty=false; [[ -t 1 ]] && _is_tty=true while true; do _dstate=$(drbdadm dstate ha-data 2>/dev/null || echo "unknown") if echo "$_dstate" | grep -q "UpToDate/UpToDate"; then - printf "\r%-80s\r" "" # clear progress line + $_is_tty && printf "\r%-80s\r" "" log "DRBD initial sync complete (dstate: $_dstate)" break fi - _pct=$(grep -oE "sync'ed: [0-9.]+%" /proc/drbd 2>/dev/null | grep -oE "[0-9.]+" | head -1 || echo "") - _eta=$(grep -oE "finish: [0-9:]+" /proc/drbd 2>/dev/null | sed 's/finish: //' | head -1 || echo "") - _spd=$(grep -oE "speed: [0-9,]+" /proc/drbd 2>/dev/null | sed 's/speed: //' | head -1 || echo "") - _sc="${_sync_chars[$_sync_spin]}" - _sync_spin=$(( (_sync_spin + 1) % 4 )) + # /proc/drbd uses variable whitespace: "sync'ed: 5.2%" (two spaces). + # Use [[:space:]]+ to tolerate any amount of whitespace after the colon. + _pct=$(grep -oE "sync'ed:[[:space:]]+[0-9.]+" /proc/drbd 2>/dev/null | grep -oE "[0-9.]+" | head -1 || echo "") + _eta=$(grep -oE "finish:[[:space:]]+[0-9:]+" /proc/drbd 2>/dev/null | grep -oE "[0-9:]+$" | head -1 || echo "") + _spd=$(grep -oE "speed:[[:space:]]+[0-9,]+" /proc/drbd 2>/dev/null | grep -oE "[0-9,]+$" | head -1 || echo "") + _sync_iter=$(( _sync_iter + 1 )) + _sc="${_sync_chars[$_sync_iter % 4]}" + if [[ -n "$_pct" ]]; then - printf "\r [%s] syncing: %s%% done — ETA %s @ %s K/s " \ - "$_sc" "$_pct" "${_eta:-??:??:??}" "${_spd:-?}" + _msg=" [${_sc}] syncing: ${_pct}% done — ETA ${_eta:-??:??:??} @ ${_spd:-?} K/s" + if $_is_tty; then + printf "\r%-80s" "$_msg" + elif [[ "$_pct" != "$_sync_last_pct" ]]; then + log "$_msg" + _sync_last_pct="$_pct" + fi else - printf "\r [%s] waiting for sync to start (dstate: %s) " "$_sc" "$_dstate" + # /proc/drbd shows cs:SyncSource but no percent line yet — or something unexpected. + _msg=" [${_sc}] dstate: ${_dstate} — waiting for sync progress in /proc/drbd" + if $_is_tty; then + printf "\r%-80s" "$_msg" + elif (( _sync_iter % 5 == 1 )); then + log "$_msg" + fi fi sleep 3 done