Guest User

Untitled

a guest
Sep 14th, 2022
1,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.19 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -euo pipefail
  4.  
  5. #Disclaimer
  6.  
  7. printf "\033[0;31mDisclaimer: This installer is unofficial and Ultra.cc staff will not support any issues with it.\033[0m\n"
  8. read -rp "Type confirm if you wish to continue: " input
  9. if [ ! "$input" = "confirm" ]; then
  10.   exit
  11. fi
  12. echo
  13.  
  14. declare -a paths
  15. paths[1]="${HOME}/Stuff/Mount"
  16. paths[2]="${HOME}/Stuff/Local/Downloads/torrents"
  17. paths[3]="${HOME}/Stuff/Local/Downloads/usenet"
  18. paths[4]="${HOME}/MergerFS"
  19. paths[5]="${HOME}/scripts"
  20. paths[6]="${HOME}/Stuff/Local/Movies"
  21. paths[7]="${HOME}/Stuff/Local/TV Shows"
  22.  
  23. #Checks
  24.  
  25. read -rp "Enter the gclone remote name for your Google Drive: " remote
  26. echo
  27. if ! gclone lsd "${remote}:" >>/dev/null; then
  28.   echo "Configure your rclone remote correctly, then run the script again. https://docs.usbx.me/link/6#bkmrk-rclone"
  29.   exit 1
  30. fi
  31.  
  32. echo "Select type of rclone upload script [ Choose from 1 - 3 ]: "
  33.  
  34. select upload in Normal Discord-Notification Quit; do
  35.   case ${upload} in
  36.   Normal)
  37.     upload="normal"
  38.     break
  39.     ;;
  40.   Discord-Notification)
  41.     upload="discord"
  42.     echo
  43.     read -rp "Enter the Discord Webhook URL: " webhook
  44.     break
  45.     ;;
  46.   Quit)
  47.     exit 0
  48.     ;;
  49.   *)
  50.     echo "Invalid option ${REPLY}"
  51.     ;;
  52.   esac
  53. done
  54. echo
  55. echo "Rclone-MergerFS workflow setup started.."
  56.  
  57. for i in {1..7}; do
  58.   if [ ! -d "${paths[${i}]}" ]; then
  59.     mkdir -p "${paths[${i}]}"
  60.   fi
  61. done
  62.  
  63. if [[ -n $(ls -A "${paths[1]}") || -n $(ls -A "${paths[4]}") ]]; then
  64.   echo
  65.   echo "ERROR: ${paths[1]} or ${paths[4]} is not empty."
  66.   echo "Please move the data out of these directories and then run the script again."
  67.   echo "Both of them must be empty for the rclone and mergerfs mounts to work properly."
  68.   echo
  69.   exit 1
  70. fi
  71.  
  72. #Install systemd services
  73.  
  74. cat <<EOF | tee "${HOME}/.config/systemd/user/rclone-vfs.service" >/dev/null
  75. [Unit]
  76. Description=RClone VFS Service
  77. Wants=network-online.target
  78. After=network-online.target
  79. [Service]
  80. Type=notify
  81. TimeoutStopSec=60
  82. Environment=GOMAXPROCS=2
  83. ExecStart=gclone mount ${remote}: %h/Stuff/Mount \\
  84.   --use-mmap \\
  85.   --dir-cache-time 1000h \\
  86.   --poll-interval=15s \\
  87.   --vfs-cache-mode writes \\
  88.   --tpslimit 10
  89. StandardOutput=file:%h/scripts/rclone_vfs_mount.log
  90. ExecStop=/bin/fusermount -uz %h/Stuff/Mount
  91. Restart=on-failure
  92. [Install]
  93. WantedBy=default.target
  94. EOF
  95.  
  96. cat <<EOF | tee "${HOME}/.config/systemd/user/mergerfs.service" >/dev/null
  97. [Unit]
  98. Description = MergerFS Service
  99. After=rclone-vfs.service
  100. RequiresMountsFor=%h/Stuff/Local
  101. RequiresMountsFor=%h/Stuff/Mount
  102. [Service]
  103. Type=forking
  104. TimeoutStopSec=60
  105. ExecStart=mergerfs \\
  106.     -o use_ino,func.getattr=newest,category.action=all \\
  107.     -o category.create=ff,cache.files=auto-full,threads=8,allow_other \\
  108.     %h/Stuff/Local:%h/Stuff/Mount %h/MergerFS
  109. StandardOutput=file:%h/scripts/mergerfs_mount.log
  110. ExecStop=/bin/fusermount -uz %h/MergerFS
  111. Restart=on-failure
  112. [Install]
  113. WantedBy=default.target
  114. EOF
  115.  
  116. systemctl --user daemon-reload
  117. systemctl --user enable --now --quiet rclone-vfs.service mergerfs.service
  118. sleep 5
  119.  
  120. #Install rclone-upload script
  121.  
  122. if [ "${upload}" == "discord" ]; then
  123.   cat <<EOF | tee "${HOME}/scripts/rclone-upload.sh" >/dev/null
  124. #!/bin/bash
  125. # Rclone upload script with optional Discord notification upon move completion (if something is moved)
  126. #
  127. # Recommended for use via cron
  128. # For example: */10 * * * * /path/to/rclone-upload.sh >/dev/null 2>&1
  129. # -----------------------------------------------------------------------------
  130. SOURCE_DIR="\$HOME/Stuff/Local/"
  131. DESTINATION_DIR="${remote}:"
  132. DISCORD_WEBHOOK_URL="${webhook}"
  133. DISCORD_ICON_OVERRIDE="https://i.imgur.com/MZYwA1I.png"
  134. DISCORD_NAME_OVERRIDE="RCLONE"
  135. LOCK_FILE="\$HOME/scripts/rclone-upload.lock"
  136. LOG_FILE="\$HOME/scripts/rclone-upload.log"
  137. # DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING
  138. # -----------------------------------------------------------------------------
  139. trap 'rm -f \$LOCK_FILE; exit 0' SIGINT SIGTERM
  140. if [ -e "\$LOCK_FILE" ]
  141. then
  142.   echo "\$0 is already running."
  143.   exit
  144. else
  145.   if [ -f "\$LOG_FILE" ]
  146.   then
  147.        rm "\$LOG_FILE"
  148.   fi
  149.   touch "\$LOCK_FILE"
  150.  
  151.   rclone_move() {
  152.     rclone_command=\$(
  153.       gclone move -vP \\
  154.       --exclude "Downloads/**" \\
  155.       --drive-chunk-size 64M \\
  156.       --use-mmap \\
  157.       --delete-empty-src-dirs \\
  158.       --log-file="\$LOG_FILE" \\
  159.       --stats=9999m \\
  160.       --tpslimit=5 \\
  161.       --transfers=2 \\
  162.       --checkers=4 \\
  163.       --bwlimit=8M \\
  164.       --drive-stop-on-upload-limit \\
  165.       "\$SOURCE_DIR" "\$DESTINATION_DIR" 2>&1
  166.     )
  167.     # "--stats=9999m" mitigates early stats output
  168.     # "2>&1" ensures error output when running via command line
  169.     echo "\$rclone_command"
  170.   }
  171.   rclone_move
  172.   if [ "\$DISCORD_WEBHOOK_URL" != "" ]; then
  173.  
  174.     rclone_sani_command="\$(echo \$rclone_command | sed 's/\x1b\[[0-9;]*[a-zA-Z]//g')" # Remove all escape sequences
  175.     # Notifications assume following rclone ouput:
  176.     # Transferred: 0 / 0 Bytes, -, 0 Bytes/s, ETA - Errors: 0 Checks: 0 / 0, - Transferred: 0 / 0, - Elapsed time: 0.0s
  177.     transferred_amount=\${rclone_sani_command#*Transferred: }
  178.     transferred_amount=\${transferred_amount%% /*}
  179.    
  180.     send_notification() {
  181.       output_transferred_main=\${rclone_sani_command#*Transferred: }
  182.       output_transferred_main=\${output_transferred_main% Errors*}
  183.       output_errors=\${rclone_sani_command#*Errors: }
  184.       output_errors=\${output_errors% Checks*}
  185.       output_checks=\${rclone_sani_command#*Checks: }
  186.       output_checks=\${output_checks% Transferred*}
  187.       output_transferred=\${rclone_sani_command##*Transferred: }
  188.       output_transferred=\${output_transferred% Elapsed*}
  189.       output_elapsed=\${rclone_sani_command##*Elapsed time: }
  190.      
  191.       notification_data='{
  192.        "username": "'"\$DISCORD_NAME_OVERRIDE"'",
  193.        "avatar_url": "'"\$DISCORD_ICON_OVERRIDE"'",
  194.        "content": null,
  195.        "embeds": [
  196.          {
  197.            "title": "Rclone Upload Task: Success!",
  198.            "color": 4094126,
  199.            "fields": [
  200.              {
  201.                "name": "Transferred",
  202.                "value": "'"\$output_transferred_main"'"
  203.              },
  204.              {
  205.                "name": "Errors",
  206.                "value": "'"\$output_errors"'"
  207.              },
  208.              {
  209.                "name": "Checks",
  210.                "value": "'"\$output_checks"'"
  211.              },
  212.              {
  213.                "name": "Transferred",
  214.                "value": "'"\$output_transferred"'"
  215.              },
  216.              {
  217.                "name": "Elapsed time",
  218.                "value": "'"\$output_elapsed"'"
  219.              }
  220.            ],
  221.            "thumbnail": {
  222.              "url": null
  223.            }
  224.          }
  225.        ]
  226.      }'
  227.      
  228.       /usr/bin/curl -H "Content-Type: application/json" -d "\$notification_data" \$DISCORD_WEBHOOK_URL
  229.     }
  230.    
  231.     if [ "\$transferred_amount" != "0 B" ]; then
  232.       send_notification
  233.     fi
  234.   fi
  235.   rm -f "\$LOCK_FILE"
  236.   trap - SIGINT SIGTERM
  237.   exit
  238. fi
  239. EOF
  240. else
  241.   cat <<EOF | tee "${HOME}/scripts/rclone-upload.sh" >/dev/null
  242. #!/bin/bash
  243. lock_file="\$HOME/scripts/rclone-upload.lock"
  244. trap 'rm -f "\$lock_file"; exit 0' SIGINT SIGTERM
  245. if [ -e "\$lock_file" ]
  246. then
  247.     echo "Rclone upload script is already running."
  248.     exit
  249. else
  250.     if [ -f "\$HOME"/scripts/rclone-upload.log ]
  251.     then
  252.         rm "\$HOME"/scripts/rclone-upload.log
  253.     fi
  254.     touch "\$lock_file"
  255.     gclone move -P "\$HOME"/Stuff/Local/ ${remote}: \\
  256.         --exclude "Downloads/**" \\
  257.         --drive-chunk-size 64M \\
  258.         --tpslimit 5 \\
  259.         -vvv \\
  260.         --drive-stop-on-upload-limit \\
  261.         --delete-empty-src-dirs \\
  262.         --bwlimit=8M \\
  263.         --use-mmap \\
  264.         --transfers=2 \\
  265.         --checkers=4 \\
  266.         --log-file "\$HOME"/scripts/rclone-upload.log
  267.     rm -f "\$lock_file"
  268.     trap - SIGINT SIGTERM
  269.     exit
  270. fi
  271. EOF
  272. fi
  273.  
  274. chmod +x "${HOME}/scripts/rclone-upload.sh"
  275.  
  276. #Install rclone-upload cronjob
  277.  
  278. croncmd="${HOME}/scripts/rclone-upload.sh > /dev/null 2>&1"
  279. cronjob="0 19 * * * $croncmd"
  280. (
  281.   crontab -l 2>/dev/null | grep -v -F "$croncmd" || :
  282.   echo "$cronjob"
  283. ) | crontab -
  284.  
  285. echo "Work flow set up complete. Continue following the guide."
Add Comment
Please, Sign In to add comment