digitalocean-torrentdl.sh
· 2.3 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
# Torrent filename should be a local file, not a URL
# torrentdl.sh ubuntu-20.04-server-amd64.iso.torrent b2: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"
echo "Downloading '$1' on $REGION:$IMAGE:$SIZE with hostname $HOSTNAME, then uploading to '$2'"
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 >> ~/.ssh/known_hosts
echo
echo Installing dependencies on droplet...
ssh -t root@$IP "apt-get update 2>&1 >/dev/null && apt-get install -y aria2; curl https://rclone.org/install.sh | sudo bash"
echo Copying rclone conf and torrent to droplet...
scp ~/.config/rclone/rclone.conf root@$IP:/root/.config/rclone/rclone.conf 2>/dev/null >/dev/null
scp "$1" root@$IP:/root/t/ 2>/dev/null >/dev/null
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 | # Torrent filename should be a local file, not a URL |
| 6 | # torrentdl.sh ubuntu-20.04-server-amd64.iso.torrent b2:linux-isos/ubuntu/ |
| 7 | export IMAGE="ubuntu-20-04-x64" |
| 8 | export REGION="sgp1" |
| 9 | export SIZE="s-4vcpu-8gb" |
| 10 | export HOSTNAME="torrent-dl" |
| 11 | export TORRENT="$1" |
| 12 | export DESTREMOTE="$2" |
| 13 | |
| 14 | echo "Downloading '$1' on $REGION:$IMAGE:$SIZE with hostname $HOSTNAME, then uploading to '$2'" |
| 15 | read -p "Press enter to continue, ctrl-c to abort..." |
| 16 | echo "Started at `date`" |
| 17 | export STARTTIME=`date +%s` |
| 18 | echo "Provisioning droplet..." |
| 19 | export SSHKEYS=`doctl compute ssh-key list --no-header --format ID | tr '\n' ',' | sed 's/,$/\n/'` |
| 20 | export IP=$( set -x; doctl compute droplet create $HOSTNAME --ssh-keys $SSHKEYS --region $REGION --size $SIZE --image $IMAGE --wait --no-header --format PublicIPv4 ) |
| 21 | echo -n "Droplet at $IP, waiting for connection" |
| 22 | until (ssh -o StrictHostKeyChecking=no root@$IP mkdir -p /root/.config/rclone /root/t 2>/dev/null >/dev/null); do |
| 23 | echo -n "." |
| 24 | sleep 1 |
| 25 | done |
| 26 | ssh-keyscan -H $IP >> ~/.ssh/known_hosts |
| 27 | echo |
| 28 | echo Installing dependencies on droplet... |
| 29 | ssh -t root@$IP "apt-get update 2>&1 >/dev/null && apt-get install -y aria2; curl https://rclone.org/install.sh | sudo bash" |
| 30 | echo Copying rclone conf and torrent to droplet... |
| 31 | scp ~/.config/rclone/rclone.conf root@$IP:/root/.config/rclone/rclone.conf 2>/dev/null >/dev/null |
| 32 | scp "$1" root@$IP:/root/t/ 2>/dev/null >/dev/null |
| 33 | echo Downloading torrent... |
| 34 | ssh -t root@$IP aria2c -d "/root/t/" --enable-dht=false --enable-peer-exchange=false --seed-time=0 "/root/t/*.torrent" |
| 35 | echo Uploading torrent to rclone remote... |
| 36 | ssh -t root@$IP rclone copy /root/t/ "$DESTREMOTE" -P --fast-list --transfers=$(($LINES-5)) |
| 37 | echo Job complete, deleting droplet |
| 38 | $(set -x; doctl compute droplet delete -f $HOSTNAME) |
| 39 | echo "Finished at `date`" |
| 40 | export ENDTIME=`date +%s` |
| 41 | echo "Total run time: $(($ENDTIME-$STARTTIME))" |
| 42 | echo -n "Total (estimated) cost: USD $" |
| 43 | doctl compute size list | grep $SIZE | awk '{print $5}' | while read p; do python3 -c "print(($p/2592000)*($ENDTIME-$STARTTIME))"; done |