Last active
November 7, 2023 17:33
-
-
Save lox/9152137 to your computer and use it in GitHub Desktop.
A bash script to select the fastest ubuntu apt geo-mirror
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# mirror_test.sh | |
# benchmarks closest ubuntu mirrors and outputs them in speed order | |
# bash -c "$(curl -fsSL https://gist.githubusercontent.com/lox/9152137/raw/mirror_test.sh)" | |
RELEASE=$(lsb_release -c -s 2>/dev/null || echo 14.04) | |
MIRRORS=$(curl -s http://mirrors.ubuntu.com/mirrors.txt) | |
TESTFILE="dists/$RELEASE/main/binary-amd64/Packages.bz2" | |
TIMEOUT=1 | |
SAMPLES=3 | |
BYTES=511999 #1mb | |
usage() { | |
cat << EOF | |
usage: $0 options | |
Tests out close by mirrors and prints them in speed order | |
OPTIONS: | |
-h Show this message | |
-s <n> Number of samples to take (default $SAMPLES) | |
EOF | |
} | |
# tests the provided mirror, outputs times | |
function test_mirror() { | |
for s in $(seq 1 $SAMPLES) ; do | |
time=$(curl -r 0-$BYTES --max-time $TIMEOUT --silent --output /dev/null --write-out %{time_total} ${1}${TESTFILE}) | |
if [ "$TIME" == "0.000" ] ; then exit 1; fi | |
echo $time | |
done | |
} | |
# tests all the mirrors, outputs mirror and average time | |
function test_all_mirrors() { | |
for MIRROR in $MIRRORS; do | |
mean=$(mean $(test_mirror $MIRROR)) | |
if [ "$mean" != "-nan" ] ; then | |
printf '%-60s %.5f\n' $MIRROR $mean | |
else | |
printf '%-60s failed, ignoring\n' $MIRROR 1>&2 | |
fi | |
done; | |
} | |
# calculates the mean of all arguments | |
function mean() { | |
len=$# | |
echo $* | tr " " "\n" | sort -n | head -n $(((len+1)/2)) | tail -n 1 | |
} | |
# parse arguments | |
while getopts "hs:" OPTION | |
do | |
case $OPTION in | |
s) SAMPLES=$OPTARG ;; | |
h) usage; exit 1 ;; | |
?) usage; exit ;; | |
esac | |
done | |
# print mirrors in order of time | |
test_all_mirrors | sort -n -k 2 | grep http |
Useful for updating the mirror your apt-cacher-ng is using
bash -c "$(curl -fsSL https://gist.githubusercontent.com/lox/9152137/raw/mirror_test.sh)" | head -n1 | awk '{ print $1 }' | sudo tee /etc/apt-cacher-ng/backends_ubuntu
Wonderful script. These days the HTTP request to http://mirrors.ubuntu.com/mirrors.txt seems to be geo-located already (according to the source IP - tested with tor).
See https://eomwandho.wordpress.com/2014/03/20/ubuntu-apt-get-select-best-server-using-apt-get-mirrors/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From australia: