Advertisement
rockdrilla

sophisticated rsync wrapper

May 27th, 2015
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.13 KB | None | 0 0
  1. #!/bin/bash
  2. LOCAL_BASE=${HOME}
  3. REMOTE_BASE=${HOME}
  4.  
  5. REMOTE_HOST=10.1.2.3
  6. REMOTE_PORT=22
  7. REMOTE_USER=${LOGNAME}
  8. REMOTE_KEY=${HOME}/.ssh/id_desktop
  9.  
  10. PUBLIC_HOST=192.0.2.2
  11. PUBLIC_PORT=22
  12. PUBLIC_USER=${LOGNAME}
  13. PUBLIC_KEY=${HOME}/.ssh/id_server
  14.  
  15. TUNNEL_HOST=127.0.0.1
  16. TUNNEL_PORT=2222
  17.  
  18. X=''
  19.     X=${X}" .bash_aliases .bash_profile .bashrc .profile"
  20.     X=${X}" .gdbinit .gitconfig .nanorc .screenrc .selected_editor"
  21.     X=${X}" .ssh"
  22.     X=${X}" .config/htop"
  23.     X=${X}" .zsh_aliases* .zsh_env* .zshrc .oh-my-zsh"
  24.     X=${X}" .config/google-chrome*"
  25.     X=${X}" .icedove"
  26.     X=${X}" .mozilla"
  27.     X=${X}" .gnupg .password-store"
  28.     X=${X}" .config/psi+ .cache/psi+ .local/share/psi+"
  29.     X=${X}" .config/sublime-text-3"
  30.     X=${X}" .config/vcsh .gitignore.d"
  31.     X=${X}" .wireshark"
  32.     X=${X}" .tsocks.conf"
  33.     X=${X}" lib"
  34.     X=${X}" prj"
  35.     X=${X}" work"
  36. RSYNC_PATH=${X}; unset X
  37.  
  38. ###
  39.  
  40. TIMEOUT=5
  41.  
  42. DO_SSH="ssh -x -o ConnectTimeout=${TIMEOUT} -o Compression=no -o EscapeChar=none -o PreferredAuthentications=publickey"
  43. RSYNC_RSH="${DO_SSH} -o IdentityFile=${REMOTE_KEY}"
  44.  
  45. X=''
  46.     X=${X}" --archive --hard-links --specials --one-file-system --update --partial --progress --checksum"
  47.     X=${X}" --modify-window=1"
  48.     X=${X}" --compare-dest=${HOME}/.rsync_tmp"
  49.     X=${X}" --verbose"
  50.     X=${X}" --compress"
  51.     X=${X}" --acls --xattrs"
  52. RSYNC_OPT=${X}; unset X
  53.  
  54. D=/dev/null
  55. SILENT="0<$D 1>$D 2>$D"; unset D
  56. DAEMONIZE="${SILENT} &"
  57.  
  58. ###
  59.  
  60. DIRECTION=
  61. case "$1" in
  62. u|U|up)   DIRECTION=u ;;
  63. d|D|down) DIRECTION=d ;;
  64. *) exit 1 ;;
  65. esac
  66. shift
  67.  
  68. echo 'probing remote side ...' 1>&2
  69. eval ${DO_SSH} -o IdentityFile=${REMOTE_KEY} -N -T ${REMOTE_USER}@${REMOTE_HOST} ${DAEMONIZE}
  70. TUNNEL_PID=$!
  71. sleep $[TIMEOUT+1]
  72.  
  73. if eval ps -p ${TUNNEL_PID} ${SILENT}; then
  74.     echo 'remote side available directly' 1>&2
  75.     REMOTE_BASE=${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_BASE}
  76.  
  77.     eval kill ${TUNNEL_PID} ${SILENT}
  78.     TUNNEL_PID=''
  79. else
  80.     echo 'remote side unavailable; opening tunnel' 1>&2
  81.     REMOTE_BASE=${REMOTE_USER}@${TUNNEL_HOST}:${REMOTE_BASE}
  82.  
  83.     RSYNC_RSH=${RSYNC_RSH}" -p ${TUNNEL_PORT}"
  84.  
  85.     eval ${DO_SSH} -o IdentityFile=${PUBLIC_KEY} -N -T -L ${TUNNEL_HOST}:${TUNNEL_PORT}:${REMOTE_HOST}:${REMOTE_PORT} ${PUBLIC_USER}@${PUBLIC_HOST} ${DAEMONIZE}
  86.     TUNNEL_PID=$!
  87.     sleep $[TIMEOUT+1]
  88.  
  89.     if ! eval ps -p ${TUNNEL_PID} ${SILENT}; then
  90.         echo 'tunnel provider is unavailable' 1>&2
  91.         exit 1
  92.     fi
  93. fi
  94.  
  95. Z=
  96. if [ -z "$*" ]; then
  97.     Z=${RSYNC_PATH[@]}
  98. else
  99.     Z=$@
  100.     LOCAL_BASE=${PWD}
  101. fi
  102.  
  103. SRC=
  104. DST=
  105. case "${DIRECTION}" in
  106. u)
  107.     SRC=${LOCAL_BASE}
  108.     DST=${REMOTE_BASE}
  109. ;;
  110. d)
  111.     SRC=${REMOTE_BASE}
  112.     DST=${LOCAL_BASE}
  113. ;;
  114. esac
  115.  
  116. ###
  117.  
  118. export RSYNC_RSH
  119.  
  120. echo 'begin transfer' 1>&2
  121. echo " \`${SRC}' -> \`${DST}'" 1>&2
  122.  
  123. for i in ${Z}; do
  124.     D=$(dirname "$i")
  125.     if [ "$D" = '.' ]
  126.     then D=${DST}
  127.     else D=${DST}/$D
  128.     fi
  129.  
  130.     echo "$i::" 1>&2
  131.     rsync ${RSYNC_OPT} "${SRC}/$i" "${D}"
  132.     echo 1>&2
  133.     echo 1>&2
  134. done
  135.  
  136. echo 'transfer is done' 1>&2
  137.  
  138. if [ -n "${TUNNEL_PID}" ]; then
  139.     echo 'closing tunnel' 1>&2
  140.     eval kill ${TUNNEL_PID} ${SILENT}
  141. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement