#!/bin/sh
trap "" 1 2 3 15
#******************************************************************************
#                       ISTP LZPR SOFTWARE
#               Property of the U.S. Government
#                       NASA/GSFC/Code 560
#******************************************************************************
#
# UNIT NAME: lzp_backup
#
# PURPOSE: To backup and recover mission database and configuration
#
# INVOCATION METHOD: lzp_backup [-hqcfo] [-t tables] [-d directory] [-n number]
#                               [-p print time] [-w wait time] [-l backup file]
#                               [-r backup file] [database users]
#
# CHANGE HISTORY:
# Author        Change ID  Build/Release   Date      Description of Change
# ------        ---------  ------------- ---------   ---------------------
# R. Wiechert   ISTP       R1.3          06/12/98    Original Code
# A. Tung       ISTP                     07/01/98    Added backup DPS Q&A tables,
#                                                    remove DPS QA and RECON data
#                                                    older than user's specified 
#                                                    # of days. 
# A. Tung       ISTP                     09/10/98    Add commit statements
#                                                    for DPS QA backup and deletion.
#
# NOTES:
# See help text embedded in code
#
#******************************************************************************
sqlprompt="IGNORESQLPROMPT "
tmpfile="/tmp/lzp_backup.$$.tmp"
lockfile=config/Assembly_Decom_Startup.parm
max_dump_count=0 # no history by default
max_sleep_time=21600 # 6 hours in seconds
print_period=900 # 15 minutes in seconds
halt_period=60 # 1 minute in seconds
sleep_time=0 # initialize to zero
min_daystokeep=15 # keep at least 15 days
configuration=false
dumptype=null
verify=false
prompt=false
quick=false
help=false
recover=""
list=""

# determine current unix user
username=`/usr/ucb/whoami`
if [ ! "$username" ]
then
   printf "\nlzp_backup: unable to determine current user\n\n"; exit 9
fi

# determine current user home directory
userhome=`grep "^$username:" /etc/passwd`
userhome=`expr "$userhome" : '.*:.*:.*:.*:.*:\(.*\):.*'`
if [ "$userhome" ]
then
   semaphore="$userhome/$lockfile"
   configdir="$userhome/config"
   dumpdir="$userhome/opslog"
else
   printf "\nlzp_backup: unable to determine home directory\n\n"; exit 9
fi

# determine runtime options
while getopts hqvcfot:d:n:p:w:l:r:a: option
do
   case $option in
      q) quick=true ;;
      v) verify=true ;;
      c) configuration=true ;;
      f) dumptype=full; prompt=true ;;
      o) dumptype=owner ;;
      t) dumptype=tables; tablelist="$OPTARG" ;;
      d) dumpdir="$OPTARG" ;;
      n) max_dump_count="$OPTARG" ;;
      p) print_period="$OPTARG" ;;
      w) max_sleep_time="$OPTARG" ;;
      l) list="$OPTARG" ;;
      r) recover="$OPTARG" ;;
      a) dumptype=acct; daystokeep="$OPTARG" ;;
      *) help=true ;;
   esac
done

# default runtime options
if [ "$dumptype" = "null" -a "$recover" ] 
then
   dumptype=owner
fi
if [ "$dumptype" = "null" -a "$configuration" = "false" ] 
then
   dumptype=owner
   configuration=true
fi

# display help as needed
if [ "$help" = "true" ]
then
more << END_USAGE_TEXT

USAGE:     lzp_backup [-hqvcfo] [-t tables] [-d directory] [-n number]
                      [-p print time] [-w wait time] [-l backup file]
                      [-r backup file] [-a number of days to keep]
                      [database users]

PURPOSE:   Backup and recover mission database and configuration

OPTIONS:   -h      display help screen
           -q      quick backup without waiting
           -v      verify cross-table consistency
           -c      mission configuration backup, files
           -f      full database backup, all objects
           -o      owner database backup, owner objects
           -t      database table backup, specified tables
           -a      accounting cleanup, keep specified days
           -d      specify backup file directory location
           -n      specify number of backups to keep online
           -p      specify number of seconds between prints
           -w      specify total number of seconds to wait
           -l      list backup file contents (dmp or tar)
           -r      recover database objects from backup file

SYNOPSIS:  The standard Oracle export utilities are used in order
           to backup the specified database users' objects, along
           with mission specific configuration files, including
           hidden files located within the mission home directory.
           The standard Oracle import utilities are used in order
           to recover the specified database users' objects, not
           including mission specific configuration files which
           must be recovered manually.

           When database users are explicitly specified on the
           command line, as must be done from non-mission Unix
           accounts, database instance names and passwords will
           be prompted.  The database user is assumed to be
           identical to the current Unix user when no database
           user is specified.

           Since more reliable backups are obtained while the
           system is in a quiescent state, lzp_backup normally
           attempts to obtain the necessary semaphores before
           performing any backups.  The -q option may be used
           to obtain a quick backup without waiting.  The -v
           option verifies cross-table consistency and may be
           incompatible with the -q option.

           The -c option is used to backup mission configuration
           files located in the mission home config directory,
           along with hidden files located in the mission home
           directory.  The hidden files are first copied into
           the config directory in order to generate the backup.

           The -fot options may be used to specify the database
           backup type:  full, owner, or table.  Full backups
           prompt for the system password which is required to
           export all objects within a particular database
           instance.  Owner backups export only those objects
           which belong to the specified database user.  Table
           backups export only the specified tables belonging
           to the specified database user.  When specifying
           tables, use a comma separated list without spaces.

           The -a option is used to perform an accounting
           cleanup which first backs up and then deletes all
           accounting information older than the specified
           number of days.  The cleanup affects both DPS as
           well as Reconstruction database objects and may
           be run most easily as a mission user cron job.
           The operator is encouraged to keep at least 15
           days of information at any given time.

           All backup files are located within the mission
           opslog directory, unless the -d option is used to
           specify an alternate location.  Multiple versions
           of backup files may be created using the -n option.
           When multiple versions are specified, backup files
           are cycled in FIFO fashion, deleting the oldest
           file when the maximum version number is reached.

           The -p and -w options may be used to specify how
           often semaphore wait messages are printed and the
           length of the semaphore timeout, after which the
           backup will be aborted.  Both options should be
           specified in seconds.  By default, semaphore wait
           messages are printed every 15 minutes while the
           semaphore timeout occurs after 6 hours.

           Once backup files have been created, the -l option
           may be used to generate a list of tables contained
           within the specified backup file.  The -r option
           is used together with the -fot options in order to
           recover the specified database objects from the
           specified backup file.  The -fot options used to
           recover database objects may differ from those
           which were used to create the backup file.  This
           allows one to recover just a subset of objects
           contained within a particular backup file.

NOTES:     Backup filenames are constructed by concatenating
           the database instance, database user, backup type,
           and backup version number as in these examples:

           POLAR_POLAR_full.dmp.0     WIND_WIND_tables.dmp.2
           POLAR_PWIW_owner.dmp.1     WIND_WIND_config.tar.3
           GEOTAIL_GEOTAIL_acct.dmp.0

           Database backups may be recovered using the -r
           option, or, by invoking the Oracle import utility
           directly.  Mission configuration backups must be
           recovered manually using the Unix tar command.

           For more help concerning the Oracle import/export
           utilities use "imp help=y" or "exp help=y".  For
           help with the Unix tar command use "man tar".

           When executing lzp_backup as a mission cron job,
           be sure to setup a valid mission environment:
           csh -fc "source .login; source .cshrc; lzp_backup"
           Also beware that some options result in prompts.

           By default, with no options, lzp_backup performs
           database owner backups and recoveries, attempting
           to backup mission configuration when appropriate.
 
END_USAGE_TEXT
exit 0
fi

# validate runtime options
max_dump_count=`echo "$max_dump_count" | tr -cd "0123456789"`
print_period=`echo "$print_period" | tr -cd "0123456789"`
max_sleep_time=`echo "$max_sleep_time" | tr -cd "0123456789"`
daystokeep=`echo "$daystokeep" | tr -cd "0123456789"`
if [ "$dumptype" = "acct" -a "$daystokeep" -lt "$min_daystokeep" ]
then
   printf "\nlzp_backup: must keep at least $min_daystokeep days\n\n"; exit 9
fi
if [ "$recover" -a ! -f "$recover" ]
then
   printf "\nlzp_backup: unable to access $recover\n\n"; exit 9
fi
if [ "$list" -a ! -f "$list" ]
then
   printf "\nlzp_backup: unable to access $list\n\n"; exit 9
fi
if [ ! "$recover" -a ! "$list" ]
then
   if [ ! -d "$dumpdir" -o ! -x "$dumpdir" -o ! -w "$dumpdir" ]
   then
      printf "\nlzp_backup: unable to access $dumpdir\n\n"; exit 9
   fi
fi

# list backup file contents when specified
if [ "$list" ]
then
   tarfile=`expr "$list" : '.*\(\.tar\).*' 2>/dev/null`
   if [ "$tarfile" ]
   then
      printf "\nList of files contained in\n$list\n\n"
      tar tfv $list
      printf "\n"
   else
      printf "\nList of tables contained in\n$list\n\n"
      grep '^TABLE' $list 2>/dev/null | sort -u | nawk '{ gsub("\"", " ");\
         if ( NR%2 == 0 ) printf("\n");\
         printf("%-40s   ",$2) } \
         END { printf("\n\n") }'
   fi
   exit 0
fi

# determine database users
shift `expr $OPTIND - 1`
if [ $# != 0 ]
then
   dpsuserlist="$*"
   prompt=true
else
   dpsuserlist=`decrypt.sql $PICSHOME`
   if [ ! "$dpsuserlist" ]
   then
      echo "\nlzp_backup: unable to determine database user\n\n"; exit 9
   fi
fi

# create temporary file
cleanup="/usr/bin/rm -f $tmpfile"
/usr/bin/rm -f $tmpfile 2>/dev/null
/usr/bin/touch $tmpfile 2>/dev/null
/usr/bin/chmod 600 $tmpfile 2>/dev/null
if [ $? != 0 ]
then
   printf "\nlzp_backup: unable to create $tmpfile\n\n"; exit 9
fi

# for each database instance
dpsuserlist=`echo "$dpsuserlist" | tr '[a-z]' '[A-Z]'`
for dpsuserpass in $dpsuserlist
do
   # obtain database instance and passwords, interactive mode only
   trap "echo INTERRUPT; stty echo; $cleanup; exit 1" 1 2 3 15
   while [ "$prompt" = "true" ]
   do
      if [ `dirname "$dpsuserpass"` != "." ]
      then
         # strip password if already known
         dpsuserpass=`dirname "$dpsuserpass"`
      fi
      stty echo
      ORACLE_SID="$dpsuserpass"
      printf "\nEnter instance name [$ORACLE_SID]: "
      read in_sid
      if [ "$in_sid" ]
      then
         ORACLE_SID=`echo "$in_sid" | tr '[a-z]' '[A-Z]'`
      fi
      stty -echo
      if [ "$dumptype" = "full" ]
      then
         printf "Enter SYSTEM password: "
         read syspass
         printf "\nAgain to confirm: "
         read confirm
         if [ ! "$syspass" -o "$syspass" != "$confirm" ]
         then
            printf "\nERROR\n"
            continue
         fi
         printf "\n"
      fi
      printf "Enter DPSUSER password: "
      read answer
      printf "\nAgain to confirm: "
      read confirm
      if [ "$answer" -a "$answer" = "$confirm" ]
      then
         export ORACLE_SID
         dpsuserpass="dpsuser/$answer"
         printf "\n\n"
         stty echo
         break
      else
         printf "\nERROR\n"
      fi
      printf "\n"
   done

   echo "lzp_backup: `date` unix user $username"
   /usr/bin/cp /dev/null $tmpfile 2>/dev/null
   sqlplus $dpsuserpass 1>/dev/null 2>/dev/null << ENDORACLEARGS
   set pagesize 100
   set linesize 100
   set sqlprompt "$sqlprompt"
   set sqlcontinue "$sqlprompt"
   set sqlnumber OFF
   set heading OFF
   set feedback OFF
   spool $tmpfile
   select ORACLELOGINANDPASS from db_oraclelogin where RESOURCETYPE like 'AD%';
ENDORACLEARGS
   if [ $? != 0 ]
   then
      printf "\nlzp_backup: unable to access database\n\n"
      eval $cleanup
      exit 9
   else
      reconuserlist=`cat $tmpfile 2>/dev/null | grep -v "$sqlprompt"`
      /usr/bin/cp /dev/null $tmpfile 2>/dev/null
      if [ ! "$reconuserlist" ]
      then
         printf "\nlzp_backup: unable to determine database user\n\n"
         eval $cleanup
         exit 9
      fi
   fi

   # obtain assembly/decom semaphore to ensure reliable backup
   while [ "$dumptype" != "null" -a "$quick" = "false" -a "$prompt" = "false" ]
   do
      print_flag=`expr $sleep_time % $print_period`
      if [ "$print_flag" = "0" ]
      then
         echo "lzp_backup: `date` waiting for semaphore"
      fi
      /usr/bin/ln -s stop $semaphore 2>/dev/null
      if [ $? = 0 ]
      then
         echo "lzp_backup: `date` obtained semaphore"
         cleanup="$cleanup $semaphore"
         break
      fi
      trap "echo INTERRUPT; $cleanup; exit 1" 1 2 3 15
      sleep 1
      sleep_time=`expr $sleep_time + 1`
      if [ "$sleep_time" -gt "$max_sleep_time" ]
      then
         echo "lzp_backup: `date` semaphore timeout"
         eval $cleanup
         exit 2
      fi
      trap "" 1 2 3 15
   done

   firstreconuser=true
   for userpass in $reconuserlist
   do
      trap "echo INTERRUPT; stty echo; $cleanup; exit 1" 1 2 3 15
      if [ "$recover" ]
      then
         printf "lzp_backup: $recover\n\n"
         printf "WARNING:  You are about to recover objects into the\n"
         printf "          `dirname $userpass` database user account...\n\n"
         printf "          If you are unsure of the recovery options\n"
         printf "          simply accept the recommended defaults.\n"
         printf "          In general, existing objects will NOT be\n"
         printf "          overwritten, only added to.  You may wish\n"
         printf "          to drop objects manually before recovery.\n"
         printf "          Beware that there may be multiple user\n"
         printf "          accounts per database instance.\n\n"
         printf "lzp_backup: $recover\n"
         printf "Import into `dirname $userpass` [N]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         if [ "$answer" != "Y" ]
         then
            echo "lzp_backup: `date` recovery aborted"
            continue
         fi
         printf "\n"
      fi
      answer=N
      while [ "$recover" -a "$answer" != "Y" ]
      do
         if [ ! "$fromuser" -o ! "$touser" ]
         then
            fromuser=`dirname $userpass`; touser=`dirname $userpass`
            grants=N; indexes=Y; rows=Y; commit=Y; ignore=Y; destroy=N
         fi
         printf "Enter original owner [$fromuser]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         if [ "$answer" ]
         then
            fromuser="$answer"
         fi
         printf " Enter current owner [$touser]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         if [ "$answer" ]
         then
            touser="$answer"
         fi
         printf "   Grant permissions [$grants]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         if [ "$answer" ]
         then
            grants="$answer"
         fi
         printf "      Import indexes [$indexes]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         if [ "$answer" ]
         then
            indexes="$answer"
         fi
         printf "   Import table rows [$rows]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         if [ "$answer" ]
         then
            rows="$answer"
         fi
         printf " Commit array insert [$commit]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         if [ "$answer" ]
         then
            commit="$answer"
         fi
         printf "Ignore create errors [$ignore]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         if [ "$answer" ]
         then
            ignore="$answer"
         fi
         printf "Overwrite tablespace [$destroy]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         if [ "$answer" ]
         then
            destroy="$answer"
         fi
         printf "\nAre these parameters correct [N]: "
         read answer
         answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YN"`
         printf "\n"
      done
      trap "" 1 2 3 15

      # obtain reconstruction semaphore to ensure reliable backup
      echo "lzp_backup: `date` oracle user `dirname $userpass`"
      while [ "$dumptype" != "null" -a "$quick" = "false" ]
      do
         echo "lzp_backup: `date` waiting for database"
         /usr/bin/cp /dev/null $tmpfile 2>/dev/null
         sqlplus $userpass 1>/dev/null 2>/dev/null << ENDORACLEARGS
         set pagesize 100
         set linesize 100
         set sqlprompt "$sqlprompt"
         set sqlcontinue "$sqlprompt"
         set sqlnumber OFF
         set heading OFF
         set feedback OFF
         spool $tmpfile
         exec DATABASE_BACKUP('START');
ENDORACLEARGS
         if [ $? != 0 ]
         then
            printf "\nlzp_backup: unable to access database\n\n"
            eval $cleanup
            exit 9
         fi
         failure=`grep "ORA\-" $tmpfile 2>/dev/null`
         success=`grep "DATABASE BACKUP STARTED." $tmpfile 2>/dev/null`
         /usr/bin/cp /dev/null $tmpfile 2>/dev/null
         if [ "$success" -a ! "$failure" ]
         then
            echo "lzp_backup: `date` database ready"
            break
         fi
         trap "echo INTERRUPT; $cleanup; exit 1" 1 2 3 15
         sleep $halt_period
         sleep_time=`expr $sleep_time + $halt_period`
         if [ "$sleep_time" -gt "$max_sleep_time" ]
         then
            echo "lzp_backup: `date` database timeout"
            eval $cleanup
            exit 3
         fi
         trap "" 1 2 3 15
      done

      # perform database backup
      while [ "$dumptype" != "null" -a ! "$recover" ]
      do
         echo "lzp_backup: `date` backup in progress"
         /usr/bin/cp /dev/null $tmpfile 2>/dev/null

         # write export parameters to temporary file
         if [ "$dumptype" = "full" ]
         then
            echo "userid=sys/$syspass" >> $tmpfile
            echo "full=y" >> $tmpfile
         fi
         if [ "$dumptype" = "owner" ]
         then
            echo "userid=$userpass" >> $tmpfile
            echo "full=n" >> $tmpfile
            echo "owner=`dirname $userpass`" >> $tmpfile
         fi
         if [ "$dumptype" = "tables" ]
         then
            echo "userid=$userpass" >> $tmpfile
            echo "full=n" >> $tmpfile
            echo "tables=$tablelist" >> $tmpfile
         fi
         if [ "$dumptype" = "acct" ]
         then
            printf "userid=$dpsuserpass\n" >> $tmpfile
            printf "full=n\n" >> $tmpfile
            printf "constraints=n\n" >> $tmpfile
            printf "grants=n\n" >> $tmpfile
            printf "tables=" >> $tmpfile
            printf "db_GPGroupIndx,"                 >> $tmpfile
            printf "db_GPProductIndx,"               >> $tmpfile
            printf "db_ProdMessageQualityandAcct,"   >> $tmpfile
            printf "db_ProductXfer,"                 >> $tmpfile
            printf "db_ProductSessionQA,"            >> $tmpfile
            printf "db_ProductSessQuality,"          >> $tmpfile
            printf "db_ProductGaps,"                 >> $tmpfile
            printf "db_ProductThreshold,"            >> $tmpfile
            printf "db_DANIndx,"                     >> $tmpfile
            printf "db_LogCatalog,"                  >> $tmpfile
            printf "db_AcqSessionCharacteristics,"   >> $tmpfile
            printf "db_AppDestSchedule,"             >> $tmpfile
            printf "db_FESessionAcct,"               >> $tmpfile
            printf "db_FEVCAppAcct,"                 >> $tmpfile
            printf "db_FEVirtualChannelAcct,"        >> $tmpfile
            printf "db_HST_SessionAcct,"             >> $tmpfile
            printf "db_HST_SIDAcct,"                 >> $tmpfile
            printf "db_PDPSessionDataDiscont,"       >> $tmpfile
            printf "db_PDPSessionsAcct,"             >> $tmpfile
            printf "db_PDPSessVCAppIDAcct,"          >> $tmpfile
            printf "db_ResourceConstituentSchedule," >> $tmpfile
            printf "db_ResourceSchedule,"            >> $tmpfile
            printf "db_RTOSDataReceiptAcct,"         >> $tmpfile
            printf "db_RTOSDataTransmissionAcct,"    >> $tmpfile
            printf "db_RTOSVCAppIDTransAcct,"        >> $tmpfile
            printf "db_RTOSUserSelectionSpec,"       >> $tmpfile
            printf "db_GroupSequenceNumbers,"        >> $tmpfile
            printf "db_FEUnselVCAcct,"               >> $tmpfile
            printf "\n" >> $tmpfile
         fi
         if [ "$verify" = "true" ]
         then
            echo "consistent=y" >> $tmpfile
         fi

         if [ "$dumptype" != "acct" -o "$firstreconuser" = "true" ]
         then
            # cycle existing database backup files
            dumpfile="$dumpdir/$ORACLE_SID"_`dirname $userpass`_"$dumptype.dmp"
            printf "lzp_backup: $dumpfile\n\n"
            dump_count="$max_dump_count"
            while [ "$dump_count" -gt "0" ]
            do
               next_count=`expr $dump_count - 1`
               /usr/bin/mv -f $dumpfile.$next_count $dumpfile.$dump_count 2>/dev/null
               dump_count="$next_count"
            done

            # backup database using temporary parameter file
            trap 'trap "" 1 2 3 15; echo INTERRUPT; break' 1 2 3 15
            echo "file=$dumpfile.0" >> $tmpfile
            exp parfile=$tmpfile
            trap "" 1 2 3 15
         fi

         if [ "$dumptype" = "acct" ]
         then
             # delete recon accounting older than specified number of days
             sqlplus $userpass 1>/dev/null 2>/dev/null << ENDORACLEARGS
             execute cleanup_range ($daystokeep);
             commit;
ENDORACLEARGS

             if [ "$firstreconuser" = "true" ]
             then
                # delete dps accounting older than specified number of days
                sqlplus $dpsuserpass 1>/dev/null 2>/dev/null << ENDORACLEARGS

                execute del_proddata ($daystokeep);
                commit;

                execute del_sessionid ($daystokeep);
                commit;

                execute del_gpseqnum ($daystokeep);
                commit;
ENDORACLEARGS

                if [ $? != 0 ]
                then
                   printf "\nlzp_backup: unable to access database\n\n"
                   break
                fi
             fi
         fi

         printf "\nlzp_backup: `date` backup completed\n"
         break
      done

      # perform configuration backup
      while [ "$configuration" = "true" -a ! "$recover" -a "$firstreconuser" = "true" ]
      do
         echo "lzp_backup: `date` backup in progress"
         if [ -d "$configdir" -a -x "$configdir" -a -w "$configdir" ]
         then
            # cycle existing config directory tar files
            dumpfile="$dumpdir/$ORACLE_SID"_`dirname $userpass`_"config.tar"
            printf "\nlzp_backup: $dumpfile\n\n"
            dump_count="$max_dump_count"
            while [ "$dump_count" -gt "0" ]
            do
               next_count=`expr $dump_count - 1`
               /usr/bin/mv -f $dumpfile.$next_count $dumpfile.$dump_count 2>/dev/null
               dump_count="$next_count"
            done

            # copy hidden files to config directory, then tar
            trap 'trap "" 1 2 3 15; echo INTERRUPT; break' 1 2 3 15
            /usr/bin/chmod u+w $configdir/.* 2>/dev/null
            /usr/bin/cp -p $userhome/.* $userhome/config 2>/dev/null
            tar cfv $dumpfile.0 $userhome/config
            trap "" 1 2 3 15
         else
            printf "\nlzp_backup: permission denied: $configdir\n"
         fi

         printf "\nlzp_backup: `date` backup completed\n"
         break
      done

      # perform database recovery
      while [ "$dumptype" != "null" -a "$recover" ]
      do
         echo "lzp_backup: `date` recovery in progress"
         /usr/bin/cp /dev/null $tmpfile 2>/dev/null

         # write import parameters to temporary file
         if [ "$dumptype" = "full" ]
         then
            echo "userid=sys/$syspass" >> $tmpfile
            echo "full=y" >> $tmpfile
         else
            echo "userid=$userpass" >> $tmpfile
            echo "full=n" >> $tmpfile
         fi
         if [ "$dumptype" = "owner" -o "$dumptype" = "tables" ]
         then
            echo "fromuser=$fromuser" >> $tmpfile
            echo "touser=`dirname $userpass`" >> $tmpfile
         fi
         if [ "$dumptype" = "tables" ]
         then
            echo "tables=$tablelist" >> $tmpfile
         fi
         if [ "$grants" = "true" ]
         then
            echo "grants=y" >> $tmpfile
         fi
         if [ "$indexes" = "true" ]
         then
            echo "indexes=y" >> $tmpfile
         fi
         if [ "$rows" = "true" ]
         then
            echo "rows=y" >> $tmpfile
         fi
         if [ "$commit" = "true" ]
         then
            echo "commit=y" >> $tmpfile
         fi
         if [ "$ignore" = "true" ]
         then
            echo "ignore=y" >> $tmpfile
         fi
         if [ "$destroy" = "true" ]
         then
            echo "destroy=y" >> $tmpfile
         fi

         # recover database using temporary parameter file
         trap 'trap "" 1 2 3 15; echo INTERRUPT; break' 1 2 3 15
         echo "file=$recover" >> $tmpfile
         printf "lzp_backup: $recover\n\n"
         imp parfile=$tmpfile
         trap "" 1 2 3 15

         printf "\nlzp_backup: `date` recovery completed\n"
         break
      done

      # release the reconstruction semaphore
      while [ "$dumptype" != "null" -a "$quick" = "false" ]
      do
         /usr/bin/cp /dev/null $tmpfile 2>/dev/null
         sqlplus $userpass 1>/dev/null 2>/dev/null << ENDORACLEARGS
         set pagesize 100
         set linesize 100
         set sqlprompt "$sqlprompt"
         set sqlcontinue "$sqlprompt"
         set sqlnumber OFF
         set heading OFF
         set feedback OFF
         spool $tmpfile
         exec DATABASE_BACKUP('STOP');
ENDORACLEARGS
         if [ $? != 0 ]
         then
            printf "\nlzp_backup: unable to access database\n\n"
            eval $cleanup
            exit 9
         fi
         failure=`grep "ORA\-" $tmpfile 2>/dev/null`
         success=`grep "DATABASE BACKUP STOPPED." $tmpfile 2>/dev/null`
         /usr/bin/cp /dev/null $tmpfile 2>/dev/null
         if [ "$success" -a ! "$failure" ]
         then
            echo "lzp_backup: `date` database ready"
         else
            echo "lzp_backup: `date` database error"
         fi
         break
      done

      firstreconuser=false
   done

done
eval $cleanup
exit 0
