fix ssh pwd issue
This commit is contained in:
+37
-8
@@ -38,6 +38,7 @@ RSYNC_EXCLUDES=(
|
|||||||
)
|
)
|
||||||
|
|
||||||
MOUNTED_POINTS=()
|
MOUNTED_POINTS=()
|
||||||
|
SSH_CONTROL_PATHS=()
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
grep '^#' "$0" | sed -n '2,20p' | sed 's/^# \{0,1\}//'
|
grep '^#' "$0" | sed -n '2,20p' | sed 's/^# \{0,1\}//'
|
||||||
@@ -53,6 +54,7 @@ cleanup() {
|
|||||||
umount "$mp" 2>/dev/null || umount -l "$mp" 2>/dev/null || true
|
umount "$mp" 2>/dev/null || umount -l "$mp" 2>/dev/null || true
|
||||||
rmdir "$mp" 2>/dev/null || true
|
rmdir "$mp" 2>/dev/null || true
|
||||||
done
|
done
|
||||||
|
close_ssh_mux
|
||||||
}
|
}
|
||||||
trap cleanup EXIT
|
trap cleanup EXIT
|
||||||
|
|
||||||
@@ -72,12 +74,13 @@ rsync_exclude_args() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
run_rsync() {
|
run_rsync() {
|
||||||
# run_rsync <src> <dst> <logfile>
|
# run_rsync <src> <dst> <logfile> [ssh command]
|
||||||
local src="$1" dst="$2" logfile="$3"
|
local src="$1" dst="$2" logfile="$3" ssh_cmd="${4:-}"
|
||||||
mkdir -p "$dst"
|
mkdir -p "$dst"
|
||||||
local -a excludes=()
|
local -a excludes=()
|
||||||
mapfile -t excludes < <(rsync_exclude_args)
|
mapfile -t excludes < <(rsync_exclude_args)
|
||||||
local -a flags=(-aAXH --partial --info=progress2 "${excludes[@]}")
|
local -a flags=(-aAXH --partial --info=progress2 "${excludes[@]}")
|
||||||
|
[ -n "$ssh_cmd" ] && flags+=(-e "$ssh_cmd")
|
||||||
[ "$DRY_RUN" -eq 1 ] && flags+=(--dry-run)
|
[ "$DRY_RUN" -eq 1 ] && flags+=(--dry-run)
|
||||||
log "rsync: $src -> $dst"
|
log "rsync: $src -> $dst"
|
||||||
rsync "${flags[@]}" "$src" "$dst" 2>&1 | tee -a "$logfile"
|
rsync "${flags[@]}" "$src" "$dst" 2>&1 | tee -a "$logfile"
|
||||||
@@ -85,10 +88,34 @@ run_rsync() {
|
|||||||
|
|
||||||
# ---------- SSH method ----------
|
# ---------- SSH method ----------
|
||||||
|
|
||||||
|
# Shared connection options so every ssh/rsync call for a NAS reuses one
|
||||||
|
# multiplexed master connection (avoids re-prompting for the key passphrase
|
||||||
|
# on every share/directory instead of asking once per NAS).
|
||||||
|
ssh_mux_opts() {
|
||||||
|
local host="$1" user="$2" identity="$3" port="${4:-22}"
|
||||||
|
local ctrl_path="/tmp/.ssh_mux_${user}@${host}:${port}"
|
||||||
|
local -a opts=(
|
||||||
|
-o StrictHostKeyChecking=accept-new -o BatchMode=yes -p "$port"
|
||||||
|
-o ControlMaster=auto -o "ControlPath=${ctrl_path}" -o ControlPersist=10m
|
||||||
|
)
|
||||||
|
[ "$identity" != "-" ] && opts+=(-i "$identity")
|
||||||
|
SSH_CONTROL_PATHS+=("${ctrl_path}|${user}@${host}|${port}")
|
||||||
|
printf '%s\n' "${opts[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
close_ssh_mux() {
|
||||||
|
local entry hostport port ctrl_path
|
||||||
|
for entry in "${SSH_CONTROL_PATHS[@]:-}"; do
|
||||||
|
[ -n "$entry" ] || continue
|
||||||
|
IFS='|' read -r ctrl_path hostport port <<< "$entry"
|
||||||
|
ssh -o ControlPath="$ctrl_path" -O exit "$hostport" -p "$port" 2>/dev/null || true
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
discover_shares_ssh() {
|
discover_shares_ssh() {
|
||||||
local host="$1" user="$2" identity="$3" port="22"
|
local host="$1" user="$2" identity="$3" port="22"
|
||||||
local -a ssh_opts=(-o StrictHostKeyChecking=accept-new -o BatchMode=yes -p "$port")
|
local -a ssh_opts=()
|
||||||
[ "$identity" != "-" ] && ssh_opts+=(-i "$identity")
|
mapfile -t ssh_opts < <(ssh_mux_opts "$host" "$user" "$identity" "$port")
|
||||||
ssh "${ssh_opts[@]}" "${user}@${host}" \
|
ssh "${ssh_opts[@]}" "${user}@${host}" \
|
||||||
'for d in /share/CACHEDEV*_DATA/*/; do b=$(basename "$d"); echo "$b"; done' 2>/dev/null \
|
'for d in /share/CACHEDEV*_DATA/*/; do b=$(basename "$d"); echo "$b"; done' 2>/dev/null \
|
||||||
| sort -u
|
| sort -u
|
||||||
@@ -96,9 +123,11 @@ discover_shares_ssh() {
|
|||||||
|
|
||||||
sync_ssh() {
|
sync_ssh() {
|
||||||
local name="$1" host="$2" user="$3" identity="$4" shares_csv="$5" logdir="$6"
|
local name="$1" host="$2" user="$3" identity="$4" shares_csv="$5" logdir="$6"
|
||||||
local -a ssh_opts=(-o StrictHostKeyChecking=accept-new -p 22)
|
local -a ssh_opts=()
|
||||||
[ "$identity" != "-" ] && ssh_opts+=(-i "$identity")
|
mapfile -t ssh_opts < <(ssh_mux_opts "$host" "$user" "$identity" 22)
|
||||||
local ssh_cmd="ssh ${ssh_opts[*]}"
|
local ssh_cmd
|
||||||
|
printf -v ssh_cmd '%q ' ssh "${ssh_opts[@]}"
|
||||||
|
ssh_cmd="${ssh_cmd% }"
|
||||||
|
|
||||||
local -a shares=()
|
local -a shares=()
|
||||||
if [ "$shares_csv" = "ALL" ]; then
|
if [ "$shares_csv" = "ALL" ]; then
|
||||||
@@ -116,7 +145,7 @@ sync_ssh() {
|
|||||||
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"
|
||||||
run_rsync "${user}@${host}:/share/CACHEDEV*_DATA/${share}/" "${dest}/" "$logfile" \
|
run_rsync "${user}@${host}:/share/CACHEDEV*_DATA/${share}/" "${dest}/" "$logfile" "$ssh_cmd" \
|
||||||
|| log "[$name/$share] rsync reported errors (see $logfile)"
|
|| log "[$name/$share] rsync reported errors (see $logfile)"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user