Allservers.sh Script

From Manjaro Linux
Jump to: navigation, search

What Does This Script Do?

27-Nov-2016:- Cleaned up this page some, removed some
superfluous info' & added some that someone may find
useful.
The Allservers script has remained unchanged.
18-July-2016:- Added a new menu option to remove the 
db.lck file. This file obstructs an upgrade after an
interrupted/incomplete upgrade. It will save the user a
little time.
16-May-2015:- Added the highlighted link to the 
Create your own Custom Mirrorlist on the
Manjaro Wiki. In an effort to help anyone that doesn't 
know how to speed up their use of the: 
[1] Option 1. - pacman-mirrors -g command. 
14-May-2015:- Added the What about ALL of those other 
Pacman/AUR Terminal commands? section.
Which is basically all but inconsequential re. the 
allservers script, though it did just need to be said.
10-Feb-2015:- The script nolonger tests for root & uses
sudo instead, so if you have still been using an alias
that uses sudo su -c you need to remove that from
your alias.

Allservers will nolonger receive the "don't use sudo"
error from yaourt. Apart from that, this change was
required to make it compatible with pacman 4.2.
 
The other change, is that both Option [2] & Option [3]
now have the --noconfirm parameter at the end of their
command line. In use, those two script Options will
demonstrate this change.

If the autoconfirm that --noconfirm brings with it does
not suit you, please edit the allservers script to suit:- 
search for --noconfirm with your editor & remove the
two instances to bring back the need for manual
confirmation when using those two Options.

Note: CacheClean has been replaced by pkgCacheClean. See this wiki page for information on pkgCacheClean: [1]

It may possibly be worth having a read of this [2] just so that you know about the dangers of partial upgrades. Though in reality, I've not had, nor have I ever heard of anyone else having experienced such a problem due to their use of the allservers script...

  • Option [1] - Uses the pacman-mirrors -g command to test the available Manjaro repository mirrors & then ranking them in speed, overwriting your previous /etc/pacman.d/mirrorlist. After which your pacman package databases are then synchronised with the new mirrors & refreshed on your system using the pacman -Syy command.
How to create a fast Custom mirrorlist: [3]
  • Option [2] - Initially performs Option [1] & then upgrades your System using the pacman -Syu command, after which it runs pkgCacheClean via the command pkgcacheclean (see below).
  • Option [3] - Initially performs Option [1] & then upgrades your System & packages from AUR using the yaourt -Syua command, after which it runs pkgCacheClean via the command pkgcacheclean (see below).
  • Option [4] - Upgrades your System using the pacman -Syu command, after which it runs pkgCacheClean via the command pkgcacheclean (see below).
  • Option [5] - Upgrades your System using the yaourt -Syua command, after which it runs pkgCacheClean via the command pkgcacheclean -v 2 (see below).
  • Option [6] - Deletes the db.lck file using the rm /var/lib/pacman/db.lck command, after which the user should not see the unable to lock database error. (see here [4] for more information on db.lck).


Allservers.menu.6.png
  • When called, the allservers script immediately presents a menu with the five available options - as shown in the image to the left.

The menu should be self explanatory, with the likely exceptions of pkgCacheClean [5] & the --noconfirm addition to Option [2] & Option [3] (see below).


--noconfirm (from the pacman man page)

--noconfirm
Bypass any and all “Are you sure?” messages. It’s not a 
good idea to do this unless you want to run pacman from 
a script.

Allservers IS a script that the --noconfirm option works beautifully in.

pkgCacheClean

You will need to install pkgCacheClean for this script to run properly, (or you can comment it out of the script if for some reason you don't want to use pkgCacheClean).

Following is the command needed to install pkgCacheClean from the AUR:

yaourt -S pkgcacheclean 


Why pkgCacheClean?

It is widely considered good policy by many who use the Arch based, rolling release upgrade system, to keep two (or under appropriate circumstances more) versions of the installation packages that reside in the /var/cache/pacman/pkg directory.

Doing so, provides us with an easy method to downgrade out of trouble, in the event that we have just upgraded into it.

For more on this topic please read this wiki page [6].


What about ALL of those other Pacman/AUR Terminal commands?

I find that most of the content that follows in this section is now superfluous to my needs since the release of PkgBrowser [7]. This software makes it so easy to find what packages are available & where you'll find them. :)

If you much prefer to use the Terminal (I like to too, but not for everything) then you don't need me to tell you here, how to do that.

I will mention a tool that I used to use, as it allowed me to add their site search field to Firefox/Pale Moon's search field. The add-on is called Add To Search Bar: [8]

With the provided benefits that come with Add To Search Bar, I could search the AUR, the Arch package database, the Arch Rollback Machine [9], or whatever I wanted, from my browsers search bar.


The most recent addition to the allservers menu is that of the remove db.lck file command. It exists (like the rest of this script) just to save a little time getting over the uncommon situation of having had your pacman or yaourt upgrade interrupted (mains power black out or network failure being the most common reasons I expect).


#!/bin/bash
#
# 18-July-2016: Added a sixth menu option that allows the user to delete
# the db.lck file that obstructs an upgrade after an incomplete/interupted
# upgrade attempt. This option will be a little time saver every now & then. 
#
# 15-Jan-2015: Modified the script so that it is NOT called via an alias
# with sudo su -c , the script is now using sudo internally & needs 
# some testing to find if it works OK.
# Months of testing/use have now gone by - the changes are problem free.
# 22-May-2014: Changed the title name to allservers . (Big change huh?)
# 20-November-2013: Removed stuff from the menu that really next to
# no one needs to see. The menu just does its job now, nothing more or less.
#
# 17-November-2013: I've finally felt like spending the time to improve
# (imho) the colours of the menu & I've also refined it some.
#
# 10-July-2013: Now using pkgCacheClean instead of CacheClean. It uses
# pacman's version checking method.
#
# 11-June-2013: Made Option 1. available (it is useful on the odd occasion).
# Made the display more informative during the progress of each option.
#
# 13-May-2013: Removed the ability for Option 1. to be run standalone, 
# due to having been told to read this:
# https://wiki.archlinux.org/index.php/Pacman#Partial_upgrades_are_unsupported 
#
# 27-April-2013: Updated to use the new pacman-mirrors -g to rankmirrors. 
#
# allservers.sh - inspired by Manjaro's Carl & Phil, initially hung together 
# by handy, the script's display prettied up & progress information added by Phil, 
# the menu & wiki page added by handy.
# Latest revision now calls everything via the menu.
# The following wiki page is about this script: 
# http://wiki.manjaro.org/index.php/Allservers.sh_Script:-_Rankmirrors,_Synchronise_Pacman_Database
# Following wiki page will introduce pkgCacheClean & related information:
# http://wiki.manjaro.org/index.php/Maintaining_/var/cache/pacman/pkg_for_System_Safety
#___________________________________________________________
# 
# allservers (I've dropped the .sh at the end) is now completely
# menu driven. The Menu describes what it does for you, if you
# need more detail see the two wiki page links listed above.
###########################################################

err() {
   ALL_OFF="\e[1;0m"
   BOLD="\e[1;1m"
   RED="${BOLD}\e[1;31m"
    local mesg=$1; shift
    printf "${RED}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}

msg() {
   ALL_OFF="\e[1;0m"
   BOLD="\e[1;1m"
   GREEN="${BOLD}\e[1;32m"
    local mesg=$1; shift
    printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}


# The menu:

clear # Clear the screen.

echo
echo -e "\e[1;32m                             allservers"
echo
echo -e " \e[0;33m   Enter Your chosen Option's number \e[0;32m[\e[1;37m?\e[0;32m] \e[4;37mOR\e[0m\e[0;32m hit \e[0;31mReturn\e[0;32m to \e[0;31mexit\e[0;32m. "
echo
echo
echo -e "    [\e[1;37m1\e[0;32m] Rank Mirrors & update mirrorlist: \033[0mpacman-mirrors -g" 
echo -e "       \e[0;33m &\e[0;32m then sync/refresh package lists: \033[0mpacman -Syy "
echo
echo -e "   \e[0;32m [\e[1;37m2\e[0;32m] Option 1. \e[0;33mplus\e[0;32m Upgrade the System: \033[0mpacman -Syu --noconfirm "
echo -e "       \e[0;33m &\e[0;32m then run pkgCacheClean: \033[0mpkgcacheclean "
echo
echo -e "   \e[0;32m [\e[1;37m3\e[0;32m] Option 1. \e[0;33mplus\e[0;32m Upgrade the System & AUR: \033[0myaourt -Syua --noconfirm "
echo -e "       \e[0;33m &\e[0;32m then run pkgCacheClean: \033[0mpkgcacheclean "
echo
echo -e "   \e[0;32m [\e[1;37m4\e[0;32m] Upgrade the System only: \033[0mpacman -Syu "
echo -e "       \e[0;33m &\e[0;32m then run pkgCacheClean: \033[0mpkgcacheclean "
echo
echo -e "   \e[0;32m [\e[1;37m5\e[0;32m] Upgrade the System & AUR only: \033[0myaourt -Syua "
echo -e "       \e[0;33m &\e[0;32m then run pkgCacheClean: \033[0mpkgcacheclean "
echo
echo -e "   \e[0;32m [\e[1;37m6\e[0;32m] Delete \e[0;33mdb.lck\e[0;32m after interupted upgrade: \033[0mrm /var/lib/pacman/db.lck "
echo -e "       \e[0;32m this should remedy the unable to lock database error. "
echo 
echo -e "\033[1m  Enter the Number of Your Choice: \033[0m"
echo    

read option

case "$option" in
# Note variable is quoted.

 "1")
 echo
 msg "Processing mirrors:"
 echo
 sudo pacman-mirrors -g
 echo
 msg "Your mirrors have been ranked &"
 msg "the mirrorlist has now been refreshed."
 echo
 msg "Refreshing your pacman database:"
 sudo pacman -Syy
 echo
 msg "Your mirrors & package database are now synchronised."
 echo
 ;;
# Note double semicolon to terminate each option.

 "2")
 echo
 msg "Processing mirrors:"
 echo
 sudo pacman-mirrors -g
 echo
 msg "Your mirrors have been ranked &"
 msg "the mirrorlist has now been refreshed."
 echo
 msg "Refreshing your pacman database:"
 sudo pacman -Syy
 echo
 msg "Your mirrors & package database are now synchronised."
 echo
 msg "Upgrading System:"
 echo
 sudo pacman -Syu --noconfirm
 echo
 msg "System upgrade complete."
 echo
 msg "pkgCacheClean will now remove all but the 2 most "
 msg "recent versions of the installation packages in "
 msg "/var/cache/pacman/pkg directory:"
 echo
 sudo pkgcacheclean
 echo
 msg "pkgCacheClean has done its job. "
 echo
 ;;
# Note double semicolon to terminate each option.

 "3")
 echo
 msg "Processing mirrors:"
 echo
 sudo pacman-mirrors -g
 echo
 msg "Your mirrors have been ranked &"
 msg "the mirrorlist has now been refreshed."
 echo
 msg "Refreshing your pacman database:"
 sudo pacman -Syy
 echo
 msg "Your mirrors & package database are now synchronised."
 echo
 msg "Upgrading System & AUR:"
 echo
 yaourt -Syua --noconfirm
 echo
 msg "System including AUR packages are up to date."
 echo
 msg "pkgCacheClean will now remove all but the 2 most "
 msg "recent versions of the installation packages in "
 msg "/var/cache/pacman/pkg directory:"
 echo
 sudo pkgcacheclean
 echo
 msg "pkgCacheClean has done its job. "
 echo
 ;;
# Note double semicolon to terminate each option.

 "4")
 echo
 msg "Upgrading System:"
 echo
 sudo pacman -Syu
 echo
 msg "System update complete."
 echo
 msg "pkgCacheClean will now remove all but the 2 most "
 msg "recent versions of the installation packages in "
 msg "/var/cache/pacman/pkg directory:"
 echo
 sudo pkgcacheclean
 echo
 msg "pkgCacheClean has done its job. "
 echo
 ;;
# Note double semicolon to terminate each option.

 "5")
 echo
 msg "Upgrading System & AUR: "
 echo
 yaourt -Syua
 echo
 msg "System including AUR packages are up to date. "
 echo
 msg "pkgCacheClean will now remove all but the 2 most "
 msg "recent versions of the installation packages in "
 msg "/var/cache/pacman/pkg directory:"
 echo
 sudo pkgcacheclean
 echo
 msg "pkgCacheClean has done its job. "
 echo
 ;;
# Note double semicolon to terminate each option.

  "6")
 echo
 msg "About to delete /var/lib/pacman/db.lck: "
 echo
 sudo rm /var/lib/pacman/db.lck
 echo
 msg "File db.lck removed, re-attempt your intended installation. "
 echo
 ;;
# Note double semicolon to terminate each option.

esac

exit 0

Installation & Execution:

The easy way to install the allservers script, is
to enter the following command in the terminal:
sudo pacman -S allservers

You can, if you choose to use the more cumbersome method of installing this script on your computer, as follows:

Copy the text of the script off of this page & into a New file in your favourite text editor.

If you are using gedit, then when you first open it, you are given a blank page that is for creating a new file, which is exactly what we want.

Firstly use your mouse to highlight all of the allservers script in the text box above. Then select the highlighted text with your right mouse button & choose the Copy option from the drop down menu; then Paste it into the new blank gedit page by either using the right mouse button menu over the blank editor page or use the editor's Edit menu & choose the Paste option.

When you have the allservers text in your text editor save it into your /home/<user.name> directory with the name allservers like so:

/home/<user.name>/allservers

You may choose to change the path & instead use /usr/bin instead. That is where installing via pacman (or any of its GUI wrappers) will locate it.

If you install manually (why would you?) you will need to manually make the script executable. To do this, type the following into the Terminal:

chmod +x <path/to>/allservers

Once the allservers.sh script has been made executable it will run by typing the following in the Terminal:

sudo <path/to>/allservers


(optional) How to Edit Your ~/.bashrc:

Section updated: 10-Feb-2015

If you are unfamiliar with the use of the alias command in GNU/Linux, then have a look at the first post (in particular) of this wiki page [10]

I use the following alias in my ~/.bashrc (change the path to suit)

alias as="/usr/bin/allservers"

When using the above alias, all you have to do is type as at the terminal prompt to bring up the allservers menu, from which you can make choose how you want to upgrade by hitting the appropriate number, between 1 & 5.

When creating an alias or calling the script from the terminal, be aware that the name of the allservers script has two official names. Initially it was called allservers.sh (in this wiki page & the forum thread. Then when it was placed in the repo, it lost the trailing .sh . So when you call allservers, use whatever name you have used for it).

(optional) Modifying the System's Search Path:

Section updated 10-2-2015

On an associated note, I have my other scripts stored in ~/data/scripts which when calling a script located there would cause an unknown command or some such error, due to that path not existing in the default system search paths.

To remedy this I've added the path to both the root search path & my single user's too.

The following methods described here can be used for as many directories that you need or desire to be in your default search path. Usually this is done in the user account & rarely for the root (administrative) account.


(optional) Changing the Root Search Path:

To edit files for root you will need to use your favourite text editor as root user. This is done in various ways depending on the type of editor. Console-based editors (those that operate in the Terminal without a Graphic User interface - GUI) are best called with the path to the file & its name, whether you are opening an existing file or looking to create a new one, it is done like so, using nano as the example here:

For nano:

sudo nano <path/to/file-name>

gedit & the other desktop GUI-based editors will happily use the same method as those editors that are console-based, though for these desktop GUI-based text editors you can use their built in menu to open files.

The following example opens gedit for the root (administrative) user:

gksudo gedit

Adding /home/handy/data/scripts to my search path saves me having to type in that path to any of my scripts when they are called. To do this for root , I modified /etc/profile , adding my path to the end of the PATH= line, & that is all there is to it:


# Set our default path
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/handy/data/scripts"
export PATH


(optional) Changing the User Search Path:

It is far more common for someone to want to modify the path for a single user account (in my case that's handy) which is done via that user's ~/.bash_profile by adding the following to the file:


PATH=$PATH:</your/path>
export PATH


See also

  • mhwd-kern is a bash script similar to allservers, that simplifies working with Manjaro's multiple kernels: [11]


Support

If you find any errors in the article or fault with the script please post in this forum thread: [12]