Skip to content

Instantly share code, notes, and snippets.

@vigindian
Created April 27, 2023 05:43
Show Gist options
  • Save vigindian/19e10d983a2d5d36079f048e2a24ed03 to your computer and use it in GitHub Desktop.
Save vigindian/19e10d983a2d5d36079f048e2a24ed03 to your computer and use it in GitHub Desktop.
Ubuntu update/upgrade scripts
#!/usr/bin/env bash
##########################################################################################################
#
# Ubuntu OS Patching
#
# VN
# v1.0 202010
#
# Upgrade packages: $0 -uU
# Fix dependency issues: $0 -d
# Check packages to be installed: $0 -c
# Cleanup cache of old packages: $0 -C
# Delete obsolete packages: $0 -D
#
# References:
# https://askubuntu.com/questions/81585/what-is-dist-upgrade-and-why-does-it-upgrade-more-than-upgrade
#
##########################################################################################################
##############
# FUNCTIONS
##############
# --------------------------------------------------------------------------- #
function msg()
{
echo "(I) " $*
}
# --------------------------------------------------------------------------- #
function err()
{
echo "(E) " $*
exit 1
}
# --------------------------------------------------------------------------- #
function debug()
{
if [ $DEBUG -gt 0 ]; then
echo "(D) " $*
fi
}
# --------------------------------------------------------------------------- #
function update()
{
apt-get update
}
# --------------------------------------------------------------------------- #
function upgradeAll()
{
apt-get upgrade -y
}
# --------------------------------------------------------------------------- #
#intelligently handles changing dependencies with new versions of packages; ref https://askubuntu.com/questions/81585/what-is-dist-upgrade-and-why-does-it-upgrade-more-than-upgrade
function distupgrade()
{
apt-get dist-upgrade -y
}
# --------------------------------------------------------------------------- #
function check()
{
/usr/lib/update-notifier/apt-check --human-readable
}
# --------------------------------------------------------------------------- #
function clean()
{
apt-get clean
}
# --------------------------------------------------------------------------- #
#remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed
function deleteobsolete()
{
apt-get autoremove -y
}
# --------------------------------------------------------------------------- #
function usage() {
echo "Usage: $0 [-h] [-uUdDcC]"
echo -e "$0 \n [-h]: Prints this help message\n [-u]: Update the package list \n [-U]: Upgrade all packages \n [-d]: handle changing dependencies with new versions of packages \n [-D]: uninstall obsolete packages\n [-c]: check number of packages to upgrade\n [-C]: clean the apt cache"
exit 1
}
# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
#########
# MAIN
#########
#script needs elevated privileges
if [ ${UID} != 0 ];then
err "script should be run as root or sudo"
fi
#process input
while getopts ":huUcCdD" opt; do
case ${opt} in
u)
option="update"
echo -e "\ngiven option is $option"
update
#check
;;
U)
option="upgrade"
echo -e "\ngiven option is $option"
check
upgradeAll
;;
c)
option="check"
echo -e "\ngiven option is $option"
check
;;
C)
option="clean"
echo -e "\ngiven option is $option"
clean
;;
d)
option="distupgrade"
echo -e "\ngiven option is $option"
distupgrade
;;
D)
option="deleteobsolete"
echo -e "\ngiven option is $option"
deleteobsolete
;;
h | *)
#echo "Incorrect syntax"
usage
;;
esac
done
shift $((OPTIND -1))
if [ -z "${option}" ];then
echo "ERROR: Any one option mandatory!"
usage
fi
echo
#!/bin/bash
########################
# Ubuntu Upgrade Script
########################
#ensure system is updated
apt update
apt upgrade -y
#handles changing dependencies with new versions of packages
apt dist-upgrade
#remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed
apt-get autoremove
#manage release upgrades
apt install -y update-manager-core
#if the new OS is listed in http://changelogs.ubuntu.com/meta-release-lts, upgrade will work; or else to upgrade with "-d" option to force upgrade to dev release
#silent-upgrade ("yes" to all prompts)
do-release-upgrade -f DistUpgradeViewNonInteractive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment