Skip to content

Instantly share code, notes, and snippets.

@jcanfield
Forked from bmatthewshea/system_update.sh
Created February 14, 2023 02:44
Show Gist options
  • Save jcanfield/f31e5d6b88fdb6fae59d42cb6910f8ca to your computer and use it in GitHub Desktop.
Save jcanfield/f31e5d6b88fdb6fae59d42cb6910f8ca to your computer and use it in GitHub Desktop.
Ubuntu-Debian APT upgrade script
#!/bin/bash
#
# Brady Shea - 18SEP2020 - conversion of system_update alias to bash script
# https://www.holylinux.net/
#
# Place this script in "/usr/local/sbin/system_update" or similar location under your $PATH
# It needs root permissions (SUDO) to execute.
#
# Change these settings to your liking:
#################################
noprompting=true # ( true | false ) (APT option: -y, --yes, --assume-yes)
sleepy=5 # Pause a bit between autoremoves and updates
bootpartitionwarning=102400 # 102400 1kb blocks (~100MiB) is a reasonable amount
#################################
# STATIC VARIABLES #
BRIGHT_RED=$(tput bold)$(tput setaf 1)
BRIGHT_GREEN=$(tput bold)$(tput setaf 2)
GREEN=$(tput setaf 2)
BRIGHT_YELLOW=$(tput bold)$(tput setaf 3)
BRIGHT_WHITE=$(tput bold)$(tput setaf 7)
COLOR_OFF=$(tput sgr0)
scriptname=`basename "$0"`
rebootfileflag=/var/run/reboot-required
# OTHER VARIABLES #
noprompt=
if [[ $noprompting == "true" ]]; then
noprompt=-y
fi
rebootreq=0
bootavail=0
problems=0
# FUNCTIONS #
root_user () { [ ${EUID:-$(id -u)} -eq 0 ]; }
## No point going any further in script if not sudo:
if ! root_user; then printf "\nPlease use: \'sudo ${scriptname}\'\nExiting.\n\n"; exit; fi
sleeptimer () {
for (( count=1; count<=$sleepy; count++ )); do
printf "${BRIGHT_WHITE}.${COLOR_OFF}"; sleep 1;
done
printf "\n"
}
rebootcheck () {
rebootreq="false"
if [ -f $rebootfileflag ]; then
rebootreq="true"
fi
}
okaynookay () {
showalert="${GREEN}(PASS)${COLOR_OFF}"
if [ $problems == "1" ]; then
showalert="${BRIGHT_RED}(FAIL)${COLOR_OFF}"
fi
problems=0
}
# MAIN PROGRAM #
printf "\n${BRIGHT_YELLOW}SYSTEM UPDATE STARTED${COLOR_OFF}\n\n"
printf " ${BRIGHT_WHITE}Current Distribution:${COLOR_OFF} %b %b %b\n" $(lsb_release -sd)
bootavail=$(df --output=avail /boot | tail -n 1)
if [[ $bootavail -lt bootpartitionwarning ]]; then
problems=1; okaynookay
displaybootavail="${bootavail} ${showalert}"
else
problems=0; okaynookay
displaybootavail="${bootavail} ${showalert}"
fi
printf " ${BRIGHT_WHITE}Available /boot space:${COLOR_OFF} %s %s\n" $displaybootavail
rebootcheck ## (pre update)
if [[ $rebootreq == "true" ]]; then
problems=1; okaynookay;
displayrebootreq="${rebootreq} ${showalert}"
else
problems=0; okaynookay;
displayrebootreq="${rebootreq} ${showalert}"
fi
printf " ${BRIGHT_WHITE}Reboot required currently:${COLOR_OFF} %s %s\n" $displayrebootreq
if [[ $rebootreq == "true" || $bootavail -lt $bootpartitionwarning ]]; then
while true; do
read -p " $(echo -e "You have warnings. Proceed anyway? [y/N] ")" userinput
case $userinput in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) exit;;
esac
done
fi
# START UPDATE #
printf "\n${BRIGHT_WHITE}Updating from sources..${COLOR_OFF}\n"
apt update
printf "\n${BRIGHT_WHITE}Running autoremove before updates..${COLOR_OFF}\n"
apt ${noprompt} autoremove
printf "\n${BRIGHT_WHITE}Autoremove completed. Sleeping ${sleepy} seconds${COLOR_OFF}"; sleeptimer
printf "\n${BRIGHT_WHITE}Installing any upgrades found..${COLOR_OFF}\n"
apt ${noprompt} upgrade
printf "\n${BRIGHT_WHITE}Updates have completed. Sleeping ${sleepy} seconds${COLOR_OFF}"; sleeptimer
printf "\n${BRIGHT_WHITE}Re-running autoremove post-updates..${COLOR_OFF}\n"
apt ${noprompt} autoremove
rebootcheck ## (post update)
if [[ $rebootreq == "true" ]]; then
printf "\n${BRIGHT_RED} *** A system reboot is required. ***${COLOR_OFF}\n"
else
printf "\n${GREEN} A system reboot is NOT required at this time.${COLOR_OFF}\n"
fi
printf "\n${BRIGHT_YELLOW}SYSTEM UPDATE COMPLETED. Exiting.${COLOR_OFF}\n\n"
@jcanfield
Copy link
Author

Documentation via @bmatthewshea

A 'clean' way to do apt updates on debian/ubuntu/etc.. with autoremove.
I have used something similar to this for years and have never had a problem with updates/upgrades.
Using Script

It needs root permissions (`sudo`) to execute.
Place this script in `/usr/local/sbin/system_update` (or similar location under your `$PATH`)

Permissions:
$ sudo chmod 755 /usr/local/sbin/system_update

Execute:
$ sudo system_update

I have added a sudo check, a reboot check (pre and post update) and a /boot space check as well (Updated 19JUN2021):

Documentation Image

Failure(s) will look like this:

Documentation Image

A failure is either low /boot space, or a reboot is needed (less than 100MiB default - see bootpartitionwarning in adjustable variables).
These should probably be corrected before proceeding. You can of course continue the update, but you were warned.

Upcoming:
So, I will be posting my updated version of this script that will include updates/upgrades to other package managers for linux. I might post a script for MacOS as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment