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