Skip to content

Instantly share code, notes, and snippets.

@Yolo-Jerome
Last active December 3, 2017 12:43
Show Gist options
  • Save Yolo-Jerome/97c3d0af23a3fd299704cbca779c2419 to your computer and use it in GitHub Desktop.
Save Yolo-Jerome/97c3d0af23a3fd299704cbca779c2419 to your computer and use it in GitHub Desktop.
Automated upgrade script for Ubuntu Servers
#!/bin/bash
#returns:
# 0 - success
# 1 - success : Requires reboot
# 2 - updated failed
# 3 - upgrade failed
# 4 - dist-upgrade failed
# 5 - autoremove failed
# 6 - autoclean failed
#
# 7 - unknown parameter
UPDATE='sudo apt update'
UPGRADE='sudo apt upgrade'
DISTUPGRADE='sudo apt dist-upgrade'
AUTOREMOVE='sudo apt autoremove'
AUTOCLEAN='sudo apt autoclean'
OPTIONS=''
# Check Parameter
if [[ $# == 1 ]]
then
if [[ $1 == '-y' ]]; then
OPTIONS='-y'
else
echo ""
echo -e "\e[91m #####################################\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m # ERROR WRONG PARAMETER! #\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m #####################################\e[39m"
echo ""
echo -e "\e[91m Unknown Option: $1\e[39m"
echo ""
exit 7
fi
fi
# First get sudo rights
sudo echo "" &> /dev/null
echo ""
echo -e "\e[92m UPDATE PACKAGE SOURCES \e[39m"
echo ${UPDATE}
echo ""
${UPDATE} ${OPTIONS}
if [[ $? != 0 ]]
then
echo ""
echo -e "\e[91m #####################################\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m # UPDATE FAILED! #\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m #####################################\e[39m"
echo ""
exit 2
fi
echo -e "\e[92m OK \e[39m"
echo ""
echo -e "\e[92m UPGRADE TO NEW PACKAGES \e[39m"
echo $UPGRADE
echo ""
${UPGRADE} ${OPTIONS}
if [[ $? != 0 ]]
then
echo ""
echo -e "\e[91m #####################################\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m # UPGRADE FAILED! #\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m #####################################\e[39m"
echo ""
exit 3
fi
echo -e "\e[92m OK \e[39m"
echo ""
echo -e "\e[92m UPGRADE DISTRIBUTION-PACKAGES \e[39m"
echo $DISTUPGRADE
echo ""
${DISTUPGRADE} ${OPTIONS}
if [[ $? != 0 ]]
then
echo ""
echo -e "\e[91m #####################################\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m # DIST-UPGRADE FAILED! #\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m #####################################\e[39m"
echo ""
exit 4
fi
echo -e "\e[92m OK \e[39m"
echo ""
echo -e "\e[92m AUTOREMOVE OLD PACKAGES \e[39m"
echo $AUTOREMOVE
echo ""
${AUTOREMOVE} ${OPTIONS}
if [[ $? != 0 ]]
then
echo ""
echo -e "\e[91m #####################################\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m # AUTOREMOVE FAILED! #\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m #####################################\e[39m"
echo ""
exit 5
fi
echo -e "\e[92m OK \e[39m"
echo ""
echo -e "\e[92m AUTOREMOVE OLD PACKAGES \e[39m"
echo ${AUTOCLEAN}
echo ""
${AUTOCLEAN} ${OPTIONS}
if [[ $? != 0 ]]
then
echo ""
echo -e "\e[91m #####################################\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m # AUTOCLEAN FAILED! #\e[39m"
echo -e "\e[91m # #\e[39m"
echo -e "\e[91m #####################################\e[39m"
echo ""
exit 6
fi
echo -e "\e[92m OK \e[39m"
clear
if [ -f /var/run/reboot-required ]; then
echo ""
echo -e "\e[92m #####################################\e[39m"
echo -e "\e[92m # #\e[39m"
echo -e "\e[92m # DONE #\e[39m"
echo -e "\e[92m # #\e[39m"
echo -e "\e[92m # \e[91mNOTE: A reboot of the System is\e[92m #\e[39m"
echo -e "\e[92m # \e[91mrequired in order to\e[92m #\e[39m"
echo -e "\e[92m # \e[91mcomplete the Update!\e[92m #\e[39m"
echo -e "\e[92m # #\e[39m"
echo -e "\e[92m #####################################\e[39m"
echo ""
exit 1
else
echo ""
echo -e "\e[92m #####################################\e[39m"
echo -e "\e[92m # #\e[39m"
echo -e "\e[92m # DONE #\e[39m"
echo -e "\e[92m # #\e[39m"
echo -e "\e[92m #####################################\e[39m"
echo ""
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment