In the best tradition of open source, I am taking parts of two other
        peoples work, and combining them to make a better whole.

        I took the save_all.sh script posted to the list the other day, and
        merged in some code by Duc M Do  from a similar script of his. 

        The enhanced version now (optionaly) does an upload/checkpoint before
        doing the saveall. Duc M Do 's code also did a sshink, and the code to
        do that if you wish is left in, but commented out.

        Posible enhancments to this script would include:

        1. Seperate run time flags for upload, shrink, checkpoint, and saveall
        2. sensing the noise from foxboro's save_all script to /dev/null
           (it's does the fancy twirling bar, and this is _ugly_ in cron's
           oputput).

        I hope this proves useful to others. Comments sugestiosn welcome:


#!/bin/sh
#####################################################################
# This script creates the savealls in a pre-configured
# directory (see variable list at the beginning of this script)
#
# A creative conception by:    M. de Waal & R. Deen
# Special guest appearance by: R. de Groot & C. van Diepen
# Thanks to B. Marsman for his valuable testing, input and bug fixes
#
##################################################################### 
#---------------------------------------------------------------------+
#  Copyright (c)2000 The Foxboro Company. All Rights Reserved.        |
#                                                                     |
#                  http://thecassandraproject.org/                    |
#                                                                     |
#  This is free software; you can redistribute it and/or modify it    |
#  under the terms of the GNU General Public License as published by  |
#  the Free Software Foundation; either version 2 of the License, or  |
#  (at your option) any later version.                                |
#                                                                     |
#  This software 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 (http://www.fsf.org/copyleft/gpl.htm)for    |
#  more details.                                                      |
#                                                                     |
#  To obtain the GNU General Public License, write to:                |
#                                                                     |
#      Free Software Foundation, Inc.                                 |
#      59 Temple Place - Suite 330                                    |
#      Boston, MA  02111-1307                                         |
#      USA                                                            |
#---------------------------------------------------------------------+
# date: jan,15 2000
# Revision history:
# 
# Feb 23, 2000 - Initial release.
# Feb 24, 2000 - Added usage information.
#              - Added backup procedure for older save_all's.
#              - A lot of testing, seems OK.
#              - Added log-file: saveall.log.
#              - Major clean-up of original script.
# Feb 25, 2000 - A lot of problems solved, it seems to work now!
#              - Compressing the backup tar files to save space.
#              - All tests pas%&&*!@~~!@#$!____!!@$$#%%$^&^^&
#                core dumped.
# Feb 28, 2000 - Scripts did not work in crontab, added ./ before
#                commandline that executes the save_all. OK now!
#              - Added check for control station.
#                Preliminary version for remote hosts. Appears to work.
#              - Let's call it version 1.0 (Amsterdam)!
# Feb 29, 2000 - Tested with 20 Series host for Control station: OK
#              - Cleaned up the script. Tested remote stations handling.
#              - Improved reporting to screen and log file.
#              - Version 1.1 (Breda)!
# Mar 01, 2000 - Added Windows NT Support.
#              - Added archive management.
#              - Added free space check.
#              - Version 1.2 (Coevorden)!
# Mar 02, 2000 - Enhanced NT Support
#              - Improved reporting to screen and log file.
#              - Version 1.3 (Deventer)!
# Mar 09, 2000 - Bug report: script fails when no SAVEALL directory exists.
#                (UNIX only)
#                Changed the sequence. Directories are created before
#                checks are done
#                Version 1.3.1 (Deventer Noord)!
# Mar 10, 2000 - Bug fixed: Deletion of old backups was done BEFORE making
# (BM)           new backup, causing one backup more then specified.
#              - Timestamp for backups now saveall-time instead of backup-
#                time, including hour:minutes, so more than 1 backup per
#                day is possible.
#              - Version 1.3.2 (Deventer Noord-Oost)
# Apr  5, 2000 - Added /usr/local to path and changed from compress
# (SDB)          to gzip for better compression
#                Also merged in the update() function from Duc M Do
# Apr  6, 2000 - Added -n option to disbale upload/checkpoint if
# (SDB)          desired. Sinde these can take a long time.
#####################################################################
#
# BUGS/CONSIDERATIONS/WISHES:
#
# When an old "save_all" for STATION exists AND the new save_all fails,
# the save_all directory is empty for STATION, although the backup was
# successfull. Maybe restore the backup in this case?
#
# When forcing 25 backup's with a line in crontab, executing a
# commandline save_all with -b is less than 25, the surplus backups are
# removed!!
#
# Maybe it would be nice to have a 'save_all -h HOSTNAME' option,
# anybody? This would create a save_all for all stations hosted by
# HOSTNAME
#
#####################################################################
#
# variables used in this script:
#
VERSION="1.3.2 / March 2000"      # Version
PLATFORM=`uname -s`               # define platform
CURDIR=`pwd`                      # the current directory.
SA_DIR="/opt/SAVEALLS"            # Location of save_alls.
TRUE_CPFILE="/etc/cplns"          # Foxboro system file, DO NOT EDIT.
CPFILE="/etc/cplns"               # List for selected stations (default = all).
# CPFILE=cplns                    # List for selected stations (testing only).
SLDB="/usr/fox/sp/sldb"           # Relation between Control Station and Host.
HLDB="/usr/fox/sp/hldb"           # Station ID file
SN_NAME=`uname -n`                # this station's name
EXEC_DIR="/opt/fox/ciocfg/api"    # the executable location
LOCK_DIR="/usr/fox/sp/locks"      # the lock files
BU_DIR=$SA_DIR/backup             # the backup directory
STATKB=2000                       # minimum of free kbytes needed per station
                                  # (estimate!!!)
