Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- LOCAL_BASE=${HOME}
- REMOTE_BASE=${HOME}
- REMOTE_HOST=10.1.2.3
- REMOTE_PORT=22
- REMOTE_USER=${LOGNAME}
- REMOTE_KEY=${HOME}/.ssh/id_desktop
- PUBLIC_HOST=192.0.2.2
- PUBLIC_PORT=22
- PUBLIC_USER=${LOGNAME}
- PUBLIC_KEY=${HOME}/.ssh/id_server
- TUNNEL_HOST=127.0.0.1
- TUNNEL_PORT=2222
- X=''
- X=${X}" .bash_aliases .bash_profile .bashrc .profile"
- X=${X}" .gdbinit .gitconfig .nanorc .screenrc .selected_editor"
- X=${X}" .ssh"
- X=${X}" .config/htop"
- X=${X}" .zsh_aliases* .zsh_env* .zshrc .oh-my-zsh"
- X=${X}" .config/google-chrome*"
- X=${X}" .icedove"
- X=${X}" .mozilla"
- X=${X}" .gnupg .password-store"
- X=${X}" .config/psi+ .cache/psi+ .local/share/psi+"
- X=${X}" .config/sublime-text-3"
- X=${X}" .config/vcsh .gitignore.d"
- X=${X}" .wireshark"
- X=${X}" .tsocks.conf"
- X=${X}" lib"
- X=${X}" prj"
- X=${X}" work"
- RSYNC_PATH=${X}; unset X
- ###
- TIMEOUT=5
- DO_SSH="ssh -x -o ConnectTimeout=${TIMEOUT} -o Compression=no -o EscapeChar=none -o PreferredAuthentications=publickey"
- RSYNC_RSH="${DO_SSH} -o IdentityFile=${REMOTE_KEY}"
- X=''
- X=${X}" --archive --hard-links --specials --one-file-system --update --partial --progress --checksum"
- X=${X}" --modify-window=1"
- X=${X}" --compare-dest=${HOME}/.rsync_tmp"
- X=${X}" --verbose"
- X=${X}" --compress"
- X=${X}" --acls --xattrs"
- RSYNC_OPT=${X}; unset X
- D=/dev/null
- SILENT="0<$D 1>$D 2>$D"; unset D
- DAEMONIZE="${SILENT} &"
- ###
- DIRECTION=
- case "$1" in
- u|U|up) DIRECTION=u ;;
- d|D|down) DIRECTION=d ;;
- *) exit 1 ;;
- esac
- shift
- echo 'probing remote side ...' 1>&2
- eval ${DO_SSH} -o IdentityFile=${REMOTE_KEY} -N -T ${REMOTE_USER}@${REMOTE_HOST} ${DAEMONIZE}
- TUNNEL_PID=$!
- sleep $[TIMEOUT+1]
- if eval ps -p ${TUNNEL_PID} ${SILENT}; then
- echo 'remote side available directly' 1>&2
- REMOTE_BASE=${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_BASE}
- eval kill ${TUNNEL_PID} ${SILENT}
- TUNNEL_PID=''
- else
- echo 'remote side unavailable; opening tunnel' 1>&2
- REMOTE_BASE=${REMOTE_USER}@${TUNNEL_HOST}:${REMOTE_BASE}
- RSYNC_RSH=${RSYNC_RSH}" -p ${TUNNEL_PORT}"
- eval ${DO_SSH} -o IdentityFile=${PUBLIC_KEY} -N -T -L ${TUNNEL_HOST}:${TUNNEL_PORT}:${REMOTE_HOST}:${REMOTE_PORT} ${PUBLIC_USER}@${PUBLIC_HOST} ${DAEMONIZE}
- TUNNEL_PID=$!
- sleep $[TIMEOUT+1]
- if ! eval ps -p ${TUNNEL_PID} ${SILENT}; then
- echo 'tunnel provider is unavailable' 1>&2
- exit 1
- fi
- fi
- Z=
- if [ -z "$*" ]; then
- Z=${RSYNC_PATH[@]}
- else
- Z=$@
- LOCAL_BASE=${PWD}
- fi
- SRC=
- DST=
- case "${DIRECTION}" in
- u)
- SRC=${LOCAL_BASE}
- DST=${REMOTE_BASE}
- ;;
- d)
- SRC=${REMOTE_BASE}
- DST=${LOCAL_BASE}
- ;;
- esac
- ###
- export RSYNC_RSH
- echo 'begin transfer' 1>&2
- echo " \`${SRC}' -> \`${DST}'" 1>&2
- for i in ${Z}; do
- D=$(dirname "$i")
- if [ "$D" = '.' ]
- then D=${DST}
- else D=${DST}/$D
- fi
- echo "$i::" 1>&2
- rsync ${RSYNC_OPT} "${SRC}/$i" "${D}"
- echo 1>&2
- echo 1>&2
- done
- echo 'transfer is done' 1>&2
- if [ -n "${TUNNEL_PID}" ]; then
- echo 'closing tunnel' 1>&2
- eval kill ${TUNNEL_PID} ${SILENT}
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement