ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 一个拷贝archivelog从ASM到本地磁盘的SHELL

一个拷贝archivelog从ASM到本地磁盘的SHELL

原创 Linux操作系统 作者:yexichang 时间:2009-02-15 13:37:13 0 删除 编辑
众所周知,asm和文件系统之间拷贝文件很不方面,写了个简单的shell,为了某些特殊场合的需要,稍做修改就可以改成拷贝任何文件从asm到本地磁盘

拷贝archivelog从ASM到本地磁盘

#!/bin/sh

######################### read parameter ###############################
read -p "Please input mode b(backup from ASM) or r(restore to ASM)(default b) " $MODE
read -p "Please input start time(FORMAT:YYYYMMDD HH24:MI)(default today 0:00): " STARTTIME
read -p "Please input end time(FORMAT:YYYYMMDD HH24:MI)(default now): " ENDTIME

######################### configuration ################################
ORALOGIN='sqlplus -s / as sysdba'

DB_NAME=pcard                           # database name
TAGET_BASE=/L1work/backup/archbk
ORA_ASM_DIR=ARCH_ASM
ORA_LOCAL_DIR=ARCH_LOCAL

################### start function definition ##########################
function GET_DB_NAME
{
  echo "SET PAGESIZE 0"
  echo "SET FEEDBACK OFF"
  echo "SELECT DISTINCT NAME FROM v\$database"
  echo "where upper(name)=upper('$DB_NAME');"
  echo "exit"
}

function GETFNAME
{
echo "set pagesize 0"
echo "set feed off"
echo "select NAME from v\$archived_log where COMPLETION_TIME>=to_date('$STARTTIME','YYYYMMDD HH24:MI')"
echo " and COMPLETION_TIME<=to_date('$ENDTIME','YYYYMMDD HH24:MI:SS');"
echo "exit"
}

function MK_ORADIR
{
    if [ -z $1 -o -z $2 ]
    then
        echo "Please indicate both Oracle Directory name and actual directory path(ASM path or local path)."
        exit 1
    fi
    echo "Creating directory $1 as '$2' ..."
$ORALOGIN< create or replace directory $1 as '$2';
exit;
EOF1
}

function COPYARCHLOG
{
    if [ -z $1 ]
    then
        echo "No target archivelog exists! "
        exit 1
    fi

    if [ -z $MODE ]
    then
        MODE="b"
    fi
    if [ $MODE == "b" ]
    then
        S_DIR=$ORA_ASM_DIR
        D_DIR=$ORA_LOCAL_DIR
    else if [ $MODE = "r" ]
         then
             D_DIR=$ORA_ASM_DIR
             S_DIR=$ORA_LOCAL_DIR
         fi
    fi
    echo "Copying $1 from $S_DIR to $D_DIR now"
$ORALOGIN< BEGIN
DBMS_FILE_TRANSFER.COPY_FILE(
SOURCE_DIRECTORY_OBJECT=>'$S_DIR',
SOURCE_FILE_NAME=>'$1',
DESTINATION_DIRECTORY_OBJECT=>'$D_DIR',
DESTINATION_FILE_NAME=>'$1');
END;
/
exit;
EOF2
}
################### end function definition ##########################

################### start main #######################################
# Get the db name
if [ -z `GET_DB_NAME | $ORALOGIN` ]
then
   echo "Please execute this shell on $DB_NAME DB server! "
   exit 1
fi

# Archive current log
echo "Archive current log..."
$ORALOGIN< alter system archive log current;
exit;
EOF3
sleep 20;

# Get the start/end time
if [ -z "$STARTTIME" ]
then
    STARTTIME=`date '+%Y%m%d'`" 00:00"
fi
if [ -z "$ENDTIME" ]
then
    ENDTIME=`date '+%Y%m%d %H:%M:%S'`
fi
echo $ENDTIME
# Check TAGET_BASE folder
if [ ! -d $TAGET_BASE ]
then
    mkdir -p $TAGET_BASE
fi

# Identify is there any archivelog in the time period you want
FULLNAME=`GETFNAME | $ORALOGIN |grep "+" |head -n 1`
if [ -z $FULLNAME ]
then
    echo "No archive log during $STARTTIME to $ENDTIME"
    exit 0
fi

# Make directory of Oracle
ARCH_DIR_NAME=`dirname $FULLNAME`
MK_ORADIR $ORA_ASM_DIR $ARCH_DIR_NAME
MK_ORADIR $ORA_LOCAL_DIR $TAGET_BASE

# Get the archivelog name
GETFNAME | $ORALOGIN |while read FNAME
do
    ARC_NAME=`basename $FNAME`
    # If target archivelog exists, skip it
    if [ -e $TAGET_BASE/$ARC_NAME* ]
    then
        echo "$ARC_NAME has been copied to $TAGET_BASE. Skip it."
        continue 2
    fi
    # Copy archivelog
    COPYARCHLOG $ARC_NAME
    if [ $? -gt 0 ]
    then
        echo "Copy file error!Abnormal exited."
        exit 1
    fi
    # After backup, compress the archivelog in local file system
    cd $TAGET_BASE
    gzip -f $ARC_NAME
done

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/317003/viewspace-551954/,如需转载,请注明出处,否则将追究法律责任。

上一篇: dailycheck内容
下一篇: Chanel [Kamus]
请登录后发表评论 登录
全部评论

注册时间:2008-08-30

  • 博文量
    32
  • 访问量
    36424