Last active 1611801809

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

Revision 072940e58e01dc117b00f2f01eab7190a223c5d7

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/
6IMAGE="ubuntu-20-04-x64"
7REGION="sgp1"
8SIZE="s-4vcpu-8gb"
9HOSTNAME="torrent-dl"
10URLREGEX='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$'
11TORRENT="$1"
12DESTREMOTE="$2"
13if [[ $TORRENT =~ $URLREGEX ]]; then
14 echo Downloading torrent from url...
15 wget "$TORRENT" -c
16 export TORRENT=`basename "$TORRENT"`
17fi
18
19TORRENTSIZE=`head -1 "$TORRENT" | grep -aoE '6:lengthi[0-9]+' | cut -di -f2 | awk '{s+=$1} END {print s}'`
20if (($TORRENTSIZE<=30000000000)); then # 30GB
21 SIZE="s-1vcpu-2gb"
22elif (($TORRENTSIZE<=60000000000)); then # 60GB
23 SIZE="s-2vcpu-4gb"
24elif (($TORRENTSIZE<=140000000000)); then # 140GB
25 SIZE="s-4vcpu-8gb"
26else # up to 300GB
27 SIZE="s-8vcpu-16gb"
28fi
29
30if [ -v $1 ]; then
31 echo Usage: $0 https://releases.ubuntu.com/20.04/ubuntu-20.04.1-live-server-amd64.iso.torrent b2:my-linux-isos/ubuntu/
32 exit
33fi
34
35echo "Downloading '$TORRENT' on $REGION:$IMAGE:$SIZE with hostname $HOSTNAME, then uploading to '$DESTREMOTE'"
36read -p "Press enter to continue, ctrl-c to abort..."
37
38echo "Started at `date`"
39export STARTTIME=`date +%s`
40
41echo "Provisioning droplet..."
42export SSHKEYS=`doctl compute ssh-key list --no-header --format ID | tr '\n' ',' | sed 's/,$/\n/'`
43export IP=$( set -x; doctl compute droplet create $HOSTNAME --ssh-keys $SSHKEYS --region $REGION --size $SIZE --image $IMAGE --wait --no-header --format PublicIPv4 )
44
45echo -n "Droplet at $IP, waiting for connection"
46until (ssh -o StrictHostKeyChecking=no root@$IP mkdir -p /root/.config/rclone /root/t 2>/dev/null >/dev/null); do
47 echo -n "."
48 sleep 1
49done
50ssh-keyscan -H $IP 2>/dev/null >> ~/.ssh/known_hosts
51echo
52
53echo Copying rclone conf and torrent to droplet...
54scp ~/.config/rclone/rclone.conf root@$IP:/root/.config/rclone/rclone.conf
55scp "$TORRENT" root@$IP:/root/t/
56
57echo Installing dependencies...
58ssh -t root@$IP "apt-get update; apt-get install -y aria2; curl https://rclone.org/install.sh | sudo bash"
59echo Downloading torrent...
60ssh -t root@$IP aria2c -d "/root/t/" --enable-dht=false --enable-peer-exchange=false --seed-time=0 "/root/t/*.torrent"
61echo Uploading torrent to rclone remote...
62ssh -t root@$IP rclone copy /root/t/ "$DESTREMOTE" -P --fast-list --transfers=$(($LINES-5))
63echo Job complete, deleting droplet
64$(set -x; doctl compute droplet delete -f $HOSTNAME)
65
66echo "Finished at `date`"
67export ENDTIME=`date +%s`
68echo "Total run time: $(($ENDTIME-$STARTTIME))"
69echo -n "Total (estimated) cost: USD $"
70doctl compute size list | grep $SIZE | awk '{print $5}' | while read p; do python3 -c "print(($p/2592000)*($ENDTIME-$STARTTIME))"; done