Subject: Script to convert onstat -d to onspaces commands
From: [email-masked]
Date: Mon, 15 Sep 1997 10:38:56 -0600
Newsgroups: comp.databases.informix

#!/bin/sh
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# copy_spaces.sh   --  A program to convert dbspace/chunk information
#                      available  from the [on|tb]stat -d command into onspaces
#                      commands.
#
#                      Comes in very handy if you need to duplicate the dbspace
#                      setup onto an another instance or if you have to rebuild
#                      an instance.
#
#                      Currently, blobspaces will not be reproduced by this
#                      script.  Also, it is assumed that none of the dbspaces
#                      are being recovered, either physically or logically.
#
#                      Output will go to standard output
#
#        +-----------------------------------------------------------+
#        | Pete Stiglich     DBA            (602)-941-6227           |
#        | ProMark One Marketing Services   [email-masked]  |
#        | http://www.promarkone.com                                 |
#        +-----------------------------------------------------------+
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# This program comes as is.  Standard disclaimers apply.
# Let me know your thoughts.  I will try to answer any questions.
# This was written on a Solaris 2.5 machine.  If nawk gives you problems you
# might need to pass the variable at the end of the nawk script instead of
# using the -v at the start of the nawk script.  Here is an example:
#
# VAR="xyz"
# nawk ' if ($1 == var) { print $0 } ' var=$VAR /tmp/foobar
# instead of
# nawk -v var=$VAR ' if ($1 == var) { print $0 } ' /tmp/foobar
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

# Determine Engine version

if [ -x $INFORMIXDIR/bin/onstat ]
then
   STAT="onstat"
else
   STAT="tbstat"
fi
# Get dbspace information

# Get the number, first chunk,flags and dbspace name
$STAT -d | sed '1,5d' | awk ' BEGIN { i=0 } {
if ($1 == "Chunks") { i=1 }
if (i == 0 && NF == 9) { printf ("%s %s %s %s %s\n", $2,$4,$6,$9,$7) }
if (i == 0 && NF == 8) { printf ("%s %s %s %s\n", $2,$4,$6,$8) }
} ' >/tmp/dbsp.$$

# Get chunk information

# Get the chunk number, dbs number, offset, size,flags &  path
$STAT -d | awk ' BEGIN { i=0 } {
if ($1 == "Chunks") { i=1 }
if (i == 1 && $1 != "address" && NF == 8) {
   printf ("%s %s %s %s %s %s\n", $2,$3,$4,$5,$7,$8 )
   }
} ' >/tmp/chunk.$$

# Just get primary chunks
for CHUNK in `cat /tmp/chunk.$$ |\
   awk ' { if ( substr($5,1,1) == "P")  print $1 }'`
do

  # Don't reproduce the onspaces for the 1'st root dbspace chunk

  if [ $CHUNK -eq 1 ]
  then
     continue
  fi

  DBSNUM=`grep $CHUNK /tmp/chunk.$$ |\
     nawk -v chunk=$CHUNK ' { if ($1 == chunk && substr($5,1,1) == "P")
          { print $2 } } '`

  OFFSET=`grep $CHUNK /tmp/chunk.$$ |\
     nawk -v chunk=$CHUNK ' { if ($1 == chunk && substr($5,1,1) == "P")
          { print $3 } } '`

  SIZE=`grep $CHUNK /tmp/chunk.$$ |\
     nawk -v chunk=$CHUNK ' { if ($1 == chunk && substr($5,1,1) == "P")
          { print $4 } } '`

  CHKPTH=`grep $CHUNK /tmp/chunk.$$ |\
     nawk -v chunk=$CHUNK ' { if ($1 == chunk && substr($5,1,1) == "P")
                print $6  } '`

  # Is this the first chunk in the dbspace?

  ISFIRST=0
  for i in `grep $CHUNK /tmp/dbsp.$$ | awk ' { print $2 } '`
  do
    if [ $i -eq $CHUNK ]
    then
       ISFIRST=$CHUNK
       break
    else
       ISFIRST=0
    fi
  done

  MIRRORED=`grep $DBSNUM /tmp/dbsp.$$ |\
     nawk -v dbsnum=$DBSNUM ' { if ($1 == dbsnum) print $3 } '`

  if [ "$MIRRORED" = "M" ]
  then
     MPATH=`grep $CHUNK /tmp/chunk.$$ |\
       nawk -v chunk=$CHUNK ' { if ( $1 == chunk && substr($5,1,1) == "M")
            { print $6 } } '`

     MOFFSET=`grep $CHUNK /tmp/chunk.$$ |\
       nawk -v chunk=$CHUNK ' { if ( $1 == chunk && substr($5,1,1) == "M")
            { print $3 } } '`

     MPATH="-m $MPATH"
  else
     MPATH=""
     MOFFSET=""
  fi

  DBSPACE=`grep $DBSNUM /tmp/dbsp.$$ |\
     nawk -v dbsnum=$DBSNUM ' { if ($1 == dbsnum) { print $4 } } '`

  ISTEMP=`grep $DBSPACE /tmp/dbsp.$$ |\
              awk ' { if (NF == 5 && $5 == "T") printf ("%s", "-t")
                      else printf ("%s", "") } '`

  echo "DBSPACE $DBSNUM $DBSPACE  --  CHUNK $CHUNK "

# Convert pages to kbytes

   OFFSET=`expr $OFFSET \* 2`
   SIZE=`expr $SIZE \* 2`
   if [ MOFFSET ]
   then
      MOFFSET=`expr MOFFSET \* 2`
   fi

# Output commands to build dbspaces/chunks

  if [ "$STAT" = "onstat" ]
  then
     SPACES="onspaces"
  else
     SPACES="tbspaces"
  fi

  if [ $ISFIRST -ne 0 ]  then  echo "$SPACES -c -d $DBSPACE $ISTEMP -p
$CHKPTH -o $OFFSET -s $SIZE $MPATH $MOFFSET"  else  echo "$SPACES -a
$DBSPACE -p $CHKPTH -o $OFFSET -s $SIZE $MPATH $MOFFSET"  fi done

rm /tmp/dbsp.$$
rm /tmp/chunk.$$