From 350c783240fdb2f34e943ee71f321ce7b15114c3 Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Sat, 9 Feb 2019 23:07:39 +0100 Subject: backup.in: autodetect ipv4/6 --- backup.in | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/backup.in b/backup.in index b6d87b7..ca4c1ae 100755 --- a/backup.in +++ b/backup.in @@ -1,6 +1,6 @@ #!/bin/bash -rsyncOptions='-6vxcaHAXF' +rsyncOptions='-vxcaHAXF' rsync --help | \ grep -q -- --may-modify-others && \ rsyncOptions="${rsyncOptions} --may-modify-others" @@ -27,6 +27,34 @@ Options: exit $1 } +extract_ssh_host() { + { + printf '%s\n' "$1" + if [ -f ~/.ssh/config ]; then + sed ' + /^Host\s\+'"$1"'$/,/^Host\s/ { + s/^\s*Hostname\s\+// + t + } + d + ' ~/.ssh/config + fi + } \ + | tail -n1 +} + +extract_ssh_ip_protocols() { + getent ahosts "$(extract_ssh_host "$1")" \ + | sed ' + s/^\([0-9]\+\.\)\{3\}[0-9]\+\s\+STREAM\(\s.*\)\?$/4/ + t + s/^[0-9a-f][0-9a-f:]\+\s\+STREAM\(\s.*\)\?$/6/ + t + d + ' \ + | sort -u +} + if [ $# -eq 1 ]; then if [ "$1" == "--help" ]; then usage 0 @@ -57,17 +85,41 @@ QuellIP=$(echo "$3" | sed "s|^[a-zA-Z]*://||; s|^[a-zA-Z]*@||; s|:\?/.*$||") if [ "$#" -eq 3 ]; then Quelle="$3" + ipVer=$( + extract_ssh_ip_protocols "${QuellIP}" \ + | sort -n \ + | tail -n1 + ) elif [ "$#" -eq 4 ]; then sshHopp="$4" lokPort=$[$RANDOM/2+8000] - rsyncShell="-e ssh -6 -p${lokPort} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" - tunnelBefehl="ssh -6 -t -t -L[::1]:${lokPort}:${QuellIP}:22 ${sshHopp}" HoppIP="${sshHopp#*@}" - Quelle="$(echo "$3" | sed "s|${QuellIP}|[::1]|")" + ipVer=$( + { + extract_ssh_ip_protocols "${QuellIP}" + extract_ssh_ip_protocols "${HoppIP}" + } \ + | sort -n \ + | uniq -d \ + | tail -n1 + ) + if [ ${ipVer} -eq 4 ]; then + localAddress='127.0.0.1' + elif [ ${ipVer} -eq 6 ]; then + localAddress='[::1]' + else + >&2 echo 'ip version of hopp and target must be the same' + exit 1 + fi + rsyncShell="-e ssh -${ipVer} -p${lokPort} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" + tunnelBefehl="ssh -${ipVer} -t -t -L${localAddress}:${lokPort}:${QuellIP}:22 ${sshHopp}" + Quelle="$(echo "$3" | sed "s|${QuellIP}|${localAddress}|")" else usage fi +rsyncOptions="${rsyncOptions}${ipVer}" + if [ ! -d ${Basis} ]; then for neededMount in "${neededMounts[@]}"; do if ! mountpoint -q "${neededMount}"; then -- cgit v1.2.3-70-g09d2