分类: Oracle
2012-01-04 20:26:04
只存在RMAN备份片的数据库恢复过程。
一.之前的数据库状态。
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 1
Next log sequence to archive 3
Current log sequence 3
SQL> select file_name from dba_data_files;
FILE_NAME
--------------------------------------------------------------------------------
/dev/raw/raw10
/dev/raw/raw9
/dev/raw/raw8
/dev/raw/raw7
SQL> select name from v$controlfile;
NAME
--------------------------------------------------------------------------------
/dev/raw/raw1
/dev/raw/raw2
/dev/raw/raw3
SQL> select member from v$logfile;
MEMBER
--------------------------------------------------------------------------------
/dev/raw/raw6
/dev/raw/raw5
/dev/raw/raw4
数据文件、控制文件和Redo日志文件都存放在裸设备上。
二.手动创建测试数据。
SQL> create user xiaoyang identified by xiaoyang;
User created.
SQL> grant connect,resource to xiaoyang;
Grant succeeded.
SQL> connect xiaoyang/xiaoyang
Connected.
SQL> create table xiaoyang(id number);
Table created.
SQL> insert into xiaoyang values(1);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from xiaoyang.xiaoyang;
ID
----------
1
三.手动执行在线RMAN备份:
connected to target database: ORCL (DBID=1295557632)
RMAN> backup database;
Starting backup at 2011-11-21 07:34:07
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/dev/raw/raw7
input datafile fno=00003 name=/dev/raw/raw9
input datafile fno=00002 name=/dev/raw/raw8
input datafile fno=00004 name=/dev/raw/raw10
channel ORA_DISK_1: starting piece 1 at 2011-11-21 07:34:07
channel ORA_DISK_1: finished piece 1 at 2011-11-21 07:34:32
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2011_11_21/o1_mf_nnndf_TAG20111121T073407_7dm3mhy3_.bkp tag=TAG20111121T073407 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:25
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
including current SPFILE in backupset
channel ORA_DISK_1: starting piece 1 at 2011-11-21 07:34:33
channel ORA_DISK_1: finished piece 1 at 2011-11-21 07:34:34
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2011_11_21/o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp tag=TAG20111121T073407 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2011-11-21 07:34:34
四.模拟损坏情况
删除整个数据库,模拟数据库被破坏,只将之前的RMAN备份集拷贝到/u02/目录下,用于恢复目的。
[oracle@rhel2 ~]$ cp /u01/app/oracle/flash_recovery_area/ORCL/backupset/2011_11_21/o1_mf_nnndf_TAG20111121T073407_7dm3mhy3_.bkp /u02/
[oracle@rhel2 ~]$ cp /u01/app/oracle/flash_recovery_area/ORCL/backupset/2011_11_21/o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp /u02/
使用dbca删除orcl数据库,模拟数据库丢失。
五.创建一个临时数据库环境
由于RMAN必须工作在MOUNT模式,所有的数据文件都丢失,无法通过只重建控制文件将其启动到MOUNT模式,所以这里利用dbca创建一个临时数据库环境,数据库的名称与原有名称保持不变,文件存放到默认位置即可。
六.将备份片的元数据加入到控制文件中
使用RMAN的restore命令,RMAN会自己从控制文件中找数据文件的元数据信息,为了能够正常的恢复数据文件,需要提前将备份片元数据信息记录到控制文件中,这样RMAN能够顺利的找到备份文件,备份文件还保留了数据库的元数据信息,使得之后通过list命令就可以查看到原有数据库的元数据信息,从而能够很容易的编写恢复脚本。
1.执行以下命令将之前数据库的备份片加入到临时数据库的控制文件中。
RMAN> catalog backuppiece '/u02/o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp';
ORA-19870: error reading backup piece /u02/o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp
ORA-19691: /u02/o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp is from different database: id=1295557632, name=ORCL
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of catalog command at 11/21/2011 07:51:16
RMAN-06209: List of failed objects
RMAN-06211: ==========================
RMAN-06212: Object Type Filename/Handle
RMAN-06213: --------------- ---------------------------------------------------
RMAN-06214: Backup Piece /u02/o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp
将备份片的信息加入到控制文件的时候报错,原因在于原有数据库和临时数据库的DBID不同,下面首先修改临时数据库的DBID,使它与原有数据库DBID保持一致。
2.由于临时数据库的DBID与原有数据库的DBID不同,导致RMAN无法执行RESTORE操作,所以需要修改临时数据库的DBID为原有数据库的DBID。
当前数据库DBID:
SQL> select dbid from v$database;
DBID
----------
1295559190
将1295559190修改为1295557632,执行下面的修改DBID步骤:
SQL> @?/rdbms/admin/dbmsbkrs.sql
Package created.
SQL> select dbid from v$database;
DBID
----------
1295560714
SQL> exec dbms_backup_restore.nidbegin('orcl','orcl','1295557632','1295560714',0,0,10);
PL/SQL procedure successfully completed.
下面是dbms_backup_restore.nidbegin存储过程参数的说明:
SQL> desc dbms_backup_restore.nidbegin
Parameter Type Mode Default?
--------- -------------- ---- --------
NEWDBNAME VARCHAR2 IN
OLDDBNAME VARCHAR2 IN
NEWDBID NUMBER IN
OLDDBID NUMBER IN
DOREVERT BINARY_INTEGER IN
DORESTART BINARY_INTEGER IN
EVENTS NUMBER IN
SQL> variable a number;
SQL> variable b number;
SQL> variable c number;
SQL> exec dbms_backup_restore.nidprocessdf(0,0,:a,:b,:c);
BEGIN dbms_backup_restore.nidprocessdf(0,0,:a,:b,:c); END;
*
ERROR at line 1:
ORA-00600: internal error code, arguments: [krbnpdf_wrngname_3],
[/u01/app/oracle/oradata/orcl/system01.dbf], [ORCL], [orcl], [], [], [], []
ORA-06512: at "SYS.DBMS_BACKUP_RESTORE", line 6161
ORA-06512: at line 1
SQL> exec dbms_backup_restore.nidprocesscf(:a,:b);
PL/SQL procedure successfully completed.
SQL> exec dbms_backup_restore.nidend;
PL/SQL procedure successfully completed.
SQL> select dbid from v$database;
DBID
----------
1295557632
这里是使用DBMS_BACKUP_RESTORE包来修改指定的DBID,这是一个恢复中常用的包,由dbmsbkrs.sql文件创建。这里虽然有报错,但DBID还是修改成功了。
对于DBID和DBNAME的修改还可以使用nid工具,这个工具除了可以修改DBID和DBNAME外,还可以对修改错误的情况进行恢复。下面是nid的帮助信息:
[oracle@rhel2 u02]$ nid
DBNEWID: Release 10.2.0.1.0 - Production on Mon Nov 21 09:07:38 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Keyword Description (Default)
----------------------------------------------------
TARGET Username/Password (NONE)
DBNAME New database name (NONE)
LOGFILE Output Log (NONE)
REVERT Revert failed change NO
SETNAME Set a new database name only NO
APPEND Append to output log NO
HELP Displays these messages NO
例如,执行以下的命令用来恢复错误的DBID或DBNAME的修改,nid target=sys/oracle revert=yes
nid工具执行的命令实际也是调用DBMS_BACKUP_RESTORE包相应的存储过程实现的。
七.再次将备份片的元数据加入到控制文件中
RMAN> catalog backuppiece '/u02/o1_mf_nnndf_TAG20111121T073407_7dm3mhy3_.bkp';
cataloged backuppiece
backup piece handle=/u02/o1_mf_nnndf_TAG20111121T073407_7dm3mhy3_.bkp recid=3 stamp=767780955
RMAN-08132: WARNING: cannot update recovery area reclaimable file list
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03009: failure of REFAF command on default channel at 11/21/2011 08:29:15
ORA-00237: snapshot operation disallowed: control file newly created
RMAN> exit
[oracle@rhel2 ~]$ sqlplus / as sysdba
SQL> shutdown abort
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 369098752 bytes
Fixed Size 2020864 bytes
Variable Size 117443072 bytes
Database Buffers 247463936 bytes
Redo Buffers 2170880 bytes
Database mounted.
SQL> exit
[oracle@rhel2 ~]$ rman target /
connected to target database: ORCL (DBID=1295557632, not open)
RMAN> catalog backuppiece '/u02/o1_mf_nnndf_TAG20111121T073407_7dm3mhy3_.bkp';
using target database control file instead of recovery catalog
cataloged backuppiece
backup piece handle=/u02/o1_mf_nnndf_TAG20111121T073407_7dm3mhy3_.bkp recid=4 stamp=767781072
RMAN> catalog backuppiece '/u02/o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp';
cataloged backuppiece
backup piece handle=/u02/o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp recid=5 stamp=767781095
RMAN> list backup;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ -------------------
1 Full 6.80M DISK 00:00:00 2011-11-21 07:34:33
BP Key: 5 Status: AVAILABLE Compressed: NO Tag: TAG20111121T073407
Piece Name: /u02/o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp
Control File Included: Ckp SCN: 535269 Ckp time: 2011-11-21 07:34:33
SPFILE Included: Modification time: 2011-11-21 07:32:31
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ -------------------
2 Full 513.27M DISK 00:00:00 2011-11-21 07:34:07
BP Key: 4 Status: AVAILABLE Compressed: NO Tag: TAG20111121T073407
Piece Name: /u02/o1_mf_nnndf_TAG20111121T073407_7dm3mhy3_.bkp
List of Datafiles in backup set 2
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- ------------------- ----
1 Full 535260 2011-11-21 07:34:07 /u01/app/oracle/oradata/orcl/system01.dbf
2 Full 535260 2011-11-21 07:34:07 /u01/app/oracle/oradata/orcl/undotbs01.dbf
3 Full 535260 2011-11-21 07:34:07 /u01/app/oracle/oradata/orcl/sysaux01.dbf
4 Full 535260 2011-11-21 07:34:07 /u01/app/oracle/oradata/orcl/users01.dbf
八.执行恢复操作:
RMAN> restore database ;
Starting restore at 2011-11-21 08:32:45
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 11/21/2011 08:32:46
RMAN-06026: some targets not found - aborting restore
RMAN-06023: no backup or copy of datafile 4 found to restore
RMAN-06023: no backup or copy of datafile 3 found to restore
RMAN-06023: no backup or copy of datafile 2 found to restore
RMAN-06023: no backup or copy of datafile 1 found to restore
虽然能用list backup命令看到相关数据文件的元数据信息,但是执行restore database却无法找到相应数据文件的备份,这时可以用到强大的DBMS_BACKUP_RESTORE包执行恢复操作,执行如下PL/SQL命令:
SQL> DECLARE
devtype varchar2(256);
done boolean;
BEGIN
devtype:=sys.dbms_backup_restore.deviceAllocate (type=>'',ident=>'t1');
sys.dbms_backup_restore.restoreSetDatafile;
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>01,toname=>'/u02/system01.dbf');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>02,toname=>'/u02/sysaux01.dbf');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>03,toname=>'/u02/users01.dbf');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>04,toname=>'/u02/undotbs01.dbf');
sys.dbms_backup_restore.restoreBackupPiece(done=>done,handle=>'/u02/o1_mf_nnndf_TAG20111121T073407_7dm3mhy3_.bkp', params=>null);
sys.dbms_backup_restore.deviceDeallocate;
END;
/
PL/SQL procedure successfully completed.
相应的数据文件已经在/u02目录下restore成功:
[oracle@rhel2 ~]$ cd /u02
[oracle@rhel2 u02]$ ll
total 1301912
-rw-r----- 1 oracle oinstall 7143424 Nov 21 07:36 o1_mf_ncsnf_TAG20111121T073407_7dm3n9lv_.bkp
-rw-r----- 1 oracle oinstall 538214400 Nov 21 07:36 o1_mf_nnndf_TAG20111121T073407_7dm3mhy3_.bkp
-rw-r----- 1 oracle oinstall 26222592 Nov 21 08:35 sysaux01.dbf
-rw-r----- 1 oracle oinstall 503324672 Nov 21 08:36 system01.dbf
-rw-r----- 1 oracle oinstall 5251072 Nov 21 08:35 undotbs01.dbf
-rw-r----- 1 oracle oinstall 251666432 Nov 21 08:36 users01.dbf
九.重建控制文件:
SQL> alter database backup controlfile to trace;
Database altered.
SQL> !
[oracle@rhel2 u02]$ cd $ORACLE_BASE/admin/orcl/udump
[oracle@rhel2 udump]$ ls -altr *| tail -1
-rw-r----- 1 oracle oinstall 6707 Nov 21 08:37 orcl_ora_9711.trc
十.重启数据库到NOMOUNT状态,执行以下控制文件创建命令:
SQL> shutdown abort
ORACLE instance shut down.
SQL> startup nomount
ORACLE instance started.
Total System Global Area 369098752 bytes
Fixed Size 2020864 bytes
Variable Size 117443072 bytes
Database Buffers 247463936 bytes
Redo Buffers 2170880 bytes
从orcl_ora_9711.trc文件中获取控制文件的创建命令:
SQL> CREATE CONTROLFILE REUSE DATABASE "ORCL" RESETLOGS NOARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 '/u02/redo01.log' SIZE 50M,
GROUP 2 '/u02/redo02.log' SIZE 50M,
GROUP 3 '/u02/redo03.log' SIZE 50M
-- STANDBY LOGFILE
DATAFILE
'/u02/system01.dbf',
'/u02/undotbs01.dbf',
'/u02/sysaux01.dbf',
'/u02/users01.dbf'
CHARACTER SET ZHS16GBK
;
Control file created.
十一.打开数据库,测试恢复的数据库:
SQL> alter database open resetlogs;
Database altered.
SQL> select * from xiaoyang.xiaoyang;
ID
----------
1
十二.指定临时表空间
由于RMAN恢复是不会恢复临时表空间(查询dba_temp_files为空),所以需要手动重新创建临时表空间,并制定为默认的临时表空间。执行以下的命令创建临时表空间和制定为数据库默认的临时表空间:
SQL> create temporary tablespace TEMP01 tempfile '/u02/temp01.dbf' size 100m autoextend on next 10m;
Tablespace created.
SQL> alter database default temporary tablespace temp01;
Database altered.
至此利用仅存的备份集恢复了数据库。如果数据库备份的时候不一致,那么可能不能直接打开,可以考虑强制打开,然后将数据导出的办法来恢复数据。
这里主要讨论的是一种思路,具体命令执行返回的结果会因不同的环境、不同的情况会有所差别,实践中可能会遇到更多的问题,需要根据具体的环境和情况来试验。
参考文章: