Guest User

Untitled

a guest
Jan 29th, 2021
16
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. ######################
  4. #### Mount Script ####
  5. ######################
  6. ### Version 0.96.7 ###
  7. ######################
  8.  
  9. ####### EDIT ONLY THESE SETTINGS #######
  10.  
  11. # INSTRUCTIONS
  12. # 1. Change the name of the rclone remote and shares to match your setup
  13. # 2. NOTE: enter RcloneRemoteName WITHOUT ':'
  14. # 3. Optional: include custom command and bind mount settings
  15. # 4. Optional: include extra folders in mergerfs mount
  16.  
  17. # REQUIRED SETTINGS
  18. RcloneRemoteName="gcrypt" # Name of rclone remote mount WITHOUT ':'. NOTE: Choose your encrypted remote for sensitive data
  19. RcloneMountShare="/mnt/user/mount_rclone" # where your rclone remote will be located without trailing slash  e.g. /mnt/user/mount_rclone
  20. LocalFilesShare="/mnt/user/local" # location of the local files and MountFolders you want to upload without trailing slash to rclone e.g. /mnt/user/local. Enter 'ignore' to disable
  21. MergerfsMountShare="/mnt/user/mount_mergerfs" # location without trailing slash  e.g. /mnt/user/mount_mergerfs. Enter 'ignore' to disable
  22. #DockerStart="nzbget plex sonarr radarr ombi" # list of dockers, separated by space, to start once mergerfs mount verified. Remember to disable AUTOSTART for dockers added in docker settings page
  23. DockerStart="binhex-delugevpn nzbget nzbhydra2 ombi plex sonarr radarr"
  24. MountFolders=\{"downloads/complete,downloads/intermediate,downloads/seeds,movies,tv,gcrypt"\} # comma separated list of folders to create within the mount
  25.  
  26. # Note: Again - remember to NOT use ':' in your remote name above
  27.  
  28. # OPTIONAL SETTINGS
  29.  
  30. # Add extra paths to mergerfs mount in addition to LocalFilesShare
  31. LocalFilesShare2="ignore" # without trailing slash e.g. /mnt/user/other__remote_mount/or_other_local_folder.  Enter 'ignore' to disable
  32. LocalFilesShare3="ignore"
  33. LocalFilesShare4="ignore"
  34.  
  35. # Add extra commands or filters
  36. Command1="--rc"
  37. Command2=""
  38. Command3=""
  39. Command4=""
  40. Command5=""
  41. Command6=""
  42. Command7=""
  43. Command8=""
  44.  
  45. CreateBindMount="N" # Y/N. Choose whether to bind traffic to a particular network adapter
  46. RCloneMountIP="192.168.1.252" # My unraid IP is 172.30.12.2 so I create another similar IP address
  47. NetworkAdapter="eth0" # choose your network adapter. eth0 recommended
  48. VirtualIPNumber="2" # creates eth0:x e.g. eth0:1.  I create a unique virtual IP addresses for each mount & upload so I can monitor and traffic shape for each of them
  49.  
  50. ####### END SETTINGS #######
  51.  
  52. ###############################################################################
  53. #####   DO NOT EDIT ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING   #######
  54. ###############################################################################
  55.  
  56. ####### Preparing mount location variables #######
  57. RcloneMountLocation="$RcloneMountShare/$RcloneRemoteName" # Location for rclone mount
  58. LocalFilesLocation="$LocalFilesShare/$RcloneRemoteName" # Location for local files to be merged with rclone mount
  59. MergerFSMountLocation="$MergerfsMountShare/$RcloneRemoteName" # Rclone data folder location
  60.  
  61. ####### create directories for rclone mount and mergerfs mounts #######
  62. mkdir -p /mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName #for script files
  63. if [[  $LocalFileShare == 'ignore' ]]; then
  64.     echo "$(date "+%d.%m.%Y %T") INFO: Not creating local folders as requested."
  65. else
  66.     echo "$(date "+%d.%m.%Y %T") INFO: Creating local folders."
  67.     eval mkdir -p $LocalFilesLocation/"$MountFolders"
  68. fi
  69. mkdir -p $RcloneMountLocation
  70. mkdir -p $MergerFSMountLocation
  71.  
  72. #######  Check if script is already running  #######
  73. echo "$(date "+%d.%m.%Y %T") INFO: *** Starting mount of remote ${RcloneRemoteName}"
  74. echo "$(date "+%d.%m.%Y %T") INFO: Checking if this script is already running."
  75. if [[ -f "/mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName/mount_running" ]]; then
  76.     echo "$(date "+%d.%m.%Y %T") INFO: Exiting script as already running."
  77.     exit
  78. else
  79.     echo "$(date "+%d.%m.%Y %T") INFO: Script not running - proceeding."
  80.     touch /mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName/mount_running
  81. fi
  82.  
  83. ####### Checking have connectivity #######
  84.  
  85. echo "$(date "+%d.%m.%Y %T") INFO: *** Checking if online"
  86. ping -q -c2 google.com > /dev/null # -q quiet, -c number of pings to perform
  87. if [ $? -eq 0 ]; then # ping returns exit status 0 if successful
  88.     echo "$(date "+%d.%m.%Y %T") PASSED: *** Internet online"
  89. else
  90.     echo "$(date "+%d.%m.%Y %T") FAIL: *** No connectivity.  Will try again on next run"
  91.     rm /mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName/mount_running
  92.     exit
  93. fi
  94.  
  95. #######  Create Rclone Mount  #######
  96.  
  97. # Check If Rclone Mount Already Created
  98. if [[ -f "$RcloneMountLocation/mountcheck" ]]; then
  99.     echo "$(date "+%d.%m.%Y %T") INFO: Success ${RcloneRemoteName} remote is already mounted."
  100. else
  101.     echo "$(date "+%d.%m.%Y %T") INFO: Mount not running. Will now mount ${RcloneRemoteName} remote."
  102. # Creating mountcheck file in case it doesn't already exist
  103.     echo "$(date "+%d.%m.%Y %T") INFO: Recreating mountcheck file for ${RcloneRemoteName} remote."
  104.     touch mountcheck
  105.     rclone copy mountcheck $RcloneRemoteName: -vv --no-traverse
  106. # Check bind option
  107.     if [[  $CreateBindMount == 'Y' ]]; then
  108.         echo "$(date "+%d.%m.%Y %T") INFO: *** Checking if IP address ${RCloneMountIP} already created for remote ${RcloneRemoteName}"
  109.         ping -q -c2 $RCloneMountIP > /dev/null # -q quiet, -c number of pings to perform
  110.         if [ $? -eq 0 ]; then # ping returns exit status 0 if successful
  111.             echo "$(date "+%d.%m.%Y %T") INFO: *** IP address ${RCloneMountIP} already created for remote ${RcloneRemoteName}"
  112.         else
  113.             echo "$(date "+%d.%m.%Y %T") INFO: *** Creating IP address ${RCloneMountIP} for remote ${RcloneRemoteName}"
  114.             ip addr add $RCloneMountIP/24 dev $NetworkAdapter label $NetworkAdapter:$VirtualIPNumber
  115.         fi
  116.         echo "$(date "+%d.%m.%Y %T") INFO: *** Created bind mount ${RCloneMountIP} for remote ${RcloneRemoteName}"
  117.     else
  118.         RCloneMountIP=""
  119.         echo "$(date "+%d.%m.%Y %T") INFO: *** Creating mount for remote ${RcloneRemoteName}"
  120.     fi
  121. # create rclone mount
  122.     rclone mount \
  123.     --allow-other \
  124.     --buffer-size 256M \
  125.     --dir-cache-time 720h \
  126.     --drive-chunk-size 512M \
  127.     --verbose \
  128.         --log-file=/mnt/user/rclone-logs/out.log \
  129.     --vfs-read-chunk-size 128M \
  130.     --vfs-read-chunk-size-limit off \
  131.     --vfs-cache-mode writes \
  132.     --bind=$RCloneMountIP \
  133.     $RcloneRemoteName: $RcloneMountLocation &
  134.  
  135. # Check if Mount Successful
  136.     echo "$(date "+%d.%m.%Y %T") INFO: sleeping for 5 seconds"
  137. # slight pause to give mount time to finalise
  138.     sleep 5
  139.     echo "$(date "+%d.%m.%Y %T") INFO: continuing..."
  140.     if [[ -f "$RcloneMountLocation/mountcheck" ]]; then
  141.         echo "$(date "+%d.%m.%Y %T") INFO: Successful mount of ${RcloneRemoteName} mount."
  142.     else
  143.         echo "$(date "+%d.%m.%Y %T") CRITICAL: ${RcloneRemoteName} mount failed - please check for problems.  Stopping dockers"
  144.         docker stop $DockerStart
  145.         rm /mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName/mount_running
  146.         exit
  147.     fi
  148. fi
  149.  
  150. ####### Start MergerFS Mount #######
  151.  
  152. if [[  $MergerfsMountShare == 'ignore' ]]; then
  153.     echo "$(date "+%d.%m.%Y %T") INFO: Not creating mergerfs mount as requested."
  154. else
  155.     if [[ -f "$MergerFSMountLocation/mountcheck" ]]; then
  156.         echo "$(date "+%d.%m.%Y %T") INFO: Check successful, ${RcloneRemoteName} mergerfs mount in place."
  157.     else
  158. # check if mergerfs already installed
  159.         if [[ -f "/bin/mergerfs" ]]; then
  160.             echo "$(date "+%d.%m.%Y %T") INFO: Mergerfs already installed, proceeding to create mergerfs mount"
  161.         else
  162. # Build mergerfs binary
  163.             echo "$(date "+%d.%m.%Y %T") INFO: Mergerfs not installed - installing now."
  164.             mkdir -p /mnt/user/appdata/other/rclone/mergerfs
  165.             docker run -v /mnt/user/appdata/other/rclone/mergerfs:/build --rm trapexit/mergerfs-static-build
  166.             mv /mnt/user/appdata/other/rclone/mergerfs/mergerfs /bin
  167. # check if mergerfs install successful
  168.             echo "$(date "+%d.%m.%Y %T") INFO: *sleeping for 5 seconds"
  169.             sleep 5
  170.             if [[ -f "/bin/mergerfs" ]]; then
  171.                 echo "$(date "+%d.%m.%Y %T") INFO: Mergerfs installed successfully, proceeding to create mergerfs mount."
  172.             else
  173.                 echo "$(date "+%d.%m.%Y %T") ERROR: Mergerfs not installed successfully.  Please check for errors.  Exiting."
  174.                 rm /mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName/mount_running
  175.                 exit
  176.             fi
  177.         fi
  178. # Create mergerfs mount
  179.         echo "$(date "+%d.%m.%Y %T") INFO: Creating ${RcloneRemoteName} mergerfs mount."
  180. # Extra Mergerfs folders
  181.         if [[  $LocalFilesShare2 != 'ignore' ]]; then
  182.             echo "$(date "+%d.%m.%Y %T") INFO: Adding ${LocalFilesShare2} to ${RcloneRemoteName} mergerfs mount."
  183.             LocalFilesShare2=":$LocalFilesShare2"
  184.         else
  185.             LocalFilesShare2=""
  186.         fi
  187.         if [[  $LocalFilesShare3 != 'ignore' ]]; then
  188.             echo "$(date "+%d.%m.%Y %T") INFO: Adding ${LocalFilesShare3} to ${RcloneRemoteName} mergerfs mount."
  189.             LocalFilesShare3=":$LocalFilesShare3"
  190.         else
  191.             LocalFilesShare3=""
  192.         fi
  193.         if [[  $LocalFilesShare4 != 'ignore' ]]; then
  194.             echo "$(date "+%d.%m.%Y %T") INFO: Adding ${LocalFilesShare4} to ${RcloneRemoteName} mergerfs mount."
  195.             LocalFilesShare4=":$LocalFilesShare4"
  196.         else
  197.             LocalFilesShare4=""
  198.         fi
  199. # make sure mergerfs mount point is empty
  200.         mv $MergerFSMountLocation $LocalFilesLocation
  201.         mkdir -p $MergerFSMountLocation
  202. # mergerfs mount command
  203.         mergerfs $LocalFilesLocation:$RcloneMountLocation$LocalFilesShare2$LocalFilesShare3$LocalFilesShare4 $MergerFSMountLocation -o rw,async_read=false,use_ino,allow_other,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true
  204. # check if mergerfs mount successful
  205.         echo "$(date "+%d.%m.%Y %T") INFO: Checking if ${RcloneRemoteName} mergerfs mount created."
  206.         if [[ -f "$MergerFSMountLocation/mountcheck" ]]; then
  207.             echo "$(date "+%d.%m.%Y %T") INFO: Check successful, ${RcloneRemoteName} mergerfs mount created."
  208.         else
  209.             echo "$(date "+%d.%m.%Y %T") CRITICAL: ${RcloneRemoteName} mergerfs mount failed.  Stopping dockers."
  210.             docker stop $DockerStart
  211.             rm /mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName/mount_running
  212.             exit
  213.         fi
  214.     fi
  215. fi
  216.  
  217. ####### Starting Dockers That Need Mergerfs Mount To Work Properly #######
  218.  
  219. # only start dockers once
  220. if [[ -f "/mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName/dockers_started" ]]; then
  221.     echo "$(date "+%d.%m.%Y %T") INFO: dockers already started."
  222. else
  223.     touch /mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName/dockers_started
  224.     echo "$(date "+%d.%m.%Y %T") INFO: Starting dockers."
  225.     docker start $DockerStart
  226. fi
  227.  
  228. rm /mnt/user/appdata/other/rclone/remotes/$RcloneRemoteName/mount_running
  229. echo "$(date "+%d.%m.%Y %T") INFO: Script complete"
  230.  
  231. exit
RAW Paste Data