Skip to content

Instantly share code, notes, and snippets.

@daimaou92
Created May 28, 2020 10:50
Show Gist options
  • Save daimaou92/b25ab34aa67632b956f5c3f32d6664a0 to your computer and use it in GitHub Desktop.
Save daimaou92/b25ab34aa67632b956f5c3f32d6664a0 to your computer and use it in GitHub Desktop.
#!/bin/bash
NUM_SERVERS=$1
if [ -z "$NUM_SERVERS" ]; then
NUM_SERVERS=20
fi
if [ ! -f "/etc/arch-release" ]; then
echo "This script should only run on arch linux"
exit 1
fi
set -e
INSTALLED=`sudo pacman -Qi pacman-contrib | grep "Name"`
if [ -z "INSTALLED" ]; then
echo "Installing pacman-contrib"
sudo pacman -Sy --noconfirm pacman-contrib
fi
echo "Fetching current mirrors.."
curl "https://www.archlinux.org/mirrorlist/all/" | grep "Server =" | sed 's/.*Server =/Server =/g' > /tmp/mirrorlist
echo -e "\nRanking mirrors. This might take some time so make and have a coffee or something"
MIRRORS=`rankmirrors -n $NUM_SERVERS /tmp/mirrorlist`
echo "Backing up existing mirrorlist"
sudo cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.backup
echo "Updating mirrorlist"
echo -e "$MIRRORS" | sudo tee /etc/pacman.d/mirrorlist > /dev/null 2>&1 || { \
sudo cp /etc/pacman.d/mirrorlist.backup /etc/pacman.d/mirrorlist; \
echo "mirror list updation failed. Restored backup"; \
exit 1; \
}
echo "Cleaning"
rm /tmp/mirrorlist
unset NUM_SERVERS
unset INSTALLED
unset MIRRORS
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment