Home > Oracle > script to backup Oracle ASM metadata

script to backup Oracle ASM metadata

Script taken from
http://jhdba.wordpress.com/tag/md_backup/

#!/bin/ksh
#
# Date          Version Author          Comments
# 09/06/09      1.1     James Hardaker  Initial Version
# 10/06/09      1.2                     Added Error Handling
# 21/03/10      1.3     Mike desouza    11g compatibility
#
# Description
# Queries ASM for a list of disk groups and backs up the metadata
# of each to /app/oracle/backups
# Should be run as the oracle user

# Tunables
###

BACKUPDIR=/dbbackup/asm

###

##########
# Start of functions
#
# asm_metadata - backup the asm metadata
asm_metadata()
{
   export ORACLE_SID=`cat /etc/oratab|grep ^+ASM \
                      |awk 'BEGIN {FS=":"}; {print $1}'`
   export ORACLE_HOME=`cat /etc/oratab|grep ^+ASM \
                      |awk 'BEGIN {FS=":"} {print $2} '`

for DG in `asmcmd ls + | sed 's/\///'`
do
  echo "INFO: Backup of ${DG} diskgroup started at `date`"
  if [ -f ${BACKUPDIR}/asm_metadata_${DG}.bkp ]
  then
    echo "INFO: Moving the file ${BACKUPDIR}/asm_metadata_${DG}.bkp \
          to ${BACKUPDIR}/asm_metadata_${DG}.old"
    cp ${BACKUPDIR}/asm_metadata_${DG}.bkp
    ${BACKUPDIR}/asm_metadata_${DG}.old 2>/dev/null
    if [ $? -ne 0 ]
    then
     echo "WARN: Unable to backup ${BACKUPDIR}/asm_metadata_${DG}.bkp,
     will overwrite it anyway"
    fi
      rm -f ${BACKUPDIR}/asm_metadata_${DG}.bkp 2>/dev/null
  fi
    echo "INFO: Backing up the ${DG} diskgroup to
         ${BACKUPDIR}/asm_metadata_${DG}.bkp"
    rm ${BACKUPDIR}/asm_metadata_${DG}.err 2>/dev/null
    ${ORACLE_HOME}/bin/asmcmd md_backup \ 
    ${BACKUPDIR}/asm_metadata_${DG}.bkp -G ${DG} \
    2>${BACKUPDIR}/asm_metadata_${DG}.err
    if [ -s ${BACKUPDIR}/asm_metadata_${DG}.err ]
    then
       echo ""
       echo "WARN: Unable to backup the ${DG}
             diskgroup to ${BACKUPDIR}/asm_metadata_${DG}.bkp"
       cat ${BACKUPDIR}/asm_metadata_${DG}.err
       echo ""
       ERR=98
    else
       echo "INFO: Backup of ${DG} diskgroup completed at `date`"
    fi
    echo ""
done
}

##########
# End of functions

### Main Program

echo "INFO: ASM Metadata backup started at `date`"
echo ""
echo "INFO: This script is being run as user `/usr/bin/id -un`"
echo ""

# create the backup directory if it does not exist
#

if [ ! -d ${BACKUPDIR} ]
then
    mkdir ${BACKUPDIR} 2>/dev/null
    echo "INFO: Created the directory ${BACKUPDIR} as it did not exist"
    echo ""
    if [ $? -ne 0 ]
    then
      echo "ERROR: Unable to create the directory ${BACKUPDIR}"
      exit 99
    fi
fi

# backup the asm metadata
#
ERR=0
asm_metadata

#Finish
#

if [ ${ERR} != 0 ]
then
    echo "ERROR: An error occurred backing 
          up one or more disk groups, see above"
    exit 98
else
    echo "INFO: ASM Metadata backup completed at `date`"
fi

exit

Advertisements
Categories: Oracle
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: