From 807aea9b43d63be5de25afeee202f89abb11c122 Mon Sep 17 00:00:00 2001 From: eminga <24529618+eminga@users.noreply.github.com> Date: Sun, 15 Jul 2018 19:49:50 +0200 Subject: [PATCH] try other mirror if download fails --- README.md | 2 +- ts3updater.sh | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fce0e3f..e74fc4a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A lightweight script to install or update a TeamSpeak 3 server on Linux or FreeB * tar * bzip2 -All other dependencies (awk, basename, cd, command, cut, dirname, echo, grep, mktemp, printf, read, sed, test, touch, uname, wc, and an sh-compatible shell) are installed by default on most systems. +All other dependencies (basename, cd, command, cut, dirname, echo, grep, mktemp, printf, read, sed, test, touch, uname, wc, and an sh-compatible shell) are installed by default on most systems. ## How to use ### Install a new TeamSpeak 3 server diff --git a/ts3updater.sh b/ts3updater.sh index 5be60df..9fc3d0a 100755 --- a/ts3updater.sh +++ b/ts3updater.sh @@ -1,7 +1,7 @@ #!/bin/sh # Script Name: ts3updater.sh # Author: eminga -# Version: 1.3 +# Version: 1.4 # Description: Installs and updates TeamSpeak 3 servers # License: MIT License @@ -55,16 +55,28 @@ version=$(printf '%s' "$server" | jq -r '.version') if [ "$old_version" != "$version" ]; then echo "New version available: $version" checksum=$(printf '%s' "$server" | jq -r '.checksum') - link=$(printf '%s' "$server" | jq -r '.mirrors | values[]') + links=$(printf '%s' "$server" | jq -r '.mirrors | values[]') - # select random mirror - i=$(printf '%s\n' "$link" | wc -l) - i=$(awk "BEGIN{srand(); printf \"%d\",(rand()*$i) + 1}") - link=$(printf '%s' "$link" | sed -n "$i"p) + # order mirrors randomly + if command -v shuf > /dev/null 2>&1; then + links=$(printf '%s' "$links" | shuf) + fi tmpfile=$(mktemp) - echo "Downloading the file $link" - curl -Lo "$tmpfile" "$link" + i=1 + n=$(printf '%s\n' "$links" | wc -l) + + # try to download from mirrors until download is successful or all mirrors tried + while [ "$i" -le "$n" ]; do + link=$(printf '%s' "$links" | sed -n "$i"p) + echo "Downloading the file $link" + curl -Lo "$tmpfile" "$link" + if [ $? = 0 ]; then + i=$(( n + 1 )) + else + i=$(( i + 1 )) + fi + done if command -v sha256sum > /dev/null 2>&1; then sha256=$(sha256sum "$tmpfile" | cut -b 1-64)