#!/bin/sh
#
# Filename: $RCSfile: backuper.sh,v $
# Internal ID & Revision:
# $Id: backuper.sh,v 1.2 2008/03/17 09:18:00 smurphy Exp $
# $Revision: 1.2 $
# $Header: /home/smurphy/scripts/RCS/backuper.sh,v 1.2 2008/03/17 09:18:00 smurphy Exp $
# Author: $Author: smurphy $
# Locked by: $Locker:  $
#
# File Name & location:
# /usr/local/bin/updater.sh
#
# Short description:
# Backups config-files from a routeros based system
#
# -------------------------------------------------------------
#
# Programm Version
VER="$Revision: 1.2 $"
#
PROGNAME=`/usr/bin/basename $0`
FLock=/var/lock/${PROGNAME}..LOCK

# Trap any Interuptions.
trap "" SIGINT

# Some absolute programm paths...
DESTDIR=/home/smurphy/backup

# Backup-User
BACKUP_USER=backup

# RouterBoard IP-Address
RB_ADDRESS=10.X.X.X

# Set terminal type
TERM=vt100

#
##############################################################################
#
errors() {
#DOC: The errors Function is called to control the exit status.
#
: ${errlvl:=9}
: ${MSG:="No Error message - Probably user interruption"}
if [ $errlvl -gt 0 ] ;
    then
    if [ $errlvl = 15 ] ;
    then
        echo -e "WARNING: $MSG"
    else
        #Usage
        echo -e "\a"
        echo "FATAL:  An error occured in \"${PROGNAME}(${FUNCTION})\". Bailing out..."
        echo -e "ERRMSG: $MSG"
        echo
	Unlock $FLock
        exit $errlvl
    fi
fi
} # errors Function
#
#
##############################################################################
#
# Lockfile Generation
Lock() {
# Lockfile to create
tolock="$1"
Action="$2"
#
# Lock file if lockfile does not exist.
if [ -s $tolock ]
then
    # If we have provided a second Var, set Exit status using  it.
    if [ ! -n "$Action" ]
    then
	# Oops, we  found a lockfile. Loop while checking if still exists.
	while [ -s $tolock ]
	do
	    sleep 5 ;
	done
	MSG="Creating lockfile $tolock failed after 5 secs"
	# write PID into Lock-File.
	echo $$ > $tolock
	errlvl=$?
	errors
    else
	Pid="`cat $tolock`"
	Exists="`ps auxw | grep \" $Pid \" | grep -c $PROGNAME`"
	if [ $Exists = 1 ]
	then
	    MSG="\"$PROGNAME\" already running. Exiting..."
	    errlvl=$Action
	    errors
	else
	    MSG="Found stale lockfile... Removing it..."
	    rm -f $tolock
	    errlvl=$?
	    errors
	    MSG="Creating lockfile $tolock failed"
	    echo $$ > $tolock
	    errlvl=$?
	    errors
	fi
    fi
else
    # Lock it
    MSG="Creating lockfile $tolock failed"
    echo $$ > $tolock
    errlvl=$?
    errors
fi
} # Lock
#
##############################################################################
#
Unlock(){
# Name of Lockfile to unlock
unlock="$1"
# Unlock the file.
if [ -s $unlock ]
then
    PID=$$
    if [ "`cat $unlock`" != "$PID" ]
    then
        echo "Problem, Wrong lock-file PID. This shouldn't happen"
    else
        # Removing Lockfile
        rm -f $unlock
    fi
fi
#
} # Unlock
#
##############################################################################
#
usage() {
    echo
    echo -e >&2 "usage:\t$PROGNAME "
    echo -e >&2 "\t-u\t Downloads router config"
    echo
} # function usage
#
##############################################################################
# Get date and Time.
DATE=`date +%Y-%m-%d`
DATEStartSecs=`date +%s`

# Lock this script.
Lock $FLock 1;

# Get process ID for ourself :)
PROG_PID=$$
#
echo 
echo "*** `date` - Starting $PROGNAME"
echo "*** $PROGNAME version $VER [Pid: $PROG_PID]"
echo "*** (c) J. Mertin <smurphy@solsys.org>"
echo "***"

echo "*** Configuration ========================================"
echo "*** Date:          $DATE "
echo "*** Host:          `hostname -f` "


MSG="Going to destination directory"
cd $DESTDIR
errlvl=$?
errors

MSG="Removing old backups"
rm -f *.backup
errlvl=$?
errors

BDATE=`date +%Y%m%d`
MSG="Downloading Gateway Config"
echo "*** ====================================================== "
curl -n ftp://${BACKUP_USER}@${RB_ADDRESS}/daily_backup.backup -o ${BDATE}_RB153.backup
errlvl=$?
errors

# Get out.
Unlock $FLock
#
# -------------------------------------------------------------
#
# End of /usr/local/bin/updater.sh
#
