Advertisement
DRVTiny

rsync_dirs

Dec 21st, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.33 KB | None | 0 0
  1. #!/bin/bash
  2. while getopts 'vxf:d:r:' opt; do
  3.         case $opt in
  4.                 f) confDirList=$OPTARG ;;
  5.                 d) remoteHost=$OPTARG ;;
  6.                 r) remoteHost=$OPTARG ;;
  7.                 x) export TRACE=1; set -x ;;
  8.                 v) flBeVerbose=1 ;;
  9.         esac
  10. done
  11. shift $((OPTIND-1))
  12.  
  13. [[ $remoteHost ]] || {
  14.         echo 'You must specify remote host to be synced to as argument of -r key' >&2
  15.         exit 1
  16. }
  17.  
  18. if [[ -t 0 ]]; then
  19.         confDirList=${confDirList:-$1}
  20.         [[ $confDirList && -e $confDirList && -f $confDirList && -r $confDirList ]] || {
  21.                 echo 'You must specify readable file as a first parameter or -f key argument (or do not specify anything at all and pass directory list on STDIN)' >&2
  22.                 exit 2
  23.         }
  24. else
  25.         confDirList='-'
  26. fi
  27.  
  28. shopt -s extglob
  29.  
  30. info_ () {
  31.         [[ $flBeVerbose ]] && echo "$@"
  32. }
  33.  
  34. while read -r d; do
  35.         [[ -d $d ]] || {
  36.                 echo "$d is not a directory, skipping it" >&2
  37.                 continue
  38.         }
  39.         d=${d%%+(/)}
  40.         info_ "Processing <<$d>>"
  41.         { ssh ${remoteHost} "mkdir -p '$d'" && echo 'mkdir OK' >&2; } || echo 'mkdir FAIL' >&2
  42.         rsync -a${flBeVerbose:+v} "${d}/" "${remoteHost}:${d}"
  43. done < <(cat "$confDirList" | sed -r -e '/^\s*(#.*)?$/d' -e 's%\s*(#.*)$%%')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement