首页 » ORACLE, 系统相关 » Backup ASM metadata with shell

Backup ASM metadata with shell

Creates a backup directory if required, works out the ASM SID, renames the previous entry, then loops round backing up the metadata data for each diskgroup. It also lists each file backed up which is quite helpful.
Finally it creates error checking and a log and error file outputs

[grid@rac1 ~]$ cat  asm_metadata_bk.sh   
#!/bin/sh
#
# Date  Version Author          Comments
# 09/06/09      1.1             Initial Version  by James Hardaker
# 10/06/09      1.2             Added Error Handling by John Hallas
# 04/22/13      1.3             Modify md_backup options by Anbob Weejar 
#
#
# 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 ASM owner user, eg. grid 
 
# Tunables
###
 
BACKUPDIR=/u01/app/backup
 
###
 
##########
# 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

It’s worked in oracle 11r2 on linux

take an example:

sys@ANBOB>select group_number,state,substr(library,1,28) lib,path,failgroup from v$asm_disk;

GROUP_NUMBER STATE    LIB                          PATH                           FAILGROUP
------------ -------- ---------------------------- ------------------------------ ------------------------------
           1 NORMAL   ASM Library - Generic Linux, ORCL:DATA1                     DATA1
           1 NORMAL   ASM Library - Generic Linux, ORCL:DATA2                     DATA2
           1 NORMAL   ASM Library - Generic Linux, ORCL:DATA3                     DATA3

sys@ANBOB>select  GROUP_NUMBER,name,type,state,total_MB from v$asm_diskgroup;

GROUP_NUMBER NAME                           TYPE   STATE         TOTAL_MB
------------ ------------------------------ ------ ----------- ----------
           1 DATA                           NORMAL CONNECTED         9201

[grid@rac1 ~]$ ./asm_metadata_bk.sh    
INFO: ASM Metadata backup started at Mon Apr 22 02:11:54 PDT 2013

INFO: This script is being run as user grid

INFO: Backup of 'DATA' diskgroup started at Mon Apr 22 02:11:55 PDT 2013
INFO: Backing up the DATA diskgroup to /u01/app/backup/asm_metadata_DATA.bkp
Disk group metadata to be backed up: DATA
Current alias directory path: ANBOB/ARCHIVELOG/2012_09_11
Current alias directory path: ANBOB/TEMPFILE
Current alias directory path: ANBOB/ARCHIVELOG/2013_04_20
Current alias directory path: ASM/ARCHIVELOG
Current alias directory path: ANBOB/BACKUPSET/2012_09_11
Current alias directory path: ANBOB/ARCHIVELOG/2013_04_21
Current alias directory path: ASM
Current alias directory path: ANBOB/ARCHIVELOG/2013_04_19
Current alias directory path: ANBOB/ARCHIVELOG/2013_04_16
Current alias directory path: ANBOB/ONLINELOG
Current alias directory path: ANBOB
Current alias directory path: ANBOB/ARCHIVELOG
Current alias directory path: ANBOB/ARCHIVELOG/2012_09_13
Current alias directory path: ASM/ASMPARAMETERFILE
Current alias directory path: ANBOB/ARCHIVELOG/2013_04_17
Current alias directory path: ANBOB/ARCHIVELOG/2013_04_18
Current alias directory path: ANBOB/PARAMETERFILE
Current alias directory path: ANBOB/DATAFILE
Current alias directory path: ANBOB/BACKUPSET
Current alias directory path: ANBOB/ARCHIVELOG/2012_09_10
Current alias directory path: ANBOB/CONTROLFILE
INFO: Backup of DATA diskgroup completed at Mon Apr 22 02:11:57 PDT 2013

INFO: ASM Metadata backup completed at Mon Apr 22 02:11:57 PDT 2013
[grid@rac1 ~]$ ll /u01/app/backup/
total 36
-rw-r--r-- 1 grid oinstall 26545 Apr 22 02:11 asm_metadata_DATA.bkp
-rw-r--r-- 1 grid oinstall     0 Apr 22 02:11 asm_metadata_DATA.err

[grid@rac1 backup]$ vi asm_metadata_DATA.bkp
@diskgroup_set = (
{
‘ATTRINFO’ => {
‘TEMPLATE.PARAMETERFILE.MIRROR_REGION’ => ‘0’,
‘TEMPLATE.TEMPFILE.REDUNDANCY’ => ’18’,
‘TEMPLATE.OCRBACKUP.REDUNDANCY’ => ’18’,
‘TEMPLATE.TEMPFILE.MIRROR_REGION’ => ‘0’,
‘TEMPLATE.PARAMETERFILE.REDUNDANCY’ => ’18’,
‘SECTOR_SIZE’ => ‘512’,
‘TEMPLATE.DATAGUARDCONFIG.REDUNDANCY’ => ’18’,
‘ACCESS_CONTROL.UMASK’ => ‘066’,
‘TEMPLATE.ASM_STALE.STRIPE’ => ‘0’,
‘TEMPLATE.ARCHIVELOG.STRIPE’ => ‘0’,
‘TEMPLATE.DATAGUARDCONFIG.PRIMARY_REGION’ => ‘0’,
‘TEMPLATE.OCRBACKUP.STRIPE’ => ‘0’,
‘TEMPLATE.DATAFILE.STRIPE’ => ‘0’,
‘AU_SIZE’ => ‘1048576’,
‘TEMPLATE.ASMPARAMETERFILE.STRIPE’ => ‘0’,
‘TEMPLATE.CHANGETRACKING.PRIMARY_REGION’ => ‘0’,
‘TEMPLATE.CONTROLFILE.REDUNDANCY’ => ’19’,
‘TEMPLATE.FLASHFILE.STRIPE’ => ‘0’,
‘TEMPLATE.BACKUPSET.PRIMARY_REGION’ => ‘0’,
‘TEMPLATE.ARCHIVELOG.PRIMARY_REGION’ => ‘0’,
‘TEMPLATE.CHANGETRACKING.STRIPE’ => ‘0’,
‘TEMPLATE.ONLINELOG.PRIMARY_REGION’ => ‘0’,
‘TEMPLATE.CHANGETRACKING.REDUNDANCY’ => ’18’,
‘TEMPLATE.BACKUPSET.STRIPE’ => ‘0’,
‘TEMPLATE.OCRFILE.MIRROR_REGION’ => ‘0’,
‘DISK_REPAIR_TIME’ => ‘3.6h’,
‘TEMPLATE.FLASHBACK.MIRROR_REGION’ => ‘0’,
….

md_restore -S option can only generate restore script does not perform recovery actions :

ASMCMD> md_restore -S create_oradg.sql asm_metadata_DATA.bkp                               
Current Diskgroup metadata being restored: DATA
ASMCMD> exit
[grid@rac1 backup]$ ll
total 84
-rw-r--r-- 1 grid oinstall 26545 Apr 22 02:13 asm_metadata_DATA.bkp
-rw-r--r-- 1 grid oinstall     0 Apr 22 02:13 asm_metadata_DATA.err
-rw-r--r-- 1 grid oinstall 26545 Apr 22 02:13 asm_metadata_DATA.old
-rw-r--r-- 1 grid oinstall 11105 Apr 22 02:18 create_oradg.sql
[grid@rac1 backup]$ vi create_oradg.sql 
create diskgroup DATA NORMAL redundancy failgroup DATA3 disk 'ORCL:DATA3' name DATA3 size 3067M failgroup DATA2 disk 'ORCL:DATA2' name DATA2 size 3067M fail
group DATA1 disk 'ORCL:DATA1' name DATA1 size 3067M attribute 'compatible.asm' = '11.2.0.0.0' , 'compatible.rdbms' = '10.1.0.0.0' , 'au_size' = '1048576', '
sector_size' = '512', 'cell.smart_scan_capable' = 'FALSE';
alter diskgroup /*ASMCMD AMBR*/DATA set attribute 'TEMPLATE.OCRBACKUP.REDUNDANCY' = '18';
alter diskgroup /*ASMCMD AMBR*/DATA set attribute 'TEMPLATE.TEMPFILE.REDUNDANCY' = '18';
alter diskgroup /*ASMCMD AMBR*/DATA set attribute 'TEMPLATE.PARAMETERFILE.MIRROR_REGION' = '0';
alter diskgroup /*ASMCMD AMBR*/DATA set attribute 'TEMPLATE.TEMPFILE.MIRROR_REGION' = '0';
alter diskgroup /*ASMCMD AMBR*/DATA set attribute 'TEMPLATE.PARAMETERFILE.REDUNDANCY' = '18';
...
打赏

, ,

对不起,这篇文章暂时关闭评论。