Finding the fastest mirror

From Alpine Linux
Revision as of 18:47, 10 November 2022 by Leonclx (talk | contribs)

After you install Alpine, you may be wondering how do I figure out the fastest mirror again?

Don't use this script, it's broken

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.

Don't use this script, it's broken

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