Advertisement
zefie

bsync (custom script for rclone)

Jan 9th, 2019
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.62 KB | None | 0 0
  1. #!/bin/bash
  2. # Use --dry-run as third argument to pass to rclone
  3. for f in 'd' 'dry' 'dry-run' 'echo'; do
  4.         if [ "${1}" == "--${f}" ]; then
  5.                 Z_DRY=1
  6.                 shift && break;
  7.         fi
  8. done
  9.  
  10. # set flag to sync FROM bucket instead of TO
  11. for f in 'rev' 'reverse' 'down' 'r'; do
  12.         if [ "${1}" == "--${f}" ]; then
  13.                 Z_REV=1
  14.                 shift && break;
  15.         fi
  16. done
  17.  
  18. # set share
  19. Z_SHR="${1}"
  20. shift
  21.  
  22. # set path
  23. Z_PTH="${1}"
  24. shift
  25.  
  26. # rcloud bucket profile
  27. Z_HST="midnight"
  28. # local src directory
  29. Z_SRC="/media/local"
  30. # local dir = local src + share
  31. Z_SRD="${Z_SRC}/${Z_SHR}"
  32. # share name to lowercase = bucket name
  33. Z_BUK="$(echo "${Z_SHR}" | tr '[:upper:]' '[:lower:]')"
  34.  
  35. # allow no path properly
  36. if [ "${Z_PTH}" == "/" ]; then
  37.         Z_PTH=""
  38.         Z_NOP=1
  39. fi
  40.  
  41. # bucket path = share path (without source dir)
  42. Z_LPT="${Z_PTH}"
  43. Z_OLP="${Z_LPT}"
  44. # rclone (defined so we can echo instead when --echo used)
  45. Z_CMD="rclone"
  46. # rcloud command
  47. Z_RCC="sync"
  48. # default rclone options
  49. Z_OPT=(
  50.         '-P'
  51.         '--delete-before'
  52.         '--delete-excluded'
  53. );
  54.  
  55. # echo only
  56. if [ ! -z "${Z_DRY}" ]; then
  57.         Z_CMD="echo ${Z_CMD}"
  58. fi
  59.  
  60. # convenience handler to add option
  61. function opt_add() {
  62.         Z_OPT+=("${@}")
  63. }
  64.  
  65. # convenience handler to remove option (by name)
  66. function opt_remove() {
  67.         delete=("${@}")
  68.         for target in "${delete[@]}"; do
  69.                 for i in "${!Z_OPT[@]}"; do
  70.                         if [[ ${Z_OPT[i]} = "${delete[0]}" ]]; then
  71.                                 unset 'Z_OPT[i]'
  72.                         fi
  73.                 done
  74.         done
  75. }
  76.  
  77. # not really needed but keeping with the functions, sets rclone command
  78. function opt_set_command() {
  79.         Z_RCC = "${1}"
  80. }
  81.  
  82. # convenience wrapper to use convenience handler to remove multiple options
  83. function opt_set_no_delete() {
  84.         opt_remove '--delete-before'
  85.         opt_remove '--delete-after'
  86.         opt_remove '--delete-during'
  87.         opt_remove '--delete-excluded'
  88.         opt_add '--max-delete 0'
  89. }
  90.  
  91. # convenience wrapper to use convenience hander add an exclude option
  92. function opt_add_exclude() {
  93.         # dont quote the argument or it wont work ¯\_(ツ)_/¯
  94.         opt_add "--exclude ${1}"
  95. }
  96.  
  97. # convenience wrapper to use another convenience wrapper to use convenience handler to exclude a directory ;)
  98. function opt_add_exclude_directory() {
  99.         opt_add_exclude "/${1}/**"
  100. }
  101.  
  102. # check if original path starts with ${1}
  103. function check_path() {
  104.         echo -n "${Z_OLP}" | grep -Pc "^${1}"
  105. }
  106.  
  107. function set_custom_path() {
  108.         # redirect structure to another bucket
  109.         # /media/local/Console -> midnight:software/Console
  110.         if [ "${Z_SHR}" == "Console" ]; then
  111.                 # override bucket name
  112.                 Z_BUK="software"
  113.                 # override source directory
  114.                 Z_SRD="${Z_SRC}/Console"
  115.                 # override local path
  116.                 Z_LPT="${Z_PTH}"
  117.                 # override bucket-side path
  118.                 Z_PTH="Console/${Z_PTH}"
  119.  
  120.                 # /media/local/Console/PS3/archive -> midnight:software/Console/PS3
  121.                 if [ $(check_path "PS3") -eq 1 ]; then
  122.                         Z_LPT="PS3/archive"
  123.                         if [ $(check_path "PS3/ISO") -eq 1 ]; then
  124.                                 # don't delete
  125.                                 opt_set_no_delete
  126.                                 opt_set_command 'copyto'
  127.                                 # lower system stress by limiting to 1 transfer/hasher
  128.                                         opt_add '--transfers 1' '--checkers 1'
  129.                         fi
  130.                         if [ $(check_path 'PS3(/)?$') -eq 1 ]; then
  131.                                 # root, so exclude ISO (see above)
  132.                                 opt_add_exclude_directory "ISO"
  133.                                 # don't delete excluded files
  134.                                 opt_remove '--delete-excluded'
  135.                         else
  136.                                 # override local path, retaining extra path info
  137.                                 Z_TMP=$(echo "${Z_OLP}" | cut -d'/' -f2-)
  138.                                 if [ ! -z "${Z_TMP}" ]; then
  139.                                         Z_LPT="${Z_LPT}/${Z_TMP}"
  140.                                 fi
  141.                         fi
  142.                 fi
  143.         fi
  144.         if [ "${Z_SHR}" == "Backups" ] && [ $(check_path 'Videos') -eq 1 ]; then
  145.                 # override bucket name
  146.                 Z_BUK="backups"
  147.                 # override source directory
  148.                 Z_SRD="${Z_SRC}"
  149.                 # override local path
  150.                 Z_LPT="${Z_PTH}"
  151.                 # override bucket-side path
  152.                 Z_PTH="Media/${Z_PTH}"
  153.         fi
  154.  
  155.         if [ "${Z_SHR}" == "Backups" ] && [ $(check_path 'Hosting') -eq 1 ]; then
  156.                 # lower system stress by limiting to 4 transfers, 2 hashers
  157.                 opt_add '--transfers 4' '--checkers 2'
  158.         fi
  159.  
  160.         if [ "${Z_SHR}" == "YouTube" ]; then
  161.                 opt_add_exclude_directory '.tmp'
  162.                 opt_add_exclude_directory 'Web/cache'
  163.         fi
  164. }
  165.  
  166. function do_bsync() {
  167.         if [ -z "${Z_PTH}" ] && [ -z "${Z_NOP}" ] || [ -z "${Z_BUK}" ]; then
  168.                 # fail if arguements not provided
  169.                 echo "Usage: ${0} [bucket] [path]"
  170.         else
  171.                 # process path overrides (see function above)
  172.                 set_custom_path
  173.  
  174.                 # make directory on bucket (doesn't fail if exists)
  175.                 ${Z_CMD} mkdir "${Z_HST}:${Z_BUK}/${Z_PTH}"
  176.                 Z_RES=$?
  177.                 if [ ! -d "${Z_SRD}/${Z_LPT}" ]; then
  178.                         echo "Could not find source directory: ${Z_SRD}/${Z_LPT}";
  179.                         exit 1;
  180.                 fi
  181.                 if [ ${Z_RES} -ne 0 ]; then
  182.                         echo "Could not create directory ${Z_HST}:${Z_BUK}/${Z_PTH}. Does the bucket exist?"
  183.                         exit ${Z_RES};
  184.                 fi
  185.                 # do the magic sync
  186.                 if [ -z ${Z_REV} ]; then
  187.                         # to bucket
  188.                         ${Z_CMD} "${Z_RCC}" "${Z_SRD}/${Z_LPT}" "${Z_HST}:${Z_BUK}/${Z_PTH}" "${Z_OPT[@]}" "${@}"
  189.                 else
  190.                         # from bucket
  191.                         ${Z_CMD} "${Z_RCC}" "${Z_HST}:${Z_BUK}/${Z_PTH}" "${Z_SRD}/${Z_LPT}" "${Z_OPT[@]}" "${@}"
  192.                 fi
  193.         fi
  194. }
  195.  
  196. if [ -z "${Z_INCLUDED}" ]; then
  197.         # if Z_INCLUDED is not defined, assume we are not included for our functions
  198.         do_bsync "${@}"
  199. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement