cfg/bin/updmirrors

39 lines
1,012 B
Text
Raw Normal View History

#!/bin/bash
set -euo pipefail
source ${HOME}/bin/script_framework.sh
check_util rankmirrors # from pacman-contrib
outfile=/etc/pacman.d/mirrorlist
oldlist=/etc/pacman.d/mirrorlist.bak
if [ $(id -u) -eq 0 ]; then
step "Creating backup of current mirrorlist"
cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak
else
outfile=/tmp/mirrors.rank
oldlist=/etc/pacman.d/mirrorlist
warn "Not running as root, new mirrorlist will be written to ${outfile}"
fi
step "Collecting mirrors"
IFS=$'\n'
mirrors=(
$(
curl -sSfL "https://archlinux.org/mirrors/status/json/" |
jq -r '.urls[] | select(.active) | select(.completion_pct == 1.0) | select( .delay < 3600) | select(.protocol == "https") | .url'
)
)
unset IFS
step "Measuring mirror speed, searching 5 fastest mirrors of ${#mirrors[@]} mirrors"
for url in "${mirrors[@]}"; do
echo "Server = ${url}\$repo/os/\$arch"
done |
rankmirrors -n 5 - |
tee "${outfile}"
step "Generating diff from old to new mirrorlist"
diff -wu "${oldlist}" "${outfile}"