You are not logged in.

#1 2017-07-12 21:24:48

swatquest
Member
Registered: 2013-01-07
Posts: 30

Simple mirror list updater [script]

Simple mirror list updater

Works with the all file
https://www.archlinux.org/mirrorlist/all/

Need the packages:
- curl
- pv
- rankmirrors

updatemirrorlist.jpg

#!/bin/bash
# Simple mirror list updater
# swatquest

declare -rx NAMESCRIPT=${0##*/} # name script
declare -rx curl="/usr/bin/curl" # command curl
declare -rx pv="/usr/bin/pv" # command pv
declare -rx rankmirrors="/usr/bin/rankmirrors" # command rankmirrors

if [ ! -x "$curl" ]; then
 printf "$NAMESCRIPT: The curl command is not available.\n" >&2
 exit 192
fi

if [ ! -x "$curl" ]; then
 printf "$NAMESCRIPT: The pv command is not available.\n" >&2
 exit 192
fi

if [ ! -x "$curl" ]; then
 printf "$NAMESCRIPT: The rankmirrors command is not available.\n" >&2
 exit 192
fi

[ "$UID" != 0 ] && SU=sudo

# Get all site mirrorlist
curl -s  https://www.archlinux.org/mirrorlist/all/ -o /tmp/all

# Check if you are online
if [[  $? != 0 ]]; then
echo "Not connected";
exit 0
fi

countries() {
   cat /tmp/all | grep "^##" | sed -r -n '6,$ s/([^*]+).*/\0/p' | sed 's/^## //g'
}

# Create list of countries
countries | nl -s" "| column 

QTD=`countries | wc -l`;
let "QTD2=$QTD +1"

while read -p "Choose countries [default = all]:" -a COUNTRIES ; do
  for a in ${COUNTRIES[@]}; do
   [[ $a -ge $QTD ]] && break
  done
   [[ $COUNTRIES = [a-z]* ]] && continue
   [[ $a -lt $QTD2 ]] && break
done

# Save and change IFS
OLDIFS=$IFS
IFS=$'\n'

CAPTURE=($(for i in "${COUNTRIES[@]}"; do countries | awk "NR == $i {print; exit}";done))

# Restore IFS
IFS=$OLDIFS 

counter=${#CAPTURE[@]}

if [[ ${counter} -eq 0 ]]; then
  cat /tmp/all  > /tmp/all2 
else
  for (( i=0; i<${counter}; i++ )); do  cat /tmp/all  | sed -r -n "/${CAPTURE[$i]}/,/^$/p";done > /tmp/all2 
fi

# Remove all file
rm /tmp/all

# Uncomment mirrors
   echo "Uncommenting mirrors."
   sed '/^#S/ s|#||' -i "/tmp/all2"

# Apply rankmirrors
   echo -e "Attention: \n It is recommended to use the rankmirrors because it determines the best connection."
   while read -p "Apply rankmirrors? [Y/n]" RANK ; do
     if [[ $RANK = "" ]]; then
       RANK="Y"; break
     elif [[ $RANK = [YyNn] ]]; then
       break
     fi
    done
     if [[ $RANK = [Yy] ]]; then
       while read -p "How many servers? [Default 6, 0 for all, max 999]" AMOUNT ; do
          if [[ $AMOUNT = "" ]]; then
              AMOUNT="6"; break
          elif  [[ $AMOUNT = [0-9] ]]; then
            break
          elif  [[ $AMOUNT = [0-9][0-9] ]]; then
            break          
          elif  [[ $AMOUNT = [0-9][0-9][0-9] ]]; then
            break
           fi
       done
       echo "Running rankmirrors."
       echo "The process may take a long time."
   	 rankmirrors -n $AMOUNT "/tmp/all2" | pv -t > /tmp/mirrorlist
         rm /tmp/all2
   	 echo "Rankmirrors finished."
     elif [[ $RANK = [Nn] ]]; then
   	 echo "Rankmirrors not executed."
     fi

# Backup and copy of file
   echo "If necessary, the sudo command will be used."
   echo "A mirrorlist backup will be made to /etc/pacman.d/ with the name \"mirrolist.bkp\"."
   while read -p "You want to move the file to the \"/etc/pacman.d/\"? [Y/n]" COPY ; do
     if [[ $COPY = "" ]]; then
       COPY="Y"; break
     else  	   
       [[ $COPY = [YyNn] ]] && break
     fi
    done
     if [[ $COPY = [Yy] ]]; then
       echo "Creating Backup."
       $SU cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bkp
        if [[ -f /tmp/all2 ]]; then
	  echo "Setting permission to 755."
          chmod 755 /tmp/all2
	  echo "Modifying owner and group for root." 
          $SU chown root:root /tmp/all2
   	  echo "Moving file."
   	  $SU mv /tmp/all2 /etc/pacman.d/mirrorlist
	elif [[ -f /tmp/mirrorlist ]]; then
	  echo "Setting permission to 755."
          chmod 755 /tmp/mirrorlist
	  echo "Modifying owner and group for root." 
          $SU chown root:root /tmp/mirrorlist 
   	  echo "Moving file."
   	  $SU mv /tmp/mirrorlist /etc/pacman.d/mirrorlist
	fi
     elif [[ $COPY = [Nn] ]]; then
       echo "File not copied."
       echo "Temporary files are in the folder\""/tmp/"\""
       echo -e "With rankmirrors: mirrorlist\nNo rankmirrors: all2" 
       exit 0
      fi

# Delete file
   while read -p "Do you want to delete the backup file or restore the mirrolist?? [D/r]" DELBACKUP ;
   do
     if [[ $DELBACKUP = "" ]]; then
       DELBACKUP="D"; break
     else  	   
       [[ $DELBACKUP = [DdRr] ]] && break
     fi
   done
     if [[ $DELBACKUP = [Dd] ]]; then
       echo "Deleting the backup file in \"/etc/pacman.d/\"."
       $SU rm /etc/pacman.d/mirrorlist.bkp
     elif [[ $DELBACKUP = [Rr] ]]; then
       echo "Restoring the backup file in \"/etc/pacman.d/\"."
       $SU mv /etc/pacman.d/mirrorlist.bkp /etc/pacman.d/mirrorlist
     fi

exit 0

Last edited by swatquest (2017-07-12 23:49:00)

Offline

#2 2017-07-12 21:58:12

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Simple mirror list updater [script]

Honestly, why not just use reflector which is in the repos, rather than reinvent the same thing in bash?

Edit: Also, your script contains a pacman -Syy invocation which automatically disqualifies it from use.

Last edited by eschwartz (2017-07-12 22:00:48)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#3 2017-07-12 22:25:21

swatquest
Member
Registered: 2013-01-07
Posts: 30

Re: Simple mirror list updater [script]

ok. thanks.

I modified pacman -Syy to pacman -Sy

Offline

#4 2017-07-12 22:34:00

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Simple mirror list updater [script]

That... didn't actually fix anything. At all.
https://wiki.archlinux.org/index.php/Sy … nsupported

Do not ever run pacman -Sy to update the database without also running pacman -Su to upgrade all packages. It doesn't really matter whether you do a hard -Syy to ignore server timestamps or a soft -Sy to download newer versions of the databases only. Just don't do it.

I'm not really sure why you think a mirrorlist will break in the two minutes since the server generated it and the 1.5 minutes since you used rankmirrors, but it's still not a good idea...

Last edited by eschwartz (2017-07-12 22:35:50)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#5 2017-07-12 23:49:45

swatquest
Member
Registered: 2013-01-07
Posts: 30

Re: Simple mirror list updater [script]

ok.

Removed pacman -Sy

Offline

Board footer

Powered by FluxBB