2014-11-20 23:24:10 +10:00
|
|
|
#!/bin/bash
|
2016-06-07 20:42:19 +10:00
|
|
|
# Script Name: check_updates_deb
|
|
|
|
# Author Name: Matt McKinnon
|
|
|
|
# Date: 7th June 2016
|
|
|
|
# Description: For use on Debian Based Systems
|
|
|
|
# This script will:
|
2014-11-20 23:24:10 +10:00
|
|
|
# Clean up the local apt repository of retrieved packages (apt-get clean)
|
|
|
|
# Resync the package index (apt-get update)
|
|
|
|
# If called with AUTOUPDATE set to yes then updates will be downloaded and applied with no feed back (not recommended)
|
|
|
|
# If called without AUTOUPDATE then packages are downloaded and an email is sent informing which packages are to be updated.
|
|
|
|
# And more ;-)
|
|
|
|
# NOTE: Perl is needed for this script to work.
|
|
|
|
|
|
|
|
#
|
|
|
|
# Make user configuration changes in this section
|
|
|
|
#
|
|
|
|
|
2017-06-15 21:06:29 +10:00
|
|
|
MAILTO="support@comprofix.com"
|
|
|
|
MAILFROM="support@comprofix.com"
|
|
|
|
THISSERVER=$(hostname -f)
|
|
|
|
SMTP="mail.comprofix.com"
|
2016-03-26 21:59:06 +10:00
|
|
|
|
2014-11-20 23:24:10 +10:00
|
|
|
AUTOUPDATE="no"
|
|
|
|
LOGFILE="/var/log/server_maint.log"
|
2016-03-28 18:55:16 +10:00
|
|
|
THISSERVER=$(hostname -f)
|
2014-11-20 23:24:10 +10:00
|
|
|
|
|
|
|
#
|
|
|
|
# End of user configuration section
|
|
|
|
#
|
|
|
|
|
|
|
|
DASHES="---------------------------------------------------------------------------------"
|
|
|
|
DASHES2="================================================================================="
|
|
|
|
|
|
|
|
|
|
|
|
# Check if the script is being run as root exit if it is not.
|
|
|
|
|
2016-03-28 18:55:16 +10:00
|
|
|
if [ $(id -u) -ne 0 ]
|
|
|
|
then
|
|
|
|
echo "You need to be root to run this script."
|
|
|
|
exit 1
|
2014-11-20 23:24:10 +10:00
|
|
|
fi
|
|
|
|
|
|
|
|
startlogging() {
|
|
|
|
echo $DASHES2 >> $LOGFILE
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$0 started running at $(date)" >> $LOGFILE
|
2014-11-20 23:24:10 +10:00
|
|
|
echo $DASHES2 >> $LOGFILE
|
|
|
|
}
|
|
|
|
|
|
|
|
stoplogging() {
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$(date) [MESSAGE] $0 finished runnning" >> $LOGFILE
|
2014-11-20 23:24:10 +10:00
|
|
|
echo $DASHES >> $LOGFILE
|
|
|
|
}
|
|
|
|
|
|
|
|
check_return() {
|
|
|
|
if [ "$?" -ne "0" ]
|
|
|
|
then
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$(date) [ERROR] $1 failed to run" >> $LOGFILE
|
2014-11-20 23:24:10 +10:00
|
|
|
send_error_email $1
|
|
|
|
stoplogging
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$(date) [SUCCESS] $1 ran without error" >> $LOGFILE
|
2014-11-20 23:24:10 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
send_error_email() {
|
2017-06-15 21:06:29 +10:00
|
|
|
sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "[$THISSERVER] There was an error whilst running $0" -m "
|
|
|
|
|
|
|
|
Hello,
|
2014-11-20 23:24:10 +10:00
|
|
|
|
|
|
|
Whilst running the update script ($0) on $THISSERVER there was a problem.
|
|
|
|
|
|
|
|
[ERROR] "$1" failed to run
|
|
|
|
|
2016-03-28 18:55:16 +10:00
|
|
|
Please log in via ssh (e.g. ssh root@$(hostname -f)) and check the log file:
|
2014-11-20 23:24:10 +10:00
|
|
|
|
|
|
|
vim $LOGFILE
|
|
|
|
|
2017-04-04 23:49:53 +10:00
|
|
|
Regards." -q
|
2016-03-28 18:55:16 +10:00
|
|
|
|
2017-04-04 23:49:53 +10:00
|
|
|
}
|
2014-11-20 23:24:10 +10:00
|
|
|
|
|
|
|
startlogging
|
|
|
|
|
|
|
|
apt-get clean > /dev/null
|
|
|
|
check_return "apt-get clean"
|
|
|
|
|
|
|
|
apt-get update > /dev/null
|
|
|
|
check_return "apt-get update"
|
|
|
|
|
|
|
|
if [[ "$AUTOUPDATE" == "yes" ]]
|
|
|
|
then
|
|
|
|
apt-get -yqq upgrade > /dev/null
|
|
|
|
check_return "apt-get -yq upgrade"
|
|
|
|
else
|
2016-06-07 20:42:19 +10:00
|
|
|
PACKAGES_TO_BE_UPGRADED=$(apt-get -Vs upgrade | perl -ne 'print if /upgraded:/ .. /upgraded,/')
|
2014-11-20 23:24:10 +10:00
|
|
|
apt-get -yqd upgrade > /dev/null
|
|
|
|
check_return "apt-get -yqd upgrade"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z $PACKAGES_TO_BE_UPGRADED ]]
|
|
|
|
then
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$(date) [MESSAGE] No packages need updating." >> $LOGFILE
|
2014-11-20 23:24:10 +10:00
|
|
|
else
|
|
|
|
|
|
|
|
echo "
|
|
|
|
Hello,
|
|
|
|
|
|
|
|
Packages have been downloaded onto $THISSERVER.
|
|
|
|
|
|
|
|
$PACKAGES_TO_BE_UPGRADED
|
|
|
|
|
2016-03-28 18:55:16 +10:00
|
|
|
To update the server log in via ssh (e.g. ssh root@$(hostname -f)) and run the following command:
|
2014-11-20 23:24:10 +10:00
|
|
|
|
|
|
|
apt-get upgrade
|
|
|
|
|
|
|
|
See the logfile for more info: vim $LOGFILE
|
|
|
|
|
|
|
|
Regards. " >/tmp/servermail.msg
|
|
|
|
|
2017-06-15 21:06:29 +10:00
|
|
|
sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "[$THISSERVER] server may need some updates applied" -m "$(cat /tmp/servermail.msg)" -q
|
2017-03-14 19:23:02 +10:00
|
|
|
|
2014-11-20 23:24:10 +10:00
|
|
|
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$(date) [MESSAGE] Packages need updating email sent to $MAILTO" >> $LOGFILE
|
2014-11-20 23:24:10 +10:00
|
|
|
fi
|
|
|
|
|
|
|
|
stoplogging
|
|
|
|
exit 0
|