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:\ftp\uploadfile\db_%U.bak_%T‘;
    sql ‘alter system switch logfile‘;
    CROSSCHECK ARCHIVELOG ALL;
    backup as compressed backupset archivelog all format ‘C:\ftp\uploadfile\archivelog_%d_%s_%p_%T‘ not backed up 2 times;
    backup spfile format ‘C:\ftp\uploadfile\spfile_%U_%T‘;
    backup current controlfile format ‘C:\ftp\uploadfile\controlfile_%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;

原文地址:http://blog.51cto.com/hsbxxl/2322690

时间: 2024-10-07 19:47:17

Oracle 自动化备份脚本的相关文章

xtrabackup一键自动化备份脚本 【原创】

xtrabackup一键自动化备份脚本 V1 [原创] xtrabackup备份工具安装与使用 http://990487026.blog.51cto.com/10133282/1697094 需要填写的信息 备份文件路径        backdir=/ASSET.innobackupex mysql用来备份用户   user=root mysql用户的密码     pass=asset 输出的信息保存路径    logfile=/var/www/html/index.html l [[ema

Oracle自动备份脚本的实现

问题描述: Oracle自动备份脚本的实现. 错误提示1: Message file RMAN.msb not found Verify that Oracle_HOME is set properly ...... 错误原因: 自动执行的不能够识别相应的命令,需要在自动备份脚本中显式的声明Oracle的环境变量. 错误提示2: standard in must be a tty ...... 错误原因: 不能在cron使用su或者管道等操作,必须将su命令移动到相关的shell脚本中. 错误提

Linux下Oracle自动备份脚本

#!/bin/bash export ORACLE_HOME=/usr/local/instantclient_11_2export PATH=$ORACLE_HOME:$PATHexport TNS_ADMIN=$ORACLE_HOME/network/adminexport LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH#export NLS_LANG='simplified chinese_china.ZHS16GBK'export NLS_LA

Oracle 自动备份脚本

需求:在每天的23点50自动备份doiido用户的数据库,备份信息保留30天 1:编写oracle环境脚本 $ vi oracle_hj.sh ORACLE_BASE=/doiido/oracle; export ORACLE_BASE ORACLE_HOME=$ORACLE_BASE/102; export ORACLE_HOME ORACLE_SID=orcl; export ORACLE_SID LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIB

Windows:Oracle 11g 备份脚本

@echo off echo ================================================ echo Windows环境下Oracle数据库的自动备份脚本 echo 1. 使用当前日期命名备份文件 echo 2. 自动删除7天前的备份 echo ================================================ ::以"YYYYMMDD"格式取出当前时间 set BACKUPDATE=%date:~0,4%%date:~

Oracle Rman备份脚本与自动删除重复备份文件

#!/bin/sh source /home/oracle/.bash_profile if [ -z $1 ] then echo "Please Input a number,Usage: 0|1|2" exit else RMAN_LEVEL=$1 fi export ORACLE_SID=xxxx export TODAY=`date '+%Y%m%d'` export RMAN_DIR=/backup/archivelog/rman/$ORACLE_SID/$TODAY-$R

oracle expdp 备份脚本

#!/bin/bash#Oracle 环境变量 NLS_LANG=AMERICAN_AMERICA.AL32UTF8 ORACLE_SID=zgw ORACLE_BASE=/opt/oracle ORACLE_HOME=/opt/oracle/11g PATH=$PATH:$ORACLE_HOME/bin:$HOME/bin export ORACLE_SID ORACLE_BASE ORACLE_HOME NLS_LANG PATH #获取时间戳 export BAKUPTIME=`date

oracle自动备份脚本

echo 设置数据服务名 set dcibkdatasource=orcl echo 设置要导出的数据库用户名 set dcibkuser=**mis echo 设置要导出的数据库用户密码 set dcibkpwd=**mis set dcibkpath=%~sdp0备份数据 set dcizipname=%dcibkpath%\%dcibkuser%%date:~0,4%-%date:~5,2%-%date:~8,2%-%time:~0,2%:%time:~3,2%:%time:~6,2%.z

xtrabackup一键自动化备份脚本 V2 版【原创】

备份文件路径        backdir=/ASSET.innobackupex mysql用来备份用户   user=用户名 mysql用户的密码     pass=密码 输出的信息保存路径   logfile=/var/www/html/index.html [[email protected] ~]# cat innobackupex.v2.sh #!/bin/bash backdir=/ASSET.innobackupex user=用户名 pass=密码 logfile=/var/w