Sunday, March 6, 2011

How to remove the sparcing in the oracle temp files

Created a script to remove the sparcing out of the temp datafiles of oracle. Please note that this script will use the existing location of the temp file so make sure that it have enough space:

$ vi removeS.sh
#!/bin/sh

# Script to expand the datafile to the maxsize
# Created by : Jeet
# Dated : 06-MARCH-2011

COMD_1="H"
TS_NAME=""
TAR=".tar"
count=0
ANOTHER_FILE="Y"

#Function to bring tablespace offline
tsOffline()
{
  echo 'connect / as sysdba'  >> ./tmp.file
  echo 'alter tablespace ' $TS_NAME ' tempfile offline;' >> ./tmp.file
  echo 'exit' >> ./tmp.file
  sqlplus -s /nolog < ./tmp.file
  rm ./tmp.file
}

tsOnline()
{
  echo 'connect / as sysdba'  >> ./tmp.file
  echo 'alter tablespace ' $TS_NAME ' tempfile online;' >> ./tmp.file
  echo 'exit' >> ./tmp.file
  sqlplus -s /nolog < ./tmp.file
  rm ./tmp.file
}

initInput()
{

while [[ $ANOTHER_FILE = "Y"  ||  $ANOTHER_FILE = "y" ]]
  do
    
    read -p "Enter file location........................................: " FILE_LOCATION
    FILE_ARR[$count]=$FILE_LOCATION
    count=`expr $count + 1`
    read -p "Do you want to enter another filename ? ( Y/y/N/n ) .......: " ANOTHER_FILE

done

  echo ""
  echo ""
  echo "**********************************************************"
  echo "Verify if the file name(s) is/are correct"
  echo "**********************************************************"
  echo "Entered file name(s): "

for i in ${FILE_ARR[@]}
do
  echo $i

done
  echo ""
  echo ""
  echo "if you want to re-enter tablespace name and file name then press any other character other than (Y/y/N/n)"
  read -p "Do you want to expand this file ( Y/y/N/n or any other Char ): " COMD_1
}

generateFileName()
{

for FILE_L in ${FILE_ARR[@]}
do

  # generate the file name
  F_NAME=`ls -tr $FILE_L | awk -F/ '{ print $NF }'`
  echo " "
  echo " "
  echo "File under operation is: $F_NAME"
  echo "Location of the file is: " $FILE_L
  #Change get the directory and work on that

  F_DIR=`dirname $FILE_L`
  cd $F_DIR

  # Start tablespace offline
  # Bringing tablespace offline
  tsOffline

  # Modification to the file
  fileMods

  # Tablespace online
  # Bringing tablespace online
  tsOnline
    
  echo "Operation on Done on File: " $FILE_L
done

}

fileMods()
{
  # Get the original size
  du -h $F_NAME

  # Start Tar
  TEMP_NAME=$F_NAME$TAR
  tar cvf $TEMP_NAME $F_NAME

  # Remove sparce file
  rm $F_NAME

  # Un Tar
  tar xvf $TEMP_NAME

  #Remove the tar file
  rm $TEMP_NAME

  # Get the new siz
  du -h $F_NAME
}

# Show all tablespace names
showTS()
{
  echo 'connect / as sysdba'  >> ./tmp.file
  echo 'select distinct tablespace_name "Temporary TableSpace Name" from dba_temp_files order by tablespace_name;' >> ./tmp.file
  echo 'exit' >> ./tmp.file
  sqlplus -s /nolog < ./tmp.file
  rm ./tmp.file
}

# show all files under the tablespace
showTSFileNames()
{
  A="select file_name from dba_temp_files where tablespace_name =upper('"
  B="') order by file_name;"
  C=$A$TS_NAME$B
  echo 'connect / as sysdba' >> ./tmp.file
  echo $C >> ./tmp.file
  echo 'exit' >> ./tmp.file
  sqlplus -s /nolog < ./tmp.file
  rm ./tmp.file
}

# use input for the tablespace name
getUserTSInput()
{
  read -p 'Enter the name of the tablespace on which you want to work: ' TS_NAME
}


preCheck ()
{
  rm ./tmp.file
}


echo "=================================================="
echo "Utility is used to expand the file to its max size"
echo "=================================================="



while [[ $COMD_1 != "Y"  &&  $COMD_1 != "y"  &&  $COMD_1 != "N"  &&  $COMD_1 != "n" ]]
  do

  # Do a precheck if the temp file already exists then delete it else do nothing
  preCheck

  # Show the current tablespaces
  showTS

  # get user input to find the tablespace on which user want to work
  getUserTSInput

  # show all files under the tablespace
  showTSFileNames

  # as user to enter initial inputs
  initInput

  if [ $COMD_1 = "Y" ] || [ $COMD_1 = "y" ]
    then
      # Get the inputs from user
      generateFileName
      echo " "
      echo "Sparce files are converted to non sparce files"
      echo " "
    else
      if [ $COMD_1 = "N" ] || [ $COMD_1 = "n" ]
        then
          echo "Nothing is done try again later"
      else
        echo "Enter either Y/N "
    fi
  fi
done

No comments: