Advertisement
Guest User

upload script

a guest
Mar 14th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.03 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ######################
  4. ### Upload Script ####
  5. ######################
  6. ### Version 0.95.5 ###
  7. ######################
  8.  
  9. ####### EDIT ONLY THESE SETTINGS #######
  10.  
  11. # INSTRUCTIONS
  12. # 1. Edit the settings below to match your setup
  13. # 2. NOTE: enter RcloneRemoteName WITHOUT ':'
  14. # 3. Optional: Add additional commands or filters
  15. # 4. Optional: Use bind mount settings for potential traffic shaping/monitoring
  16. # 5. Optional: Use service accounts in your upload remote
  17. # 6. Optional: Use backup directory for rclone sync jobs
  18.  
  19. # REQUIRED SETTINGS
  20. RcloneCommand="copy" # choose your rclone command e.g. move, copy, sync
  21. RcloneRemoteName="gdrive_crypt" # Name of rclone remote mount WITHOUT ':'.
  22. RcloneUploadRemoteName="gdrive_crypt" # If you have a second remote created for uploads put it here. Otherwise use the same remote as RcloneRemoteName.
  23. LocalFilesShare="/mnt/user/mount_rclone" # location of the local files without trailing slash you want to rclone to use
  24. RcloneMountShare="/mnt/user/mount_rclone" # where your rclone mount is located without trailing slash e.g. /mnt/user/mount_rclone
  25. MinimumAge="0m" # sync files suffix ms|s|m|h|d|w|M|y
  26. ModSort="ascending" # "ascending" oldest files first, "descending" newest files first
  27.  
  28. # Note: Again - remember to NOT use ':' in your remote name above
  29.  
  30. # Bandwidth limits: specify the desired bandwidth in kBytes/s, or use a suffix b|k|M|G. Or 'off' or '0' for unlimited. The script uses --drive-stop-on-upload-limit which stops the script if the 750GB/day limit is achieved, so you no longer have to slow 'trickle' your files all day if you don't want to e.g. could just do an unlimited job overnight.
  31. BWLimit1Time="01:00"
  32. BWLimit1="off"
  33. BWLimit2Time="08:00"
  34. BWLimit2="15M"
  35. BWLimit3Time="16:00"
  36. BWLimit3="12M"
  37.  
  38. # OPTIONAL SETTINGS
  39.  
  40. # Add name to upload job
  41. JobName="_daily_upload" # Adds custom string to end of checker file. Useful if you're running multiple jobs against the same remote.
  42.  
  43. # Add extra commands or filters
  44. Command1="--exclude downloads/**"
  45. Command2=""
  46. Command3=""
  47. Command4=""
  48. Command5=""
  49. Command6=""
  50. Command7=""
  51. Command8=""
  52.  
  53. # Bind the mount to an IP address
  54. CreateBindMount="N" # Y/N. Choose whether or not to bind traffic to a network adapter.
  55. RCloneMountIP="192.168.1.253" # Choose IP to bind upload to.
  56. NetworkAdapter="eth0" # choose your network adapter. eth0 recommended.
  57. VirtualIPNumber="1" # creates eth0:x e.g. eth0:1.
  58.  
  59. # Use Service Accounts. Instructions: https://github.com/xyou365/AutoRclone
  60. UseServiceAccountUpload="N" # Y/N. Choose whether to use Service Accounts.
  61. ServiceAccountDirectory="/mnt/user/appdata/other/rclone/service_accounts" # Path to your Service Account's .json files.
  62. ServiceAccountFile="sa_gdrive_upload" # Enter characters before counter in your json files e.g. for sa_gdrive_upload1.json -->sa_gdrive_upload100.json, enter "sa_gdrive_upload".
  63. CountServiceAccounts="15" # Integer number of service accounts to use.
  64.  
  65. # Is this a backup job
  66. BackupJob="N" # Y/N. Syncs or Copies files from LocalFilesLocation to BackupRemoteLocation, rather than moving from LocalFilesLocation/RcloneRemoteName
  67. BackupRemoteLocation="backup" # choose location on mount for deleted sync files
  68. BackupRemoteDeletedLocation="backup_deleted" # choose location on mount for deleted sync files
  69. BackupRetention="90d" # How long to keep deleted sync files suffix ms|s|m|h|d|w|M|y
  70.  
  71. ####### END SETTINGS #######
  72.  
  73. ###############################################################################
  74. ##### DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING #####
  75. ###############################################################################
  76.  
  77. ####### Preparing mount location variables #######
  78. if [[ $BackupJob == 'Y' ]]; then
  79. LocalFilesLocation="$LocalFilesShare"
  80. echo "$(date "+%d.%m.%Y %T") INFO: *** Backup selected. Files will be copied or synced from ${LocalFilesLocation} for ${RcloneUploadRemoteName} ***"
  81. else
  82. LocalFilesLocation="$LocalFilesShare/$RcloneRemoteName"
  83. echo "$(date "+%d.%m.%Y %T") INFO: *** Rclone move selected. Files will be moved from ${LocalFilesLocation} for ${RcloneUploadRemoteName} ***"
  84. fi
  85.  
  86. RcloneMountLocation="$RcloneMountShare/$RcloneRemoteName" # Location of rclone mount
  87.  
  88. ####### create directory for script files #######
  89. mkdir -p /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName #for script files
  90.  
  91. ####### Check if script already running ##########
  92. echo "$(date "+%d.%m.%Y %T") INFO: *** Starting rclone_upload script for ${RcloneUploadRemoteName} ***"
  93. if [[ -f "/mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/upload_running$JobName" ]]; then
  94. echo "$(date "+%d.%m.%Y %T") INFO: Exiting as script already running."
  95. exit
  96. else
  97. echo "$(date "+%d.%m.%Y %T") INFO: Script not running - proceeding."
  98. touch /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/upload_running$JobName
  99. fi
  100.  
  101. ####### check if rclone installed ##########
  102. echo "$(date "+%d.%m.%Y %T") INFO: Checking if rclone installed successfully."
  103. if [[ -f "$RcloneMountLocation/mountcheck" ]]; then
  104. echo "$(date "+%d.%m.%Y %T") INFO: rclone installed successfully - proceeding with upload."
  105. else
  106. echo "$(date "+%d.%m.%Y %T") INFO: rclone not installed - will try again later."
  107. rm /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/upload_running$JobName
  108. exit
  109. fi
  110.  
  111. ####### Rotating serviceaccount.json file if using Service Accounts #######
  112. if [[ $UseServiceAccountUpload == 'Y' ]]; then
  113. cd /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/
  114. CounterNumber=$(find -name 'counter*' | cut -c 11,12)
  115. CounterCheck="1"
  116. if [[ "$CounterNumber" -ge "$CounterCheck" ]];then
  117. echo "$(date "+%d.%m.%Y %T") INFO: Counter file found for ${RcloneUploadRemoteName}."
  118. else
  119. echo "$(date "+%d.%m.%Y %T") INFO: No counter file found for ${RcloneUploadRemoteName}. Creating counter_1."
  120. touch /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_1
  121. CounterNumber="1"
  122. fi
  123. ServiceAccount="--drive-service-account-file=$ServiceAccountDirectory/$ServiceAccountFile$CounterNumber.json"
  124. echo "$(date "+%d.%m.%Y %T") INFO: Adjusted service_account_file for upload remote ${RcloneUploadRemoteName} to ${ServiceAccountFile}${CounterNumber}.json based on counter ${CounterNumber}."
  125. else
  126. echo "$(date "+%d.%m.%Y %T") INFO: Uploading using upload remote ${RcloneUploadRemoteName}"
  127. ServiceAccount=""
  128. fi
  129.  
  130. ####### Upload files ##########
  131.  
  132. # Check bind option
  133. if [[ $CreateBindMount == 'Y' ]]; then
  134. echo "$(date "+%d.%m.%Y %T") INFO: *** Checking if IP address ${RCloneMountIP} already created for upload to remote ${RcloneUploadRemoteName}"
  135. ping -q -c2 $RCloneMountIP > /dev/null # -q quiet, -c number of pings to perform
  136. if [ $? -eq 0 ]; then # ping returns exit status 0 if successful
  137. echo "$(date "+%d.%m.%Y %T") INFO: *** IP address ${RCloneMountIP} already created for upload to remote ${RcloneUploadRemoteName}"
  138. else
  139. echo "$(date "+%d.%m.%Y %T") INFO: *** Creating IP address ${RCloneMountIP} for upload to remote ${RcloneUploadRemoteName}"
  140. ip addr add $RCloneMountIP/24 dev $NetworkAdapter label $NetworkAdapter:$VirtualIPNumber
  141. fi
  142. else
  143. RCloneMountIP=""
  144. fi
  145.  
  146. # Remove --delete-empty-src-dirs if rclone sync or copy
  147. if [[ $RcloneCommand == 'move' ]]; then
  148. echo "$(date "+%d.%m.%Y %T") INFO: *** Using rclone move - will add --delete-empty-src-dirs to upload."
  149. DeleteEmpty="--delete-empty-src-dirs "
  150. else
  151. echo "$(date "+%d.%m.%Y %T") INFO: *** Not using rclone move - will remove --delete-empty-src-dirs to upload."
  152. DeleteEmpty=""
  153. fi
  154.  
  155. # Check --backup-directory
  156. if [[ $BackupJob == 'Y' ]]; then
  157. echo "$(date "+%d.%m.%Y %T") INFO: *** Will backup to ${BackupRemoteLocation} and use ${BackupRemoteDeletedLocation} as --backup-directory with ${BackupRetention} retention for ${RcloneUploadRemoteName}."
  158. LocalFilesLocation="$LocalFilesShare"
  159. BackupDir="--backup-dir $RcloneUploadRemoteName:$BackupRemoteDeletedLocation"
  160. else
  161. BackupRemoteLocation=""
  162. BackupRemoteDeletedLocation=""
  163. BackupRetention=""
  164. BackupDir=""
  165. fi
  166.  
  167. # process files
  168. rclone $RcloneCommand $LocalFilesLocation $RcloneUploadRemoteName:$BackupRemoteLocation $ServiceAccount $BackupDir \
  169. --user-agent="$RcloneUploadRemoteName" \
  170. -vv \
  171. --buffer-size 512M \
  172. --drive-chunk-size 512M \
  173. --tpslimit 8 \
  174. --checkers 8 \
  175. --transfers 4 \
  176. --order-by modtime,$ModSort \
  177. --min-age $MinimumAge \
  178. $Command1 $Command2 $Command3 $Command4 $Command5 $Command6 $Command7 $Command8 \
  179. --exclude *fuse_hidden* \
  180. --exclude *_HIDDEN \
  181. --exclude .recycle** \
  182. --exclude .Recycle.Bin/** \
  183. --exclude *.backup~* \
  184. --exclude *.partial~* \
  185. --drive-stop-on-upload-limit \
  186. --bwlimit "${BWLimit1Time},${BWLimit1} ${BWLimit2Time},${BWLimit2} ${BWLimit3Time},${BWLimit3}" \
  187. --bind=$RCloneMountIP $DeleteEmpty
  188.  
  189. # Delete old files from mount
  190. if [[ $BackupJob == 'Y' ]]; then
  191. echo "$(date "+%d.%m.%Y %T") INFO: *** Removing files older than ${BackupRetention} from $BackupRemoteLocation for ${RcloneUploadRemoteName}."
  192. rclone delete --min-age $BackupRetention $RcloneUploadRemoteName:$BackupRemoteDeletedLocation
  193. fi
  194.  
  195. ####### Remove Control Files ##########
  196.  
  197. # update counter and remove other control files
  198. if [[ $UseServiceAccountUpload == 'Y' ]]; then
  199. if [[ "$CounterNumber" == "$CountServiceAccounts" ]];then
  200. rm /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_*
  201. touch /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_1
  202. echo "$(date "+%d.%m.%Y %T") INFO: Final counter used - resetting loop and created counter_1."
  203. else
  204. rm /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_*
  205. CounterNumber=$((CounterNumber+1))
  206. touch /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_$CounterNumber
  207. echo "$(date "+%d.%m.%Y %T") INFO: Created counter_${CounterNumber} for next upload run."
  208. fi
  209. else
  210. echo "$(date "+%d.%m.%Y %T") INFO: Not utilising service accounts."
  211. fi
  212.  
  213. # remove dummy file
  214. rm /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/upload_running$JobName
  215. echo "$(date "+%d.%m.%Y %T") INFO: Script complete"
  216.  
  217. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement