Finding the fastest mirror

From Alpine Linux
Revision as of 22:14, 26 September 2021 by Legale (talk | contribs)

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 -q ${URL%/} -O /dev/null) if [ -n "$TIME" ]; then echo "$(get_hostname_url $URL) was $TIME" DATA="$DATA$TIME $URL\n" fi done echo -e $DATA | sort -n | tail -n +2 > $DST $? && 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