Created
October 4, 2011 15:10
-
-
Save alxrogan/1261904 to your computer and use it in GitHub Desktop.
ubuntu auto-update script to keep systems up to date on patches/etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script Name: auto_update.sh | |
# Original Author Name: Keith Bawden | |
# Modified by : Aaron | |
# Date: Wed May 17 15:40:32 JST 2006 | |
# Updated: Thur Feb 01, 2007 | |
# Description: This script will: | |
# 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. | |
# NOTE: A custom header is added to every email send to: | |
# X-Maint: ERROR and X-Maint: SUCCESS | |
# NOTE: Standard and detailed logging added, as well as e-mail notifications | |
# for any update activity | |
# | |
# Make user configuration changes in this section | |
# | |
export [email protected] | |
MAILTO="aaron" | |
AUTOUPDATE="yes" | |
LOGFILE="/var/log/auto_update.log" | |
MAILFILE="/var/log/auto_update_detail.log" | |
THISSERVER=`hostname --fqdn` | |
# | |
# End of user configuration section | |
# | |
DASHES="---------------------------------------------------------------------------------" | |
DASHES2="=================================================================================" | |
# Check if the script is being run as root exit if it is not. | |
if [ "$UID" -ne "0" ] | |
then | |
echo "[ERROR] This script must be run as root" | |
exit 1 | |
fi | |
function start_logging { | |
echo $DASHES2 >> $LOGFILE | |
echo "$0 started running at `date`" > $LOGFILE | |
echo $DASHES2 >> $LOGFILE | |
} | |
function stoplogging { | |
echo "`date` [MESSAGE] $0 finished runnning" >> $LOGFILE | |
echo $DASHES >> $LOGFILE | |
} | |
function check_return { | |
if [ "$?" -ne "0" ] | |
then | |
echo "`date` [ERROR] $1 failed to run" >> $LOGFILE | |
send_error_email $1 | |
stoplogging | |
exit 1 | |
fi | |
echo "`date` [SUCCESS] $1 ran without error" >> $LOGFILE | |
} | |
function send_error_email { | |
mail -s "[$THISSERVER] There was an error whilst running $0" -a "X-Maint: ERROR" $MAILTO <<EOF | |
Hello, | |
Whilst running the update script ($0) on $THISSERVER there was a problem. | |
[ERROR] "$1" failed to run | |
The server has the following network interfaces configured ${SERVERADDS[@]}. | |
Please log in via ssh (e.g. ssh root@${IPADDR[0]}) and check the log file: | |
vim $LOGFILE | |
Regards. | |
EOF | |
} | |
# IP Address stuff | |
declare -a IPADDR | |
declare -a NICINTERFACE | |
declare -a SERVERADDS | |
index=0 | |
for i in $( ifconfig | grep 'inet addr' | awk '{print $2}'| sed 's#addr:##g' ); | |
do | |
IPADDR[$index]=$i | |
let "index += 1" | |
done | |
index=0 | |
for i in $( ifconfig | grep 'eth' | awk '{print $1}' ); | |
do | |
SERVERADDS[$index]="$i ${IPADDR[$index]}" | |
let "index += 1" | |
done | |
# End IP Address stuff | |
start_logging | |
apt-get clean > /dev/null | |
check_return "apt-get clean" | |
apt-get update > /dev/null | |
check_return "apt-get update" | |
if [[ "$AUTOUPDATE" == "yes" ]] | |
then | |
PACKAGES_TO_BE_UPGRADED=`apt-get -Vs upgrade | perl -ne 'print if /upgraded:/ .. /upgraded,/'` | |
if [[ -z $PACKAGES_TO_BE_UPGRADED ]] | |
then | |
echo "`date` [MESSAGE] No packages need updating." >> $LOGFILE | |
mail -s "[$THISSERVER] does not need upgrading on `date +%m-%d-%y`" $MAILTO <<EOF | |
On `date` [$THISSERVER] checked for updates, the status was | |
`cat $LOGFILE` | |
. | |
EOF | |
else | |
apt-get -yq upgrade > $MAILFILE | |
check_return "apt-get -yq upgrade" | |
mail -s "[$THISSERVER] has been updated on `date +%m-%d-%y`" $MAILTO <<EOF | |
On `date` [$THISSERVER] was upgraded with the following packages | |
$PACKAGES_TO_BE_UPGRADED | |
The status was | |
`cat $MAILFILE` | |
`cat $LOGFILE` | |
. | |
EOF | |
fi | |
else | |
PACKAGES_TO_BE_UPGRADED=`apt-get -Vs upgrade | perl -ne 'print if /upgraded:/ .. /upgraded,/'` | |
apt-get -yqd upgrade > /dev/null | |
check_return "apt-get -yqd upgrade" | |
fi | |
if [[ -z $PACKAGES_TO_BE_UPGRADED ]] | |
then | |
echo "`date` [MESSAGE] No packages need updating." >> $LOGFILE | |
else | |
mail -s "[$THISSERVER] server may need some updates applied" -a "X-Maint: SUCCESS" $MAILTO <<EOF | |
Hello, | |
Packages have been downloaded onto $THISSERVER. | |
$PACKAGES_TO_BE_UPGRADED | |
The server has the following network interfaces configured ${SERVERADDS[@]}. | |
To update the server log in via ssh (e.g. ssh root@${IPADDR[0]}) and run the following command: | |
apt-get upgrade | |
See the logfile for more info: vim $LOGFILE | |
Regards. | |
EOF | |
echo "`date` [MESSAGE] Packages need updating email sent to $MAILTO" >> $LOGFILE | |
fi | |
stoplogging | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment