#!/bin/bash

# -------------------------------------------------------
# file   : export8k.sh
# purpose: Installation script to export data from system.
# author : Vittal.S
#
# usage  : export8k
#
# modifications:
# 09-05-08  Initial Creation .
# -------------------------------------------------------

### Constants
_SELF_="$(basename "$(readlink -e "${BASH_SOURCE[0]}")" && echo X)" && \
readonly _SELF_="${_SELF_%$'\nX'}"
_SELFDIR_="$(dirname "$(readlink -e "${BASH_SOURCE[0]}")" && echo X)" && \
readonly _SELFDIR_="${_SELFDIR_%$'\nX'}"
readonly COMMONINC="${_SELFDIR_}/common8k"

chmod o+r /etc/login.defs

### Include files
source ${COMMONINC} || exit 1
#Check if osv_version is available
OSV_VERSION=$(whereis -b osv_version.sh | awk '{ print $2 }')
if [ ! -f "$OSV_VERSION" ]; then
   logger --id $$ --priority user.warning "$0: osv_version.sh not found in \$PATH ($PATH), exiting!" >/dev/null 2>&1
   exit 1
fi


# ----------------------------------------------------------------
# function: rssh
# args    : none
# description:
#   Make sure  to execure command for partner only in regular mode.
# ----------------------------------------------------------------

rssh( )
{
   if [ "$RECOVER8k" = "" ];
   then
      ssh $*
   else
      echo "   - Skipping ssh to partner"
   fi
} 2>/dev/null

rscp( )
{
   if [ "$RECOVER8k" = "" ];
   then
      scp $*
   else
      echo "   - Skipping scp to partner"
   fi
} 2>/dev/null


# ----------------------------------------------------------
# function: checkrecovery
# description:
#   checks for recovery mode. Toolkit will try to help you
#   if your hardware on one node failed.
# ----------------------------------------------------------

checkrecovery( )
{
   if [ "$testbed" = "cluster" ];
   then
      textncolor $BoldCyan "export8k: Checking if partner node is alive."
      prepare8k -quiet -ping 1>>/log/check8k.log 2>&1
      if [ $? -ne 0 ];
      then
         textncolor $BoldRed   "export8k: Remote node is not alive, or in invalid state!."
         textncolor $BoldRed   "export8k: Follow the hints below on each node."
         textncolor $BoldGreen ">Hint#1: Please Check toolkit is installed on both nodes."
         echo                "    - As user root run 'rpm -q UNSPmigration'."
         textncolor $BoldGreen ">Hint#2: Please Check passwords for root,solid,srx have not expired."
         echo                "    - As user root run 'chage -l root'."
         echo                "    - As user root run 'chage -l solid'."
         echo                "    - As user root run 'chage -l srx'."
         textncolor $BoldGreen ">Hint#3: Please Check security has not invalidated access to accounts."
         echo                "    - As user root run 'pam_tally'."
         if [ "$INTERACTIVE8k" = "true" ];
         then
            textncolor $BoldGreen "export8k: You have two options."
            textncolor $BoldCyan  "Warning: Use recovery mode only if you are sure of partner node failure!."  
            echo "   - [1]. Run export in recovery mode. Only Data on current node will be exported."
            echo "   - [2]. Exit. No action."
            echo -n "   - Enter (1=Recover,2=Exit) : "
            read answer
            if [ "$answer" -eq "1" ];
            then
               RECOVER8k="-recover"
            else
               exit $FAIL
            fi
         else
            exit $FAIL
         fi
      fi
   fi
}

# ---------------------------------------------------------------
# function: initvars
# args    : None
# description:
#   just initialize one variables.
# ---------------------------------------------------------------

initvars( )
{
   SUCCESS=0
   FAIL=2
   MISSINGCFGDIR=8
   MAKECFGDIR=9

   BoldGreen="\033[1;32m"
   BoldBlue="\033[1;34m"
   BoldCyan="\033[1;36m"
   BoldWhite="\033[1;37m"
   BoldRed="\033[1;31m"
   UnderLine="\033[4m"
   Yellow="\033[1;33m"
   Norm="\033[0m"

   checkpwd

   CFG8k=/etc/hiq8000/node.cfg
   LOGFILE=/log/prepare8k.log
   TKDIR=/software/toolkit
   export LOG8k4TOOLKIT=${LOGFILE}
   USER=`id -u`
   if [ $USER -ne 0 ];
   then
       textncolor $BoldRed "export8k: You  must be root to run this script."
       exit $FAIL
   fi

   if processCalledByImageUpgrade "$$";
   then
      export LOG8k4TOOLKIT=""
   fi

   if [ "`pwd`" = "/software/toolkit" ];
   then
      textncolor $BoldRed "export8k: Sorry, Move away from /software/toolkit before executing command."
      exit $FAIL
   fi

   #<CheckRSU>
      oplock check
      if [ $? -ne 0 ];
      then
         local pid=$$
         local pidstatus=2
         local runpids=$(pgrep upgrade8k)

         local allpids="${pid} $(getAllAncestors "${pid}")"

         for idx in ${allpids}
         do
            if [ "$idx" = "$runpids" ];
            then
               pidstatus=0
               break;
            fi
         done

         if [ $pidstatus -ne 0 ]; 
         then
            nupgrade=`ps -ef f | grep upgrade8k |                \
                      egrep -v "\-s|\-h|vi|less|more|cat|lock" | \
                      grep -v grep | wc -l`
            if [ "$nupgrade" -ge 2 ];
            then
               echo "> Following processes are running."
               ps -eo pid,command | grep upgrade8k |  grep -v grep
               textncolor $BoldRed "export8k: Upgrade or fallback in progress. Exiting now."
               textncolor $BoldRed "        : Wait for upgrade or fallback to complete."
               exit $FAIL
            fi
         fi
      fi
      oplock clean
   #</CheckRSU>

   mynode="`uname -n`"
   primvar=node_1_name
   secvar=node_2_name

   getcfgvar $CFG8k test_bed testbed
   getcfgvar $CFG8k $primvar primhost
   getcfgvar $CFG8k $secvar  sechost
   nafoxch="`cfgread nafo3`"
   
   primcip="`echo $nafoxch   | awk '{ print $4 }'`"
   seccip="`echo $nafoxch    | awk '{ print $5 }'`"
   
   if [ "$mynode" = "$primhost" ];
   then
      primary=true
      othernode=$sechost
   else
      secondary=true
      othernode=$primhost
   fi

   export Export8kOnly=true
   export INTERACTIVE8k=true
   export LIVE8kUPGRADE=false
   HWFALGS=""
   RECOVER8k=""
   checkrecovery
}

checkprecond( )
{
   #<RtpAtLeast#3>
      setmode $BoldWhite
      su - srx -c "RtpSolid -l" 2>/dev/null | grep -e "PRIMARY[ ]*ACTIVE"
      primstatus=$?
      su - srx -c "RtpSolid -l" 2>/dev/null | grep -e "SECONDARY[ ]*ACTIVE"
      secstatus=$?
      setmode $Norm
      if [ $primstatus -ne 0 -o $secstatus -ne 0 ];
      then
         textncolor $BoldCyan "export8k:Sorry, Check the db/Not in HSB and active."
         exit $FAIL
      fi
   #</RtpAtLeast#3>

   #<TestSShConnectivity>
      rssh $primcip "df -h -P" 1>/dev/null 2>&1
      if [ $? -ne 0 ];
      then
         textncolor $BoldCyan "export8k:Sorry, Cannot ssh between nodes via cluster ip."
         exit $FAIL
      fi
   #</TestSShConnectivity>

}

# ---------------------------------------------------------------
# function: setupseckeys
# args    : None
# description:
#   preserve the sec keys from current install.
# ---------------------------------------------------------------

