Finding the fastest mirror: Difference between revisions

From Alpine Linux
(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.)
(Add link to How to setup a Alpine Linux mirror)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
After you install Alpine, you may be wondering how do I figure out the fastest mirror again?
After you install Alpine, you may be wondering how do I figure out the fastest mirror again?
You could run <code>/sbin/setup-apkrepos</code> again.
The following script shows the closest mirror for a single ping, which might not be accurate:


{{Cat|/home/user/fastestmirror|<nowiki>
{{Cat|/home/user/fastestmirror|<nowiki>
Line 14: Line 18:
</nowiki>}}
</nowiki>}}


== See also ==


 
* [[How to setup a Alpine Linux mirror]]
This is more advanced implementation.
{{Cat|/etc/apk/fastest-mirror|<nowiki>
#!/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
 
 
</nowiki>}}
 
 
[[Category:Package Manager]]
 
 


[[Category:Package Manager]]
[[Category:Package Manager]]

Latest revision as of 08:45, 7 July 2024

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

You could run /sbin/setup-apkrepos again.

The following script shows the closest mirror for a single ping, which might not be accurate:

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

See also