ARCHNR=10                         # default number of station-archives in
                                  # backup dir
BIDLIST="|207|C002|"              # Bad ID list, stations without remote host
                                  # support (NT)
TEE="tee -a $LOG"                 # tee to the log file
GZIP=/opt/FSFgzip/bin/gzip        # Better compression

if [ "$PLATFORM" = "Windows_NT" ] # Define executable Name
then
   SA_EXEC="save_all.ksh"
else
   SA_EXEC="save_all"             
fi

# Add /stupid Foxboro /usr/local to PATH
PATH=$PATH:/usr/local
export PATH

###########################################################################
# Let's begin
###########################################################################
# clear
echo
echo "Save-all backup script version: $VERSION"
echo "-----------------------------------------------------"| $TEE
echo
###########################################################################
# Logging can go to screen or log file
###########################################################################
#
LOG=$CURDIR/saveall.log        # use this line for a log file
# LOG=`tty`                    # use this line for screen output
#
###########################################################################
# Function to display command usage and exit
###########################################################################
usage_exit ()
{
   echo "Usage: `basename $0` -s string [string] [string] ... [-b backups] [-n]"
   echo "       `basename $0` all [-b backups] [-n]"
   echo "       Use regular expression similar to grep (no *)"
   echo 
   echo "       -n to not do upload/checkpoint before doing saveall "
   echo "          default is to do so."
   echo "Example:"
   echo "        `basename $0` -s CP30 -b 4"
   echo "        will make a save_all for stations:"
   echo "        CP3011, MGCP30, ACP30A, etc."
   echo "        and maintain a maximum of 4 backups:"
   echo "        first in will be first out."
   echo
   exit 1
}

###########################################################################
# Function to do the actual upload/checkpoint/saveall 
###########################################################################
upload()        # this function first builds the upload command file for 
                # the upload then performs the actual uploading of parameters
                # of the specified CP to the workfile
                #
                # parameter:  $1  letterbug ID of CP to be uploaded
{
    CMD=/tmp/$1.upload
    echo OPEN $1 MODIFY byDuc > $CMD
    echo UPLOAD              >> $CMD
#   echo SHRINK              >> $CMD
    echo CHECKPOINT 60       >> $CMD
    echo CLOSE               >> $CMD
    echo EXIT                >> $CMD
    echo "`date` --- upload and checkpoint $1" | $TEE
    ./iccdrvr.tsk -i /tmp/$1.upload -o /tmp/$1.log -n ECHO
    cat /tmp/$1.log | $TEE
    rm /tmp/$1.upload /tmp/$1.log 2> /dev/null
}

