阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Shell脚本实现Oracle 数据库备份

206次阅读
没有评论

共计 2539 个字符,预计需要花费 7 分钟才能阅读完成。

本文是一个 Shell 脚本。主要用于 Oracle 数据库备份。默认情况下,在周一晚上进行全备。其他时间进行累积增量备份。

使用方法:

假如脚本保存名为: oracle_backup.sh

使用方法为 oracle_backup.sh -p $ORACLE_DIRECTORY -L $BACKUP_LEVEL

ORACLE_DIRECTORY 可从 dba_directories 里查询所得。

BACKUP_LEVEL:0(全备 ) 1 增量。

#!/bin/sh
###############################################################
#: This script is used to backup Oracle database with RMAN.  #
#: A full backup will be taken early moring on Monday .      #
#: I recomment that the incremental backup should be taken on #
#: every day except Monday.                                  #
#: Made by Halberd(Asiainfo-PMO) , 20160827                  #
#: Modify Records:                                            #
#: 1.                                                        #
#: Thu May  4 19:42:54 CST 2017                              #
#: halberd                                                    #
#  add getopts . make the scripts more flexible              #
###############################################################

#: initial environment

source ~/.bash_profile

#: judge if parameters are offered

[$# == 0] && echo -e “Usage :: $0 -[pl]\n p –> backup path \n l –> backup level with 0(full) or 1(incremental) \n examples:: \n $0 -p /home/oracle -l 1” && exit 1

#: variables setting
#:            attach the arguments values to their variables
while getopts p:l: option
do
    case “$option” in
    p)
            BACK_PATH=”$OPTARG”
        ;;
  l)
            if
        [“$OPTARG” == 0] ;
        then
        LEVEL=0
        elif
        [“$OPTARG” == 1] ;
        then
        LEVEL=1
        else        echo “Level 0 or 1 is recommended. You should better choose 0 for full backup or 1 for incremental backup”
                exit 1
        fi
        ;;
        \?)
            echo “Usage:  [-p <PATH>] [-l]”
            echo “-p : the path in which backup files will be allocated”
            echo “-l : RMAN backup level”
            exit 1
        ;;
    esac

done

#:            initial other basic variables

md=`date +%m%d`
weekday=`date +%w`

if [-z “$BACK_PATH”] ;
then
    BACK_PATH=”$ORACLE_HOME”/dbs
    echo -e “WARNING :: The backup piecies will be stored in $ORACLE_HOME/dbs .\n “
fi

if [-z “$LEVEL”] ;
then
    if [“$weekday” == 1] ;
    then LEVEL=0
    else LEVEL=1
    fi
fi
 
BACK_FORMAT=”$BACK_PATH”/db”$LEVEL”‘_%d_%T_%u’

 echo “BACKUP_PATH: $BACK_PATH”
 echo “BACKUP_LEVEL:” “$LEVEL”
 echo “BACKUP_FORMAT: $BACK_FORMAT”

#: generate the rman commands
back_comm=’
run{
allocate channel c1 type disk;\n
allocate channel c2 type disk;\n
allocate channel c3 type disk;\n
backup as compressed backupset incremental level ‘”$LEVEL”‘ format ‘\'”$BACK_FORMAT”\” database include current controlfile plus archivelog delete input;\n
release channel c1;\n
release channel c2;\n
release channel c3;\n
}\n
crosscheck archivelog all;\n
delete noprompt expired archivelog all;\n
report obsolete;\n
delete noprompt obsolete;\n
exit

#: execute rman backup
 echo -e $back_comm| rman target sys/oracle log “$BACK_PATH”/rman_”$md”.log
exit

更多 Oracle 相关信息见 Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-11/148502.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2539字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中