oracle,RMAN恢复数据库数据到指定时间,RMAN不完全恢复shell脚本

oracle,RMAN恢复数据库数据到指定时间,RMAN不完全恢复shell脚本

RMAN备份配置,shell脚本见:https://blog.csdn.net/weixin_43614067/article/details/109647639

su - oracle
sqlplus / as sysdba

#设置时间格式
SQL> ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS';
#查看数据库原型(当前原型为2)
SQL> SELECT INCARNATION#,STATUS,RESETLOGS_TIME FROM V$DATABASE_INCARNATION;

INCARNATION# STATUS  RESETLOGS_TIME
------------ ------- -------------------
           1 PARENT  2009-08-15 00:16:43
           2 CURRENT 2020-10-22 10:13:52
           3 ORPHAN  2020-11-13 09:31:38
           4 ORPHAN  2020-11-16 17:09:12
           5 ORPHAN  2020-11-16 17:11:07
           6 ORPHAN  2020-11-16 17:25:57
           7 ORPHAN  2020-11-16 17:29:44
           8 ORPHAN  2020-11-16 17:34:25
           9 ORPHAN  2020-11-16 17:39:11
          10 ORPHAN  2020-11-16 17:51:56
          11 ORPHAN  2020-11-16 17:54:40

INCARNATION# STATUS  RESETLOGS_TIME
------------ ------- -------------------
          12 ORPHAN  2020-11-16 17:57:59

12 rows selected.
#另起会话窗口
su - oracle 
rman target/
#切换数据库原型至11(需在mount状态)
#强制关闭数据库
#SQL>shutdown abort;
#将数据库以mount方式启动
#SQL>startup mount; 

RMAN> reset database to incarnation 11;

using target database control file instead of recovery catalog
database reset to incarnation 11

#在rman查看当前的数据库原型(当前数据库原型为11)
#orcl为数据库名
RMAN>  list incarnation of  database "orcl";

List of Database Incarnations
DB Key  Inc Key DB Name  DB ID            STATUS  Reset SCN  Reset Time
------- ------- -------- ---------------- --- ---------- ----------
1       1       ORCL     1582243166       PARENT  1          15-AUG-09
2       2       ORCL     1582243166       PARENT  945184     22-OCT-20
3       3       ORCL     1582243166       PARENT  2659636    13-NOV-20
4       4       ORCL     1582243166       PARENT  2858078    16-NOV-20
5       5       ORCL     1582243166       ORPHAN  2858279    16-NOV-20
6       6       ORCL     1582243166       PARENT  2858279    16-NOV-20
7       7       ORCL     1582243166       ORPHAN  2858501    16-NOV-20
10      10      ORCL     1582243166       ORPHAN  2858501    16-NOV-20
8       8       ORCL     1582243166       ORPHAN  2858569    16-NOV-20
11      11      ORCL     1582243166       CURRENT 2858575    16-NOV-20
12      12      ORCL     1582243166       ORPHAN  2858575    16-NOV-20
9       9       ORCL     1582243166       ORPHAN  2878961    16-NOV-20

恢复数据库数据至指定时间

#恢复数据库数据至指定时间
#在第一步中查到数据库原型11的具体日期 11 ORPHAN  2020-11-16 17:54:40
#切换数据库原型至11后 11 CURRENT 2020-11-16 17:54:40;
#注意:下面命令中time='2020-11-16 17:55:00'只能在当前数据库原型时间之后。
#若在当前时间前,会报UNTIL TIME or RECOVERY WINDOW is before RESETLOGS time。
#举个栗子:time='2020-11-16 17:53:00'时间在11号原型时间2020-11-16 17:54:40之前,
#就会报UNTIL TIME or RECOVERY WINDOW is before RESETLOGS time

RMAN>run {
    
    
shutdown abort;
startup mount; 
allocate channel ch1 type disk;
allocate channel ch2 type disk;
sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"';
set until time='2020-11-16 17:55:00';
restore database;
recover database;
alter database open resetlogs;
}

rman不完全恢复shell脚本

#!/bin/bash
rmanbakDir=/u01/rmanbak
#日志输出路径
logDir=$rmanbakDir/log
recoverdata(){
    
    
        echo "恢复数据到日期:${1}"
        su - oracle -c "
        rman target / <<EOF
        run{
        shutdown abort;
        startup mount;
        allocate channel ch1 type disk;
        allocate channel ch2 type disk;
        sql 'alter session set nls_date_format=\"yyyy-mm-dd hh24:mi:ss\"';
        set until time='${1}';
        restore database;
        recover database;
        alter database open resetlogs;
        }
        exit;
        EOF"
        opendatabase
}

startRecover(){
    
    
        read -p  "请输入恢复到的日期(如2020-11-16 17:55:00):" inputdate
        if [ -z '$inputdate' ]
        then
                echo "输入日期为空,请重新输入!"
                startRecover
        else
                echo "输入参数为:$inputdate"
                date -d "$inputdate" +%Y-%m-%d\ %H:%M:%S
                if test $? -eq 0
                then
                        echo "输入的日期格式合法!"
                        recoverdata "${inputdate}" | log
                else
                        echo "输入的日期格式不合法,请重新输入!"
                        startRecover
                fi
        fi
}
opendatabase(){
    
    
        if test $? -ne 0
        then
                echo "rman恢复非正常运行,重新打开数据库!"
                su - oracle -c "
                sqlplus  / as sysdba <<EOF
                alter database open;
                exit;
                EOF"
        else 
                echo "rman恢复正常运行,数据库已启动!"
        fi;
}

log(){
    
    
        tee -a $logDir/rmanrecover_$backtime.log
}

isDirectory(){
    
    
        if test -d $1
        then
                echo "日志目录:$1目录已存在!"
                else
                        echo "日志目录:$1目录不存在!,创建目录!"
                        mkdir -p $1
        fi
}

isDirectory $logDir | log
startRecover  | log

猜你喜欢

转载自blog.csdn.net/weixin_43614067/article/details/109735499