Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # check color support
  4. colors=$(tput colors)
  5. if (($colors >= 8)); then
  6. red='\033[0;31m'
  7. nocolor='\033[00m'
  8. else
  9. red=
  10. nocolor=
  11. fi
  12.  
  13. NAME=`basename ${BASH_SOURCE[0]}`
  14. DESCRIPTION="Watches for filesystem changes in given path and rsync with remote ssh path"
  15.  
  16. # Define help function
  17. function help(){
  18. if [[ -z "$1" ]]; then echo "${NAME}: ${DESCRIPTION}"; else echo ${NAME}: $1; fi
  19. # echo "${NAME} - ${DESCRIPTION}";
  20. echo "Usage example: ${NAME} -l value -r value -e";
  21. echo "Options:";
  22. echo " -l: LOCAL_PATH. Required. 'Example: /Users/me/folder/'";
  23. echo " -r: REMOTE_SSH_PATH. Required. Example: 'user@ssh.server.address:/remote/server/path'";
  24. echo " -d: WATCH_DELAY. Optional. Example: '2'";
  25. exit 1;
  26. }
  27.  
  28. # set remote + local paths here
  29. LOCAL_PATH="LOCAL_PATH_HERE"
  30. REMOTE_SSH_PATH="REMOTE_SSH_PATH_HERE"
  31.  
  32. while getopts ":l:r:d:h" optname
  33. do
  34. case "$optname" in
  35. "h")
  36. help
  37. ;;
  38. "l")
  39. LOCAL_PATH=$OPTARG
  40. ;;
  41. "r")
  42. REMOTE_SSH_PATH=$OPTARG
  43. ;;
  44. "d")
  45. DELAY=$OPTARG
  46. ;;
  47. "?")
  48. help "Invalid option: -$OPTARG"
  49. ;;
  50. ":")
  51. help "Option -$OPTARG requires an argument."
  52. ;;
  53. *)
  54. help "Unknown error while processing options"
  55. ;;
  56. esac
  57. done
  58.  
  59. shift $((OPTIND-1))
  60.  
  61. # Check required arguments
  62. # if [[ -z "$LOCAL_PATH" ]]; then help "Option -l is required"; fi
  63. # if [[ -z "$REMOTE_SSH_PATH" ]]; then help "Option -r is required"; fi
  64. if [[ -z "$DELAY" ]]; then DELAY=1; fi
  65. echo LOCAL_PATH=\'$LOCAL_PATH\' REMOTE_SSH_PATH=\'$REMOTE_SSH_PATH\' DELAY=\'$DELAY\'
  66.  
  67. # Perform initial complete sync
  68. read -n1 -r -p "Press any key to continue (or abort with Ctrl-C)... " key
  69. echo ""
  70. echo -en "${green}"`date` "${nocolor}"". Synchronizing... "
  71. rsync --filter=':- .gitignore' -rvza --progress --delete --force \
  72. ${LOCAL_PATH} ${REMOTE_SSH_PATH}
  73. echo -e "${green}""Done.""${nocolor}"
  74.  
  75. fswatch -0 -r -l ${DELAY} -e 'datasets/*' -e '.tmp_files' -e'.idea' -e'.git' -e '.*___' . | while read -d "" event
  76. do
  77. echo $event > .tmp_files
  78. echo -en "${green}"`date` "${nocolor}'$event' changed. Synchronizing... "
  79. rsync --filter=':- .gitignore' -rvza -q --delete --force \
  80. --include-from=.tmp_files \
  81. ${LOCAL_PATH} ${REMOTE_SSH_PATH}
  82. echo -e "${green}""Done.""${nocolor}"
  83. rm -rf .tmp_files
  84. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement