Last active 1611700355

Provisions a droplet on DigitalOcean to download a given torrent file, then reupload to a given rclone remote

Revision d6d720246ea78dd9c7698303425580b100dab1f6

digitalocean-torrentdl.sh Raw
1#!/bin/bash
2# Provisions a droplet on DigitalOcean to download a given torrent file, then reupload to a given rclone remote
3# Prints estimated USD cost of transaction when complete
4# uses your user's rclone.conf, so set up the remote locally and make sure it works
5# torrentdl.sh https://releases.ubuntu.com/20.04/ubuntu-20.04.1-live-server-amd64.iso.torrent b2:my-linux-isos/ubuntu/
6export IMAGE="ubuntu-20-04-x64"
7export REGION="sgp1"
8export SIZE="s-4vcpu-8gb"
9export HOSTNAME="torrent-dl"
10export TORRENT="$1"
11export DESTREMOTE="$2"
12export URLREGEX='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$'
13
14if [[ $TORRENT =~ $URLREGEX ]]; then
15 echo Downloading torrent from url...
16 wget "$TORRENT"
17 export TORRENT=`basename "$TORRENT"`
18fi
19
20echo "Downloading '$TORRENT' on $REGION:$IMAGE:$SIZE with hostname $HOSTNAME, then uploading to '$DESTREMOTE'"
21read -p "Press enter to continue, ctrl-c to abort..."
22
23echo "Started at `date`"
24export STARTTIME=`date +%s`
25
26echo "Provisioning droplet..."
27export SSHKEYS=`doctl compute ssh-key list --no-header --format ID | tr '\n' ',' | sed 's/,$/\n/'`
28export IP=$( set -x; doctl compute droplet create $HOSTNAME --ssh-keys $SSHKEYS --region $REGION --size $SIZE --image $IMAGE --wait --no-header --format PublicIPv4 )
29
30echo -n "Droplet at $IP, waiting for connection"
31until (ssh -o StrictHostKeyChecking=no root@$IP mkdir -p /root/.config/rclone /root/t 2>/dev/null >/dev/null); do
32 echo -n "."
33 sleep 1
34done
35ssh-keyscan -H $IP 2>/dev/null >> ~/.ssh/known_hosts
36echo
37
38echo Copying rclone conf and torrent to droplet...
39scp ~/.config/rclone/rclone.conf root@$IP:/root/.config/rclone/rclone.conf
40scp "$TORRENT" root@$IP:/root/t/
41
42echo Installing dependencies...
43ssh -t root@$IP "apt-get update; apt-get install -y aria2; curl https://rclone.org/install.sh | sudo bash"
44echo Downloading torrent...
45ssh -t root@$IP aria2c -d "/root/t/" --enable-dht=false --enable-peer-exchange=false --seed-time=0 "/root/t/*.torrent"
46echo Uploading torrent to rclone remote...
47ssh -t root@$IP rclone copy /root/t/ "$DESTREMOTE" -P --fast-list --transfers=$(($LINES-5))
48echo Job complete, deleting droplet
49$(set -x; doctl compute droplet delete -f $HOSTNAME)
50
51echo "Finished at `date`"
52export ENDTIME=`date +%s`
53echo "Total run time: $(($ENDTIME-$STARTTIME))"
54echo -n "Total (estimated) cost: USD $"
55doctl compute size list | grep $SIZE | awk '{print $5}' | while read p; do python3 -c "print(($p/2592000)*($ENDTIME-$STARTTIME))"; done