Bash script for keeping Debian & Ubuntu systems up to date

maint.sh - Bash script for keeping Debian & Ubuntu systems up to date.

This script will do a bunch of things - it\'s main purpose is to help keep apt based systems up to date. Check out the top part of ths script for a more in depth explanation.

Run this script periodically via cron.

NOTE: This script has been tested on Debian (Sarge) and Ubuntu (5.10), your mileage may vary on other systems using apt.

 

#!/bin/sh
# Script Name: maint.sh
# Author Name: Keith Bawden
# Date: Wed May 17 15:40:32 JST 2006
# Updated: Tue May 30 13:42:06 JST 2006
# 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
 
#
# Make user configuration changes in this section 
#
 
export REPLYTO=bawdo@example.com
MAILTO="bawdo@example.com"
AUTOUPDATE="no" 
LOGFILE="/var/log/server_maint.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 startlogging {

 

	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
}

# End IP Address stuff
 
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 -yq upgrade > /dev/null
	check_return "apt-get -yq upgrade"
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

tags: bash, Linux, system administration

Fri 27 Oct 2006, 00:00

0 comments

Back