Forum

Simple script to check for updates and update immediately

Guerreiro
17 December 2012, 13:24
#!/bin/bash

LATEST=`wget -q -O - http://www.hiawatha-webserver.org/latest`
VERSION=`hiawatha -v | cut -f 1 -d , | sed 's/Hiawatha\ //' | grep v | sed 's/v//'`
if [ $LATEST = $VERSION ]; then
echo "Hiawatha is already the latest version"
else echo "Run the update script"
fi


Save above in name.sh and name can be replaced with whatever you want. Also do chmod +x name.sh and you are done. This will tell you if you need to update or not.

Now next one will update your Hiawatha server.
#!/bin/bash
#########################################################################
# Author: Jan Harustiak (2010-2012)
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#########################################################################

#########################################################################
#
# Automatically download latest Hiawatha webserver and perform update.
# Tested on Debian only, for Hiawatha v8.0 and higher!
#
#########################################################################
#
# Set this variable, if you want to be informed per email:
# example: MAILTO=admin@yourserver.com
MAILTO=youremailaddresshere
#########################################################################

PATH=/usr/local/sbin:/usr/sbin:/sbin:$PATH

# Make sure that I'm root
if [ "$(id -u)" != "0" ]; then
echo "$0 - This script must be run as root!" 1>&2
exit 1
fi

LSOF=$(lsof -p $$ 2>/dev/null | grep -E "/"$(basename $0)"$")
MY_PATH=$(echo $LSOF | sed -r s/'^([^\/]+)\/'/'\/'/1 2>/dev/null)
MY_ROOT=$(dirname $MY_PATH)

date

LATEST=`wget -q -O - http://www.hiawatha-webserver.org/latest`
if [ -z $LATEST ]; then
echo "Error fetching Hiawatha version number from server!"
exit 1
fi
if [ -s $MY_ROOT/hiawatha-$LATEST.tar.gz ]; then
echo "Latest Hiawatha v$LATEST already downloaded."
exit 0
fi

echo -n "Downloading new Hiawatha v$LATEST webserver..."
wget -q -O $MY_ROOT/hiawatha-$LATEST.tar.gz http://www.hiawatha-webserver.org/files/hiawatha-$LATEST.tar.gz
if [ -s $MY_ROOT/hiawatha-$LATEST.tar.gz ]; then
echo 'OK'
else
echo 'ERROR'
if [ -f $MY_ROOT/hiawatha-$LATEST.tar.gz ]; then
rm -f $MY_ROOT/hiawatha-$LATEST.tar.gz
fi
exit 1
fi

echo -n 'Unpacking...'
cd $MY_ROOT
tar -xzf $MY_ROOT/hiawatha-$LATEST.tar.gz >/dev/null
if [ $? != 0 ]; then
echo "ERROR"
exit 1
fi
echo 'OK'

if [ ! -d $MY_ROOT/hiawatha-$LATEST ]; then
echo "Error: hiawatha-$LATEST directory not found."
exit 1
fi
echo 'Packaging...'
cd $MY_ROOT/hiawatha-$LATEST/extra
$MY_ROOT/hiawatha-$LATEST/extra/make_debian_package

DEBPAK=`ls -1 $MY_ROOT/hiawatha-$LATEST/hiawatha_$LATEST\_*.deb 2>/dev/null | tail -n 1`
if [ ! -s $DEBPAK ]; then
echo -e "\nFAILED"
echo "Debian package not found!"
exit 1
fi

mv "$DEBPAK" "$MY_ROOT"
DEBPAK=`ls -1 $MY_ROOT/hiawatha_$LATEST\_*.deb 2>/dev/null | tail -n 1`
cd $MY_ROOT

echo -n 'Removing temp files...'
rm -rf $MY_ROOT/hiawatha-$LATEST
echo 'DONE'

echo 'Installing new webserver...'
dpkg -i --force-all $DEBPAK
if [ $? == 0 ]; then
MSG="Hiawatha webserver successfully updated to v$LATEST."
EC=0
else
MSG="Hiawatha webserver update to v$LATEST FAILED."
EC=1
fi

echo $MSG
if [ ! -z $MAILTO ]; then
echo $MSG | mail -s "Hiawatha update" $MAILTO
fi
exit $EC

Save above in another name.sh and do a chmod +x name.sh again. Now if you want to update automatically change first script to this (using update-hiawatha.sh as an example name)

#!/bin/bash

LATEST=`wget -q -O - http://www.hiawatha-webserver.org/latest`
VERSION=`hiawatha -v | cut -f 1 -d , | sed 's/Hiawatha\ //' | grep v | sed 's/v//'`
if [ $LATEST = $VERSION ]; then
echo "Hiawatha is already the latest version"
else ./update-hiawatha.sh
fi

That is it really. Hope that helps you people out a bit to keep your awesome hiawatha server in tip top condition running the latest and greatest version.
Hugo Leisink
17 December 2012, 13:59
Thanks for sharing!
This topic has been closed.