Finding the fastest mirror: Difference between revisions
No edit summary |
Eric.schultz (talk | contribs) (Only try the mirror's once - Certain errors will retry inf and hang the script. sh version of echo does not support -e option corrupting the output.) |
||
Line 39: | Line 39: | ||
#find best | #find best | ||
for URL in $MIRRORS; do | for URL in $MIRRORS; do | ||
TIME=$(time_cmd wget -T 1 -q ${URL%/} -O /dev/null) | TIME=$(time_cmd wget -T 1 -t 1 -q ${URL%/} -O /dev/null) | ||
if [ -n "$TIME" ]; then | if [ -n "$TIME" ]; then | ||
echo "$(get_hostname_url $URL) was $TIME" | echo "$(get_hostname_url $URL) was $TIME" | ||
Line 47: | Line 47: | ||
echo | echo $DATA | sort -n | tail -n +2 > $DST | ||
[ $? = 0 ] && echo file $DST created | [ $? = 0 ] && echo file $DST created | ||
Revision as of 15:43, 11 July 2022
After you install Alpine, you may be wondering how do I figure out the fastest mirror again?
Contents of /home/user/fastestmirror
data=""
for s in $(wget -qO- http://rsync.alpinelinux.org/alpine/MIRRORS.txt); do
t=$(time -f "%E" wget -q $s/MIRRORS.txt -O /dev/null 2>&1)
echo "$s was $t"
data="$data$t $s\n"
done
echo "===RESULTS==="
echo -e $data | sort
This is more advanced implementation.
Contents of /etc/apk/fastest-mirror
#!/bin/sh
get_hostname_url() {
local n=${1#*://}
echo ${n%%/*}
}
time_cmd() {
local proc=$(cut -d ' ' -f1 /proc/uptime)
local start="$(echo $proc | cut -d . -f1)$(echo $proc | cut -d . -f2)"
$@ >/dev/null 2>&1 || return
proc=$(cut -d ' ' -f1 /proc/uptime)
local end="$(echo $proc | cut -d . -f1)$(echo $proc | cut -d . -f2)"
echo $(( $end - $start ))
}
DATA=""
MIRRORS=$(wget -qO- "http://rsync.alpinelinux.org/alpine/MIRRORS.txt")
DST=/etc/apk/mirrors.txt
#find best
for URL in $MIRRORS; do
TIME=$(time_cmd wget -T 1 -t 1 -q ${URL%/} -O /dev/null)
if [ -n "$TIME" ]; then
echo "$(get_hostname_url $URL) was $TIME"
DATA="$DATA$TIME $URL\n"
fi
done
echo $DATA | sort -n | tail -n +2 > $DST
[ $? = 0 ] && echo file $DST created
BEST=$(head -n1 $DST | cut -d ' ' -f2)
echo "Best mirror is: $BEST"
sed -i -r 's#^http.+/(.+/main)#'${BEST%/}'/\1#' /etc/apk/repositories
sed -i -r 's#^http.+/(.+/community)#'${BEST%/}'/\1#' /etc/apk/repositories
sed -i -r 's#^http.+/(.+/testing)#'${BEST%/}'/\1#' /etc/apk/repositories