Oracle数据库RMAN的自动备份脚本简介

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle数据库RMAN的自动备份脚本简介相关的知识,希望对你有一定的参考价值。

参考技术A

Oracle数据库RMAN的自动备份脚本简介

  各位同学知道Oracle数据库RMAN如何自动备份脚本嘛?下面我为大家整理了关于Oracle数据库RMAN的自动备份脚本文章,希望能为你提供帮助:

  1、数据库设置为归档方式

  2、数据库的备份脚本

  db_full_backup.sh :数据库全备脚本

  db_l0_backup.sh :数据库0级备份脚本

  db_l1_backup.sh :数据库1级备份脚本

  ftp.sh :数据FTP上传脚本

  ftp_del.sh :数据FTP清理脚本

  rman_bak.sh :数据备份主程序

  3、备份原理

  每周1、3、6进行0级备份

  每周日、2、4、5进行1级备份

  备份文件上传到FTP服务器

  FTP服务器每周清理一次,但是清理后将周六和周日的备份进行保留(6.bak和0.bak)

  所有工作防暑crontab中自动执行备份

  4、备份目录含义

  arc :数据库归档目录

  rmanbak :数据库备份文件的保存目录

  rmanscripts :数据库脚本存放路径

  5、FTP目录

  ftp上必须手动建立目录

  L0:

  ---1

  ---3

  ---6

  ---6.bak

  L1:

  ---2

  ---4

  ---5

  ---0

  ---0.bak

  rman_bak.sh脚本主程序

  #!/bin/bash

  #--------------------------------------------

  # Oracle auto backup using rman

  #

  # author:songrh

  # week:1,3,6 Level 0 backup

  # 2,4,5,0 Level 1 backup

  # Copyright by ChenLong Tec

  #--------------------------------------------

  #

  #

  export ORACLE_BASE=/u02/oracle

  export ORACLE_HOME=/u02/oracle/product/9.2.4

  export ORACLE_SID=PROD

  export LD_LIBRARY_PATH=$ORACLE_HOME/lib

  export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib:$ORACLE_HOME/network/jlib

  export NLS_LANG=american_america.ZHS16GBK

  export TNS_ADMIN=$ORACLE_HOME/network/admin

  export ORA_NL33=$ORACLE_HOME/ocommon/nls/admin/data

  export PATH=/bin:/usr/bin:/usr/sbin:$ORACLE_HOME/bin:$PATH

  export PATH=$PATH:/opt/local/bin

  #

  SH_PATH=/u02/rmanscripts

  ARC_PATH=/u02/arch

  RMAN_BAK_PATH=/u02/rmanbak

  #

  #FULL_PATH=$RMAN_BAK_PATH/full

  L0_PATH=$RMAN_BAK_PATH/L0

  L1_PATH=$RMAN_BAK_PATH/L1

  #

  #DAY_TAG=`date "%Y-%m-%d"`

  LOG_TAG=`date "%Y-%m-%d"`

  #FIRST_DAY=`date %e`

  WEEK=`date %w`

  #WEEK=1

  #

  # FTP configure

  IP="122.120.150.155"

  FTPUSER="ftpbak"

  FTPPASS="******"

  FTPROOT0="L0"

  FTPROOT1="L1"

  #

  DISK_USE=`df -k |sed -n '/u02/'p | awk 'print $5' |sed 's/%//'`

  ####check path function

  ############

  if [[ $DISK_USE -ge 90 ]]; then

  rm -rf $L0_PATH/*

  rm -rf $L1_PATH/*

  fi

  if [ "$WEEK" = "6" -o "$WEEK" = "3" -o "$WEEK" = "1" ]; then

  if [ ! -d $L0_PATH ]; then

  mkdir $L0_PATH

  fi

  if [ "$WEEK" = "1" ]; then

  rm -rf $L0_PATH/*

  rm -rf $L1_PATH/*

  mkdir $L0_PATH/$WEEK

  $SH_PATH/db_l0_backup.sh $L0_PATH/$WEEK

  cd $L0_PATH/$WEEK

  $SH_PATH/ftp_del.sh $IP $FTPUSER $FTPPASS $FTPROOT0 $WEEK $WEEK_$LOG_TAG.log

  else

  if [ ! -d $L0_PATH/$WEEK ]; then

  mkdir $L0_PATH/$WEEK

  $SH_PATH/db_l0_backup.sh $L0_PATH/$WEEK

  cd $L0_PATH/$WEEK

  $SH_PATH/ftp.sh $IP $FTPUSER $FTPPASS $FTPROOT0 $WEEK $WEEK_$LOG_TAG.log

  else


;

Oracle 自动化备份脚本

Oracle 日常RMAN备份脚本,很基础。但是对于多个需要备份的Oracle数据库,可以很简单实施,并利于后期批量状态的查询。

  1. 备份脚本,基于linux,windows环境需要适当修改。
    主脚本,会调用2,3步骤的rman.sql&status.sql

    $ more main.sh
    #set env
    #########################Change the below parameter for the different server##################
    export host_ip=172.16.32.115
    export instance_name=liang
    export username=liang
    export password=liang
    export syspsw=oracle
    export backup_home=/home/oracle/bk
    export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_1
    export PATH=$PATH:$ORACLE_HOME/db_1/bin:/sbin:/usr/sbin
    #####################################################################################
    export curTime=$(date "+%Y%m%d")
    mkdir $backup_home/ftp/$curTime
    echo "-----------------------------RMAN start-----------------------------";date
    #backup start
    cd $backup_home
    $ORACLE_HOME/bin/rman target sys/[email protected]$host_ip:1521/$instance_name cmdfile=‘rman.sql‘
    echo "------------------------------RMAN End------------------------------";date
    sleep 10
    echo "------------------------------SQL Start------------------------------";date
    $ORACLE_HOME/bin/sqlplus $username/[email protected]$host_ip:1521/$instance_name @status.sql
    echo "------------------------------END-----------------------------";date
  2. RMAN备份sql脚本

    [[email protected] bk]$ more rman.sql
    run
    {
    ALLOCATE CHANNEL node_c1 DEVICE TYPE DISK MAXPIECESIZE=5G;
    ALLOCATE CHANNEL node_c2 DEVICE TYPE DISK MAXPIECESIZE=5G;
    backup as compressed backupset database format ‘C:ftpuploadfiledb_%U.bak_%T‘;
    sql ‘alter system switch logfile‘;
    CROSSCHECK ARCHIVELOG ALL;
    backup as compressed backupset archivelog all format ‘C:ftpuploadfilearchivelog_%d_%s_%p_%T‘ not backed up 2 times;
    backup spfile format ‘C:ftpuploadfilespfile_%U_%T‘;
    backup current controlfile format ‘C:ftpuploadfilecontrolfile_%d_%s_%p_%I_%u_%T‘;
    sql ‘alter system switch logfile‘;
    CROSSCHECK BACKUP;
    CROSSCHECK COPY;
    delete noprompt archivelog all completed before ‘sysdate-7‘;
    release channel node_c1;
    release channel node_c2;
    }
  3. 查询备份状态的脚本,并将查询信息打印到日志,方便查询。
    $ more status.sql
    ----每日归档产生量,可以判断数据库是否繁忙
    spool $backup_home/redo_switch.log;
    set echo off
    set feedback off
    set colsep ‘,‘
    set pagesize 2000
    set term off
    set heading off
    set line 400
    col Count for 9999
    col GB for 99999
    select
    d.dbid,
    to_char(trunc(completion_time),‘yyyy-mm-dd‘) as "Date"
    ,count(*) as "Count"
    ,substr((sum(blocks*block_size))/1024/1024/1024,0,4) as "GB"
    from v$archived_log,v$database d
    group by trunc(completion_time),d.dbid;
    spool off;
    ---查询表空间使用率
    spool $backup_home/tablepace_usage.log;
    set echo off
    set feedback off
    set colsep ‘,‘
    set pagesize 2000
    set term off
    set heading off
    set line 400
    col startup_time for a20
    col status for a6
    col tablespace_name for a20
    col total_mb for 99999999
    col used_mb for 99999999
    col used_pct for a10
    select
    d.dbid,
    to_char(b.STARTUP_TIME,‘yyyy-mm-dd-hh24-mi-ss‘) as startup_time,
    b.status,
    total.tablespace_name,
    round(total.MB, 2) as Total_MB,
    round(total.MB - free.MB, 2) as Used_MB,
    round((1 - free.MB / total.MB) * 100, 2) || ‘%‘ as Used_Pct
    from (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
    from dba_free_space
    group by tablespace_name) free,
    (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
    from dba_data_files
    group by tablespace_name) total, v$instance b, v$database d
    where free.tablespace_name = total.tablespace_name;
    spool off;
    --- 查看近7天备份情况
    spool /home/oracle/bk/log/rman.log;
    set echo off
    set feedback off
    set colsep ‘,‘
    set pagesize 2000
    set term off
    set heading off
    set line 202000
    col DBID for 9999999999
    col status for a25
    col type for a12
    col  start_time for a22
    col  Finish_time for a22
    col in_sec for a12
    col out_sec for a12
    col command for a8
    col  INPUT_M for 99999
    col  OUTPUT_M for 99999
    col obj_type for a15
    select d.DBID as DBID,
    s.status as status,
    b.INPUT_TYPE as type,
    to_char(b.START_TIME,‘yyyy-mm-dd hh24:mi:ss‘) as start_time,
    to_char(b.end_time, ‘yyyy-mm-dd hh24:mi:ss‘) as  Finish_time,
    b.INPUT_BYTES_PER_SEC_DISPLAY in_sec,
    b.OUTPUT_BYTES_PER_SEC_DISPLAY out_sec,
    s.OPERATION as command,
    trunc(s.INPUT_BYTES/1024/1024,2) as INPUT_M,
    trunc(s.OUTPUT_BYTES/1024/1024,2) as OUTPUT_M,
    s.OBJECT_TYPE as obj_type
    from v$rman_status s,v$rman_backup_job_details b, v$database d
    where to_char(s.START_TIME, ‘yyyy-mm-dd hh24:mi:ss‘) < to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘)
    and to_char(s.END_TIME, ‘yyyy-mm-dd hh24:mi:ss‘) > to_char(sysdate-7,‘yyyy-mm-dd hh24:mi:ss‘)
    and s.COMMAND_ID=b.COMMAND_ID
    order by s.START_TIME desc ;
    spool off;
    exit;

以上是关于Oracle数据库RMAN的自动备份脚本简介的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本 rman备份

Oracle 自动化备份脚本

利用SHELL脚本来验证Oracle数据库RMAN备份集的有效性

Oracle数据库RMAN备份恢复脚本分享,附SQL实时查看进度

Oracle数据库RMAN备份恢复脚本分享,附SQL实时查看进度

RMAN 报:ORA-19504 ORA-27038