Arch Linux Update Helper

https://yalneb.blogspot.com/2018/01/arch-linux-update-helper.html

Keeping your Arch Linux system up to date is a good habit, and something you should definitively be doing. It can however become time consuming (specially if you do it on a weekly basis), reason enough to postpone it for too long.

This script will guide you intuitively through the update process, tidy after it, and also optimize your pacman package manager.


Source

You can find the latest iteration of this script in my GIT repository, among many other useful bash scripts.

This script will:

  • Update your system's mirrorlists for maximum download speed
  • Update with pacman, and create a log file in your home directory. This log can be deleted and is only intended to let you review the latest updates.
  • Optimize pacman, and tidy up for the next update.

#!/bin/sh


################################################################################
## FUNCTIONS                                                             ##
################################################################################


################################################################################
## ECHO HELPERS (Inspired by Zach Holman)

printHeader ()
{
 printf "\n\n\r \033[1;37m$1\n"
}

printInfo ()
{
 printf "\r [ \033[00;34m..\033[0m ] $1\n"
}

printPromt ()
{
 printf "\r [ \033[0;33m??\033[0m ] $1"
}

printSuccess ()
{
 printf "\r\033[2K [ \033[00;32mOK\033[0m ] $1\n"
}


printWarn ()
{
 printf "\r\033[2K [ \033[00;33m!!\033[0m ] $1\n"
}

printFail ()
{
 printf "\r\033[2K [\033[0;31mFAIL\033[0m] $1\n"
 echo ''
 exit
}



################################################################################
## UPDATE MIRROLIST FOR MAXIMUM SPEED 

updateMirrorlist ()
{
 printHeader "Optimizing mirrorlist..."
 
 
 printInfo "Checking if reflector is installed"
 if [ -f /usr/bin/reflector ]; then 
  printSuccess "Reflector already installed"
 
 else 
   printInfo "Reflector not installed. Installing:\n"
   sudo pacman -Sy reflector --noconfirm --color=auto
   echo ""
   if [ -f /usr/bin/reflector ]; then 
    printSuccess "Reflector successfully installed"
   else
    printFail "Could not install reflector"
   fi
 fi
 
 
 printInfo "Creating backup mirrorlist"
 MIRRORLIST=/etc/pacman.d/mirrorlist
 BACKUP=/etc/pacman.d/mirrorlist.backup
 if [ -f $BACKUP ]; then 
  sudo rm $BACKUP # remove old backup if it exists
 fi
 sudo cp $MIRRORLIST $BACKUP
 if [ -f $BACKUP ]; then 
  printSuccess "Mirrorlist backed up to $BACKUP"
 else
  printFail "Could not backup current mirrorlist to $BACKUP"
 fi
 
 
 printInfo "Optimizing mirrorlist for maximum download speed:\n"
 sudo reflector -l 5 --verbose --sort rate --save /etc/pacman.d/mirrorlist
 echo ""
 printInfo "Refreshing package list:\n"
 sudo pacman -Syy --color=auto
 echo ""
 printSuccess "Mirrorlist updated and packages refreshed"
 
 
}


################################################################################
## UPDATE SYSTEM

updateSystem ()
{
 printHeader "Updating system..."
 
 
 printInfo "Downloading new packages"
 sudo pacman -Suwq --noconfirm --color=auto
 echo""
 
 
 LOGFILE=~/pacman.log
 printInfo "A brief pacman log will be stored at $LOGFILE"
 printInfo "Updating system:\n"
 sudo pacman -Su --noconfirm | tee $LOGFILE
 echo "" 
 

 printSuccess "System updated"
}



################################################################################
## OPTIMIZE PACMAN

optimizePacman ()
{
 local prunechace=
 local action=
 local THISTTY=$(tty)
 

 printHeader "Optimizing pacman..."
 
 
 printInfo "Removing orphan packages from the system"
 printInfo "If it complains about no targets, it means that you have no orphans:\n"
 sudo pacman -Rns --noconfirm --color=auto $(pacman -Qtdq)
 echo ""
 
 
 printPromt "Remove old packages from cache? (Not recommended) y/N: "
 exec 6<&0
 exec 0<"$THISTTY"
 read -n 1 action
 exec 0<&6 6<&-

 case "$action" in
  y )
   prunechace=true;;
  Y )
   prunechace=true;;
  * )
   prunechace=false;;
 esac
 
 if [ "$prunechace" == "true" ]; then
  printInfo "Removing uninstalled packages:\n"
  sudo pacman -Sc --noconfirm --color=auto
  echo ""
 else
  printInfo "... skipping"
 fi
 
 
 printInfo "Compacting pacman database for faster access in the future:\n"
 sudo pacman-optimize
 
 
 printSuccess "Pacman optimized" 
}




################################################################################
## MAIN                                                                  ##
################################################################################

## GREET MESSAGE
printHeader "ARCHLINUX SYSTEM-UPDATE HELPER"
printWarn "I will use super-user privileges...\n"


## GET SU PRIVILEGES
sudo -v
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &


## RUN SCRIPT
updateMirrorlist
updateSystem
optimizePacman


## GOODBY MESSAGE
printHeader "SUCCESS! SYSTEM UPDATED AND OPTIMIZED"
printWarn "But remmember to check the logs to see if you have to:"
printWarn " >>  Replace old config files with .pacnew files"
printWarn " >>  Act on any alert or warning"
printWarn "If needed, you may find a more extensive log at /var/log/pacman.log"
printWarn "Reference: https://wiki.archlinux.org/index.php/System_maintenance"
echo "\n"


### EOF ### 

No comments :

Post a Comment