2023-05-29 19:31:13 -05:00
|
|
|
#!/bin/env bash
|
|
|
|
|
|
|
|
# This is a simple script to fetch and update Arch and EndeavourOS mirrors
|
|
|
|
# using rate-mirrors.
|
2023-05-30 21:31:59 -05:00
|
|
|
# Syntax get-best-mirrors.sh <arch|eos|both> (nothing means both)
|
|
|
|
# I run it weekly using anacron.
|
|
|
|
|
|
|
|
update_arch() {
|
2023-06-01 16:44:22 -05:00
|
|
|
# Create temporary file to output to
|
|
|
|
TMPFILE_ARCH="$(mktemp)"
|
|
|
|
|
2023-05-30 21:31:59 -05:00
|
|
|
# Rank Arch mirrors
|
|
|
|
echo "===================================================================================================="
|
|
|
|
echo "Ranking Arch mirrors"
|
|
|
|
echo "===================================================================================================="
|
|
|
|
|
|
|
|
rate-mirrors --protocol=https --save=$TMPFILE_ARCH arch
|
2023-06-01 16:44:22 -05:00
|
|
|
|
|
|
|
# Create backup directory if not present already
|
|
|
|
sudo mkdir -p /etc/pacman.d/mirrorlist-backup
|
|
|
|
|
|
|
|
# Place the new mirrorlist and do a backup
|
2023-05-30 21:31:59 -05:00
|
|
|
sudo mv /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist-backup/mirrorlist.$TIME
|
|
|
|
sudo mv $TMPFILE_ARCH /etc/pacman.d/mirrorlist
|
|
|
|
}
|
|
|
|
|
|
|
|
update_eos() {
|
2023-06-01 16:44:22 -05:00
|
|
|
# Create temporary file to output to
|
|
|
|
TMPFILE_EOS="$(mktemp)"
|
2023-05-30 21:31:59 -05:00
|
|
|
|
|
|
|
# Rank EndeavourOS mirrors
|
|
|
|
echo $'\n'"===================================================================================================="
|
|
|
|
echo "Ranking EndeavourOS mirrors"
|
|
|
|
echo "===================================================================================================="
|
|
|
|
|
|
|
|
rate-mirrors --protocol=https --save=$TMPFILE_EOS endeavouros
|
2023-06-01 16:44:22 -05:00
|
|
|
|
|
|
|
# Create backup directory if not present already
|
|
|
|
sudo mkdir -p /etc/pacman.d/mirrorlist-backup
|
|
|
|
|
|
|
|
# Place the new mirrorlist and do a backup
|
2023-05-30 21:31:59 -05:00
|
|
|
sudo mv /etc/pacman.d/endeavouros-mirrorlist /etc/pacman.d/mirrorlist-backup/endeavouros-mirrorlist.$TIME
|
|
|
|
sudo mv $TMPFILE_EOS /etc/pacman.d/endeavouros-mirrorlist
|
2023-05-29 19:31:13 -05:00
|
|
|
|
2023-06-01 16:44:22 -05:00
|
|
|
# Remove files created by eos-rankmirror, if any
|
|
|
|
sudo find /etc/pacman.d/ -maxdepth 1 -type f -name "endeavouros-mirrorlist\.*" -exec rm -v {} \;
|
|
|
|
}
|
2023-05-29 19:31:13 -05:00
|
|
|
|
|
|
|
# Get the current time
|
|
|
|
TIME="$(date '+%Y.%m.%d-%H.%M.%S')"
|
|
|
|
|
2023-05-30 21:31:59 -05:00
|
|
|
if [ "$1" == "arch" ]; then
|
|
|
|
update_arch
|
|
|
|
elif [ "$1" == "eos" ]; then
|
|
|
|
update_eos
|
|
|
|
else
|
|
|
|
update_arch
|
|
|
|
update_eos
|
|
|
|
fi
|
2023-05-29 19:31:13 -05:00
|
|
|
|
|
|
|
# Do some cleanup, keep only the last 3 backups
|
|
|
|
echo $'\n'"===================================================================================================="
|
|
|
|
echo "Cleaning up old backups to keep only the latest 3"
|
|
|
|
|
|
|
|
find /etc/pacman.d/mirrorlist-backup/ -maxdepth 1 -type f -name "mirrorlist\.[[:digit:]]*" -printf '%Ts\t%P\n' |
|
|
|
|
sort -rn |
|
|
|
|
tail -n +4 |
|
|
|
|
cut -f2- |
|
|
|
|
sudo xargs -r -I {} rm -v "/etc/pacman.d/mirrorlist-backup/{}"
|
|
|
|
find /etc/pacman.d/mirrorlist-backup/ -maxdepth 1 -type f -name "endeavouros-mirrorlist\.[[:digit:]]*" -printf '%Ts\t%P\n' |
|
|
|
|
sort -rn |
|
|
|
|
tail -n +4 |
|
|
|
|
cut -f2- |
|
|
|
|
sudo xargs -r -I {} rm -v "/etc/pacman.d/mirrorlist-backup/{}"
|
|
|
|
|
|
|
|
echo "Done!"
|