setupseckeys( )
{
   SECKEYS=/seckeys
   rm -rf ${SECKEYS}/*
   if [ "$testbed" = "cluster" ];
   then
      ssh $othernode "rm -rf ${SECKEYS}/*" 1>/dev/null 2>&1
      if [ $? -ne 0 ];
      then
         textncolor $BoldRed "export8k: Key generation failure."
         exit $FAIL
      fi
   fi

   sourcebuild="`cfgread srx_build_id`"

   # hardware upgrade
   if [ "${HWFALGS}X" = " -hwX" ]
   then
      if findDataPath
      then
         textncolor $BoldRed "export8k: Found data path in ${USER_DATA_DIR}"
      else
         textncolor $BoldRed "export8k: Could not find user data path. Hw migration procedure aborts" 
         exit $FAIL
      fi 
   else
      # normal upgrade
      USER_DATA_DIR="/repository/upload"   
   fi
   
   target_nafo_admin=`fcfgread ${USER_DATA_DIR}/node.cfg.primary nafo0| awk '{print $2}'`
   target_nafo_xch=`fcfgread ${USER_DATA_DIR}/node.cfg.primary nafo3| awk '{print $2}'`

   if [ $primary = "true" ]
   then
      targetbuild="`fcfgread ${USER_DATA_DIR}/node.cfg.primary srx_build_id`"
   else
      targetbuild="`fcfgread ${USER_DATA_DIR}/node.cfg.secondary srx_build_id`"
   fi

   if [ "X${NEED_UMOUNT}X" = "XtrueX" -a "${USER_DATA_DIR}" != "/repository/upload" ]
   then
      umount ${USER_DATA_DIR}
   fi
 
   V9_BUILD_NEW_KEYS="false"

   mkdir -p ${SECKEYS}/primary ${SECKEYS}/secondary
   for fileidx in /etc/ssh/ssh_host_dsa_key              \
                  /etc/ssh/ssh_host_dsa_key.pub          \
                  /etc/ssh/ssh_host_key                  \
                  /etc/ssh/ssh_host_key.pub              \
                  /etc/ssh/ssh_host_rsa_key              \
                  /etc/ssh/ssh_host_rsa_key.pub          \
                  /root/.ssh/id_rsa                      \
                  /root/.ssh/id_rsa.pub                  \
                  /unisphere/srx3000/srx/.ssh/id_rsa     \
                  /unisphere/srx3000/srx/.ssh/id_rsa.pub \
                  /etc/sec/l3_key
   do

      if [ ! -f "$fileidx" ];
      then
         if [ "$fileidx" != "/etc/sec/l3_key" ];
         then
            textncolor $BoldRed "export8k: Key generation failure."
            exit $FAIL
         else
            #<CheckL3Key>
              
               mkdir -p ${SECKEYS}/primary/etc/sec ${SECKEYS}/secondary/etc/sec

               if "$OSV_VERSION" compare "$targetbuild" lt V9;
               then
                  # We are going from v[678] to v[678]
                  textncolor $BoldRed "export8k: Going from $sourcebuild to $targetbuild, keep existing keys logic"                
                  if [ ! -s ${SECKEYS}/primary/etc/sec/l3_key ];
                  then
                     opensslkey=`openssl rand -base64 14`
                     opensslenckey=`openssl rand -base64 24`
                     {
                        echo $opensslkey
                        echo $opensslenckey
                     } > ${SECKEYS}/primary/etc/sec/l3_key
                     cp ${SECKEYS}/primary/etc/sec/l3_key ${SECKEYS}/secondary/etc/sec/l3_key
                  fi
               else
                   # we are going to v9 or above, we will need new keys
                  V9_BUILD_NEW_KEYS="true"
                  textncolor $BoldRed "export8k: Going from $sourcebuild to $targetbuild, we will need to create new keys"                
               fi
            #</CheckL3Key>
            continue
         fi
      else
          if "$OSV_VERSION" compare "$targetbuild" ge V9 && [ "$fileidx" = "/etc/sec/l3_key" ];
          then
                textncolor $BoldRed "export8k: Going from $sourcebuild to $targetbuild, we will need to create new keys although $fileidx alread exists"
                V9_BUILD_NEW_KEYS="true"
          fi

          cp --parents -p $fileidx ${SECKEYS}/primary/.
      fi

      if [ ${V9_BUILD_NEW_KEYS} = "true" ]
      then
         # we are going to V9 or above
         local temp_randfile="/tmp/openssl.rnd"
         local opensslkey1=''
         local opensslkey2=''
         local opensslenckey1=''
         local opensslenckey2=''
         local readonly keysize=24
         local readonly enckeysize=27
          
         # if we are going to V9 and source is v[789] and  xchannel is shared with admin 
         # keep a backup of current key, so they will be temporarily restored later just before commit
         # In V9 after PS12 the setkey.conf might be encrypted in /etc/sec/.setkey.conf, so decrypt and copy
         if "$OSV_VERSION" compare "$sourcebuild" ge V7 && [ "$target_nafo_admin" = "$target_nafo_xch" ];
         then
            if [ ! -f /etc/sec/setkey.conf -a -f /etc/sec/.setkey.conf ]
	    then
               if [ -x /usr/local/bin/.setipsec ]
               then
                  /usr/local/bin/.setipsec -decrypt > /etc/sec/setkey.conf.old_liveupgrade
               else
	          textncolor $BoldRed "export8k : could not decrypt setkey.conf"
                  exit $FAIL
                  
               fi
	    else
               cp -p /etc/sec/setkey.conf /etc/sec/setkey.conf.old_liveupgrade	 
            fi
            textncolor $BoldRed "export8k : build is $sourcebuild , xchannel and admin interfaces are shared , keep a copy of sec keys"
         fi

	 if [ ! -d ${SECKEYS}/primary/etc/sec/ ]
         then
            mkdir -p ${SECKEYS}/primary/etc/sec/
         fi

	 if [ ! -d ${SECKEYS}/secondary/etc/sec/ ]
         then
            mkdir -p ${SECKEYS}/secondary/etc/sec/
         fi

         # we are going to v9 or above, we need new keys
         opensslkey1=$(RANDFILE=${temp_randfile} openssl rand -base64 ${keysize} 2>/dev/null)
         opensslenckey1=$(RANDFILE=${temp_randfile} openssl rand -base64 ${enckeysize} 2>/dev/null)
         opensslkey2=$(RANDFILE=${temp_randfile} openssl rand -base64 ${keysize} 2>/dev/null)
         opensslenckey2=$(RANDFILE=${temp_randfile} openssl rand -base64 ${enckeysize} 2>/dev/null)
                 
         {
            echo $opensslkey1
            echo $opensslenckey1
            echo $opensslkey2
            echo $opensslenckey2
         } > ${SECKEYS}/primary/etc/sec/l3_key   
                         
         textncolor $BoldRed "export8k: Going from $sourcebuild to $targetbuild, created new keys"                
         cp -rf ${SECKEYS}/primary/etc/sec/l3_key ${SECKEYS}/secondary/etc/sec/l3_key

      fi

      if [ "$testbed" = "cluster" ];
      then
         if "$OSV_VERSION" compare "$targetbuild" ge V9 && [ "$fileidx" = "/etc/sec/l3_key" ];
         then
            textncolor $BoldCyan "no need to copy the secondary for $fileidx"
          
         else
            keydir=`dirname $fileidx`
            mkdir -p ${SECKEYS}/secondary/${keydir} 
            scp -p $othernode:$fileidx ${SECKEYS}/secondary/${keydir}/. 1>/dev/null 2>&1
            if [ $? -ne 0 ];
            then
               textncolor $BoldRed "export8k: Key generation failure."
               exit $FAIL
            fi
         fi
      fi

   done

   SOLHOME=/home/solid
   for fileidx in id_rsa  id_rsa.pub
   do
      if [ ! -f "${SOLHOME}/.ssh/$fileidx" ];
      then
         {
            cp --parents -p ${SOLHOME}/. ${SECKEYS}/primary/.
            cp --parents -p ${SOLHOME}/. ${SECKEYS}/secondary/.
         } 2>/dev/null
         mkdir -p ${SECKEYS}/primary/home/solid/.ssh ${SECKEYS}/secondary/home/solid/.ssh
         cp ${SECKEYS}/primary/unisphere/srx3000/srx/.ssh/id_rsa     ${SECKEYS}/primary/home/solid/.ssh/.
         cp ${SECKEYS}/primary/unisphere/srx3000/srx/.ssh/id_rsa.pub ${SECKEYS}/primary/home/solid/.ssh/.  
         chown -R solid.dba ${SECKEYS}/primary/home/solid 
         if [ "$testbed" = "cluster" ];
         then
            cp ${SECKEYS}/secondary/unisphere/srx3000/srx/.ssh/id_rsa ${SECKEYS}/secondary/home/solid/.ssh/.
            cp ${SECKEYS}/secondary/unisphere/srx3000/srx/.ssh/id_rsa.pub ${SECKEYS}/secondary/home/solid/.ssh/.  
            chown -R solid.dba ${SECKEYS}/secondary/home/solid 
         fi
      else
         cp --parents -p ${SOLHOME}/.ssh/$fileidx ${SECKEYS}/primary/.
         if [ "$testbed" = "cluster" ];
         then
            keydir=${SOLHOME}/.ssh
            mkdir -p ${SECKEYS}/secondary/${keydir} 
            scp -p $othernode:${keydir}/$fileidx ${SECKEYS}/secondary/${keydir}/. 1>/dev/null 2>&1
            if [ $? -ne 0 ];
            then
               textncolor $BoldRed "export8k: solid user Key generation failure."
               exit $FAIL
            fi
         fi
      fi
   done

   SECADHOME=/home/secad
   local SECAD_NEW_KEYS=false
   for fileidx in id_rsa  id_rsa.pub
   do
      if [ -f "${SECADHOME}/.ssh/$fileidx" ];
      then
         cp --parents -p ${SECADHOME}/.ssh/$fileidx ${SECKEYS}/primary/.
         if [ "$testbed" = "cluster" ];
         then
            keydir=${SECADHOME}/.ssh
            mkdir -p ${SECKEYS}/secondary/${keydir}
            scp -p $othernode:${keydir}/$fileidx ${SECKEYS}/secondary/${keydir}/. 1>/dev/null 2>&1
            if [ $? -ne 0 ];
            then
               textncolor $BoldRed "export8k: secad user Key generation failure."
               exit $FAIL
            fi
         fi
      else
         SECAD_NEW_KEYS=true 
      fi
   done
   
   if "$OSV_VERSION" compare "$targetbuild" ge V9 && [ "X${SECAD_NEW_KEYS}X" = "XtrueX" ];
   then
      # No secad keys we need to generate a new ones
      textncolor $BoldRed "export8k: No  secad  ssh Keys, will create a new set."
     
      for xr in primary secondary 
      do
         if [ -d ${SECKEYS}/${xr}/${SECADHOME}/.ssh ]
         then
            rm -rf ${SECKEYS}/${xr}/${SECADHOME}/.ssh/*
         else
            mkdir -p ${SECKEYS}/${xr}/${SECADHOME}/.ssh
         fi

         ssh-keygen -t rsa -f ${SECKEYS}/${xr}/${SECADHOME}/.ssh/id_rsa -N "" >>$LOGFILE 2>&1
         if [ $? -ne 0 ]
         then
            textncolor $BoldRed "export8k: Could not create secad ssh Keys for ${xr}"
            exit $FAIL
         fi

         sed -i 's/root/secad/g' ${SECKEYS}/${xr}/${SECADHOME}/.ssh/id_rsa.pub
         chown -R secad:seclog ${SECKEYS}/${xr}/${SECADHOME}/
         chmod -R 600 ${SECKEYS}/${xr}/${SECADHOME}/
         chmod 700 ${SECKEYS}/${xr}/${SECADHOME}

      done
            
   fi

   
   
   if [ "$testbed" = "cluster" ];
   then
      scp -r ${SECKEYS} $othernode:/.
      if [ $? -ne 0 ];
      then
         textncolor $BoldRed "export8k: Key generation failure."
         exit $FAIL
      fi
   fi
}

ARGLIST="$*"
timerstart=$(jobtimer)
initvars
while [ "$#" -gt "0" ];
do
   case $1 in
      -cfg)
          shift
          export CFGMNTPATH=$1
          if [ "$CFGMNTPATH" = "" ];
          then
              textncolor $BoldCyan "Sorry, Specify the cfg path where exported data will be placed."
              exit $MISSINGCFGDIR 
          fi
          if [ "$(ls -A $CFGMNTPATH 2>/dev/null)" ];
          then
             textncolor $BoldCyan "Sorry, cannot export to a directory which already exist."
             exit $MISSINGCFGDIR
          fi
          textncolor $BoldCyan "Exported data will be placed in $CFGMNTPATH"
          mkdir -p $CFGMNTPATH 2>/dev/null
          if [ $? -ne 0 ];
          then
             textncolor $BoldCyan "Sorry, cannot make $CFGMNTPATH."
             exit $MAKECFGDIR
          fi
          if [ "$mynode" = "$primhost" ];
          then
             cp $CFG8k ${CFGMNTPATH}/node.cfg.primary
             if [ "$testbed" = "cluster" ];
             then
                rssh $sechost "mkdir -p $CFGMNTPATH 2>/dev/null; cp $CFG8k ${CFGMNTPATH}/node.cfg.secondary"
             fi
          else
             cp $CFG8k ${CFGMNTPATH}/node.cfg.secondary
             rssh $primhost "mkdir -p $CFGMNTPATH 2>/dev/null; cp $CFG8k ${CFGMNTPATH}/node.cfg.primary"
          fi
          if [ $? -ne 0 ];
          then
             textncolor $BoldCyan "Sorry, cannot make $CFGMNTPATH."
             exit $MAKECFGDIR
          fi
          ;;
        -quiet)
          textncolor $BoldGreen "Information: Quiet mode. No interaction on errors."
          INTERACTIVE8k=false
          ;;
        -recover)
          RECOVER8k="-recover"
          ;;
        -local)
          textncolor $BoldCyan "Exported data placed locally!."
          ;;
        -liveexp)
          textncolor $BoldCyan "Outage free mode!."
          setupseckeys
          ;;
        -zen)
          textncolor $BoldCyan "Checking node.cfg."
          prepare8k -zen
          exit $?
          ;;
        -keys)
          setupseckeys
          exit $?
          ;;
	-isonlyone)
	  isOnlyOne
	  exit $?
	  ;;
        -hw)
          HWFALGS=" -hw"
          ;;
        -ucdbexp)
          textncolor $BoldCyan "Will export symphonia schema and data"
          ;;
         *)
           textncolor $BoldCyan "Sorry, Unknown option."
           echo "Usage: export8k [-cfg]"
           exit $FAIL
           ;;
   esac
   shift
done

# Make sure this is the only job running
if isOnlyOne
then
   :
else
   textncolor $BoldCyan "export8k: Another export8k job already running on $mynode: pid - $PID_JOB_RUNNING"
   echo                 ">Info"
   cat                  $INFLOG 2>/dev/null
   textncolor $BoldRed  "export8k: Terminate existing running job or wait for its completion."
   exit $FAIL
fi
   
if [ "$testbed" = "cluster" ];
then
   #<CheckRemoteAdmin>
      if [ "$RECOVER8k" = "" ];
      then
         rssh -o ConnectTimeout=8 $othernode "id" 1>/dev/null 2>&1
         if [ $? -ne 0 ];
         then
             textncolor $BoldRed  "export8k: Remote node $othernode(Admin network) is not alive."
             exit $FAIL
         fi
      fi
   #</CheckRemoteAdmin>

   rssh $othernode "export8k -isonlyone" 1>/dev/null 2>&1
   if [ $? -ne 0 ];
   then
      rscp $othernode:$PIDLOG /tmp 2>/dev/null
      rscp $othernode:$INFLOG /tmp 2>/dev/null
      PID_JOB_RUNNING="`cat $PIDLOG`"
      textncolor $BoldCyan "export8k: Another export8k job already running on $othernode: pid - $PID_JOB_RUNNING"
      echo                 ">Info"
      cat                  $INFLOG 2>/dev/null
      textncolor $BoldRed  "export8k: Terminate existing running job or wait for its completion."
      exit $FAIL
   fi
fi

if [ "$testbed" = "cluster" -a "$mynode" != "$primhost" ];
then
   textncolor $BoldCyan "export8k:O.k you are on secondary node of cluster. Will still proceed."
   textncolor $BoldCyan "export8k:Unlocking all partitions."
   rm -f  /log/unlock8k.log
   if [ "$RECOVER8k" = "" ];
   then
      rssh $primcip "prepare8k $RECOVER8k -unlock" 1>>/log/unlock8k.log 2>&1 
      rssh $primcip "prepare8k $RECOVER8k $ARGLIST -export"
   else
      prepare8k $RECOVER8k -unlock 1>>/log/unlock8k.log 2>&1
      prepare8k $HWFALGS $RECOVER8k $ARGLIST -export
   fi
   status=$?
   if [ $status -ne 0 ];
   then
      textncolor $BoldRed "Export Failed : `date` Unlocking all partitions."
      if [ "$RECOVER8k" = "" ];
      then
         textncolor $Yellow "export8k: Restoring RTP parameters."
         {
            ZMODIFIED="/opt/SMAW/SMAWrtp/cust_conf/zModifiedParms.parm"
            HIQALARMTCN="/opt/SMAW/SMAWrtp/cust_conf/HiqAlarmFilters.tcn"
            rscp -p -o ConnectTimeout=8 $ZMODIFIED   root@${primcip}:$ZMODIFIED
            rscp -p -o ConnectTimeout=8 $HIQALARMTCN root@${primcip}:$HIQALARMTCN
            rscp -p -o ConnectTimeout=8 root@${primcip}:$ZMODIFIED   $ZMODIFIED
            rscp -p -o ConnectTimeout=8 root@${primcip}:$HIQALARMTCN $HIQALARMTCN
         } 2>/dev/null
         rssh $primcip "prepare8k $RECOVER8k -unlock" 1>>/log/unlock8k.log 2>&1 
      else
         prepare8k $RECOVER8k -unlock 1>>/log/unlock8k.log 2>&1
      fi
   else
      if [ "$RECOVER8k" = "" ];
      then
         textncolor $BoldCyan "export8k:Distributing exported data."
         unset CFGMNTPATH
         rssh $primcip "prepare8k $RECOVER8k -dist"
      fi
      textncolor $BoldCyan "Export completed: `date`."
   fi
else
   textncolor $BoldCyan "export8k:Unlocking all partitions."
   rm -f  /log/unlock8k.log
   prepare8k $RECOVER8k -unlock 1>>/log/unlock8k.log 2>&1
   prepare8k $HWFALGS $RECOVER8k $ARGLIST -export
   status=$?
   if [ $status -ne 0 ];
   then
      textncolor $BoldRed "Export Failed : `date` Unlocking all partitions."
      textncolor $Yellow "export8k: Restoring RTP parameters."
      {
            ZMODIFIED="/opt/SMAW/SMAWrtp/cust_conf/zModifiedParms.parm"
            HIQALARMTCN="/opt/SMAW/SMAWrtp/cust_conf/HiqAlarmFilters.tcn"
            rscp -p -o ConnectTimeout=8 $ZMODIFIED   root@${seccip}:$ZMODIFIED
            rscp -p -o ConnectTimeout=8 $HIQALARMTCN root@${seccip}:$HIQALARMTCN
            rscp -p -o ConnectTimeout=8 root@${seccip}:$ZMODIFIED   $ZMODIFIED
            rscp -p -o ConnectTimeout=8 root@${seccip}:$HIQALARMTCN $HIQALARMTCN
      } 2>/dev/null
      prepare8k $RECOVER8k -unlock 1>>/log/unlock8k.log 2>&1
   else
      if [ "$RECOVER8k" = "" ];
      then
         textncolor $BoldCyan "export8k:Distributing exported data."
         unset CFGMNTPATH
         prepare8k $RECOVER8k -dist
      fi
      textncolor $BoldCyan "Export completed: `date`."
   fi
fi
if [ "$RECOVER8k" != "" ];
then
   cp -R $TKDIR/$mynode $TKDIR/$othernode
   if [ "$primary" = "true" ];
   then
       mv $TKDIR/$mynode/node.cfg.*    $TKDIR/$mynode/node.cfg.primary  
       mv $TKDIR/$othernode/node.cfg.* $TKDIR/$othernode/node.cfg.secondary
   else
       mv $TKDIR/$mynode/node.cfg.*    $TKDIR/$mynode/node.cfg.secondary
       mv $TKDIR/$othernode/node.cfg.* $TKDIR/$othernode/node.cfg.primary
   fi
fi
textncolor $BoldCyan "Elapsed time: $(jobtimer $timerstart)"
exit $status