UPLOAD=TRUE
###########################################################################
# Check for executable presence, no use to go on if it's not there...
###########################################################################
# For the NT platform: Verify that script is installed/executed from drive D:
if [ "$PLATFORM" = "Windows_NT" ]
then
   DRIVE=`pwd | cut -c1`
   if [ "$DRIVE" != "D" ]
   then
      echo "Script must be executed from D: drive"
      echo
      exit
   fi
fi
if [ ! -f $EXEC_DIR/$SA_EXEC ]
then
   echo "No $SA_EXEC program found."
   echo "aborting..."
   exit 2
fi
###########################################################################
# Evaluating the command-line parameters
###########################################################################
if [ ! $# -gt 0 ]
then 
   usage_exit
fi

if [ -f "$CURDIR"/tmp_cpln ] || [ -f "$CURDIR"/tmp_cplns ]
then
   rm "$CURDIR"/tmp_cpln*
fi

while [ $# -gt 0 ]
do
   case $1 in
      all)
         if [ -f $LOG ]
         then
            rm $LOG
         fi
         # This selection indicates all stations in the variable CPFILE 
         echo "Save-all for all stations in $CPFILE selected" | $TEE
         cp $CPFILE "$CURDIR"/tmp_cplns
         shift
      ;;
      -b)
         shift
         if [ $# -gt 0 ]
         then
            ARCHNR=`expr $1 + 0 2>/dev/null`
            if [ "$?" != "0" ] || [ "$ARCHNR" -lt 1 ]
            then
               echo "-b must be followed by a positive numeric value"
               echo
               usage_exit
            fi
         else
            echo "$1 is not a positive number"
            echo
            usage_exit
         fi
         shift
      ;;
      -n)
         shift
                 UPLOAD=FALSE
                 export UPLOAD
      ;;
      -s)
         if [ -f $LOG ]
         then
            rm $LOG
         fi
         # This selection indicates some of the configured stations
         # will be done
         # move the -s parameter out of the way
         shift
         # the next parameter should be (part of) a Control station lbug
         # and the number of params should be 1 or greater
         if [ ! $# -gt 0 ]
         then
            echo "No station(s) specified after -s"
            echo
            usage_exit
         fi
      
         echo "Creating station letterbug file..." | $TEE;echo
         while [ $# -gt 0 ] && [ `echo $1|cut -c1` != "-" ]
         do
            # Check if the stations are valid for this system
            grep $1 $TRUE_CPFILE >> "$CURDIR"/tmp_cpln
            shift
         done
         # Remove possible duplicate letterbugs due to selection criteria
         sort -u "$CURDIR"/tmp_cpln > "$CURDIR"/tmp_cplns
         CPFILE="$CURDIR"/tmp_cplns
         if [ ! -s $CPFILE ]
         then
            echo "No stations matching selection criteria" | $TEE
            echo
            exit 1
         fi
      ;;
      *)
         usage_exit
      ;;
   esac
done

if [ ! -f tmp_cplns ]
then
   echo "No stations specified!"
   echo
   usage_exit
fi

###########################################################################
# printing a list of all the stations / hosts to be processed
###########################################################################
echo;echo "Stations selected for save_all:" | $TEE
echo "Station      Host name" | $TEE
for CP in `cat $CPFILE`
do
   # HOST=`grep $CP $SLDB | awk '{print $2}'`
   HOST=`awk '{if ($1==cp) {print $2} }' cp=$CP < $SLDB`
   echo "$CP       $HOST" | $TEE
done

###########################################################################
# Setting up the basic administration.
###########################################################################
echo; echo "Writing logfile in: $LOG"
echo; echo "Checking directories..." | $TEE
#
# Check for save all directory and create if needed
#
if [ ! -d $SA_DIR ]
then
   echo "Creating directory "$SA_DIR | $TEE
   mkdir $SA_DIR
fi
#
# Check for backup directory for the old save_alls and create if needed
#
if [ ! -d $BU_DIR ]
then
   echo "Creating directory "$BU_DIR | $TEE
   mkdir $BU_DIR
fi
#
# Create a directory for all the configured control stations
#
for CP in `cat $TRUE_CPFILE`
do
if [ ! -d $SA_DIR/$CP ]
then 
   echo "Creating directory for $CP..." | $TEE
   mkdir $SA_DIR/$CP
fi
done
echo "All directories OK." | $TEE

###########################################################################
# Check for diskspace
###########################################################################
echo "Checking for available diskspace" | $TEE
STATNR=`cat "$CURDIR"/tmp_cplns | wc -l`
NEEDED=`echo "$STATNR * $STATKB" | bc`
if [ "$PLATFORM" = "Windows_NT" ] 
then
   AVAILABLE=`df -k D: | tail -1 | awk '{print $3}' | awk -F"/" '{print $1}'`
else
   AVAILABLE=`df -k $SA_DIR | tail -1 | awk '{print $4}'`
fi

echo "Available space on system: $AVAILABLE" | $TEE
echo "Space required on system: $NEEDED" | $TEE
if [ $NEEDED -gt $AVAILABLE ]
then
   echo "Not enough space available for selected stations" | $TEE
   exit 1
else
   echo "Enough space available, continuing..." | $TEE
fi
###########################################################################
# The actual save_all part starts here
###########################################################################
#
# Starting the Save-all LOG file here...
#
echo
echo "#####################################################"| $TEE
echo "# Automatic save-all procedure started"| $TEE
echo "# Date:               "`date` | $TEE
echo "# Started on:         "$SN_NAME | $TEE
echo "# Save-all Location:  "$SA_DIR | $TEE
echo "# Backup location:    "$BU_DIR | $TEE
echo "#####################################################"| $TEE
#
# Start with routine to make the save_alls
#

for CP in `cat $CPFILE` 
do 
   #HOST=`grep $CP $SLDB | awk '{print $2}'`
   HOST=`awk '{if ($1==cp) {print $2} }' cp=$CP < $SLDB`
   if [ "$HOST" = "" ]
   then
      echo "#####################################################"| $TEE
      echo "# $CP is not a valid station         " | $TEE
      echo "#####################################################"| $TEE
      LOCK="unknown"
   else
      cd $EXEC_DIR
      echo "#####################################################"| $TEE
      echo "# Save_all starting for: $CP on $HOST" | $TEE
      echo "#####################################################"| $TEE
      # Station may be in use by another ICC session
      # So, check if station is locked (also remote hosts)
      if [ "$HOST" = "$SN_NAME" ]
      then
         # echo "Station is hosted locally." | $TEE
         if [ -f $LOCK_DIR/*$CP+ ]
         then
            LOCK="yes"
            echo "Database locked, skipping $CP" | $TEE
           else
            LOCK="no"
           fi
      else
         # Check platform to see how to connect to remote host
         # This doesn't work on hybrid systems yet!!!!
         STATID=`grep $HOST $HLDB|awk '{print $2}'`
         IDCHECK=`echo "$BIDLIST"|grep "|"$STATID`
         if [ "$PLATFORM" = "Windows_NT" ] || [ "$IDCHECK" != "" ] 
         then
            echo "System is NT, remote host-access not yet supported (NT problem)" | 
$TEE
            LOCK="unknown"
         else
            echo "Trying to contact $HOST..." | $TEE
            # Check if remote station is rmounted and where
            MOUNTPOINT=`rmount | grep $HOST | awk '{print $4}'`
            if [ "$MOUNTPOINT" = "" ]
            then
               if [ ! -d /rem/$HOST ]
               then
                  mkdir /rem/$HOST
               fi
               rmount $HOST /rem/$HOST 2>/dev/null
               if [ "$?" -ne "0" ]
               then
                  echo "$HOST is unavailable"
                  LOCK="unknown"
               elif [ -f /rem/"$HOST"/"$LOCK_DIR"/"$CP"+ ]
               then
                  LOCK="yes"
                  echo "Database locked, unable to perform the save_all." | $TEE
               else
                  LOCK="no"
               fi
               rumount $HOST 2>/dev/null
            else
               if [ -f "$MOUNTPOINT"/"$LOCK_DIR"/"$CP"+ ]
               then
                  LOCK="yes"
               else
                  LOCK="no"
               fi
            fi
         fi
      fi
      # Lock check complete
   fi

   if [ -d $SA_DIR/$CP/$CP"_STA" ] && [ $LOCK = "no" ]
   then
      echo "Found a previously stored save_all for this station." | $TEE
      echo "Making back-up first" >> $LOG
      echo "Making back-up first\r\c" 
      NAME=`ls -ld $SA_DIR/$CP | awk '{print $7 $6 $8}' | sed s/\://`
      cd $SA_DIR
      echo "...backing-up $CP" >> $LOG
      echo "...backing-up $CP \r\c" 
      tar cf $BU_DIR/$NAME"_"$CP.tar $SA_DIR/$CP >/dev/null
      $GZIP -f $BU_DIR/$NAME"_"$CP.tar >/dev/null
      echo "Back-up for $CP created in:\n   $BU_DIR/$NAME""_""$CP.tar.gz" | $TEE
      # determine level of backups until now
      LVL=`ls -l $BU_DIR/*"$CP"* | wc -l` 2>/dev/null
      DEL=`echo "$LVL - $ARCHNR" | bc`
      if [ $DEL -gt 0 ]
      then
         for DELFILE in `ls -lt $BU_DIR/*"$CP"* | tail -$DEL | awk '{print $9}'`
         do
            rm $DELFILE
         done
      fi
      rm -r $SA_DIR/$CP/*
   elif [ $LOCK = "no" ]
   then   
      echo "No previously stored save-all found to backup. Continuing..." | $TEE
   fi
   if [ $LOCK = "no" ]
   then 
      cd $EXEC_DIR
      if [ $UPLOAD = "TRUE" ]
      then 
          echo "Starting upload/checkpoint......." | $TEE
          upload $CP
          echo "upload/checkpoint Complete" | $TEE
          echo "Save_all for $CP started" | $TEE
          fi
      # Insert ./ to ensure the scripts runs OK from cron!
      ./$SA_EXEC $CP $SA_DIR/$CP > /dev/null & 
      PID=`echo $!`
      #echo "PID= $PID"
      VAR=0 
      while [ $VAR -ne 1 ]
      do
         ps -p $PID > /dev/null  2>&1
         VAR=$?
         echo "|\r\c";sleep 1;echo "/\r\c";sleep 1
         echo "-\r\c";sleep 1;echo "\\ \r\c";sleep 1
      done
      if [ ! -f /tmp/output* ]
      then
         echo "Save-all for $CP successfully completed." | $TEE
      else
         echo "There were errors for $CP." | $TEE
         cat /tmp/output* >> $LOG
      fi
   fi
done
echo "#####################################################" | $TEE
echo "# Automatic save-all procedure finished" | $TEE
echo "# Date:"`date` | $TEE
echo "#####################################################" | $TEE
# Cleanup after work is done (don't try this at home)
cd $CURDIR
if [ -f "$CURDIR"/tmp_cpln ] || [ -f "$CURDIR"/tmp_cplns ]
then
   rm "$CURDIR"/tmp_cpln*
fi
echo

-- 
Stan Brown     [EMAIL PROTECTED]                                    843-745-3154
Charleston SC.
-- 
Windows 98: n.
        useless extension to a minor patch release for 32-bit extensions and
        a graphical shell for a 16-bit patch to an 8-bit operating system
        originally coded for a 4-bit microprocessor, written by a 2-bit 
        company that can't stand for 1 bit of competition.
-
(c) 2000 Stan Brown.  Redistribution via the Microsoft Network is prohibited.

-----------------------------------------------------------------------
This list is neither sponsored nor endorsed by the Foxboro Company. All 
postings from this list are the work of list subscribers and no warranty 
is made or implied as to the accuracy of any information disseminated 
through this medium. By subscribing to this list you agree to hold the 
list sponsor(s) blameless for any and all mishaps which might occur due to 
your application of information received from this mailing list.

To be removed from this list, send mail to 
[EMAIL PROTECTED] 
with "unsubscribe foxboro" in the Subject. Or, send any mail to
[EMAIL PROTECTED]

Reply via email to