一、不完全恢复特性
1、不完全恢复
不完全恢复仅仅是将数据恢复到某一个特定的时间点或特定的SCN,而不是当前时间点。不完全恢复会影响整个数据库,需要在MOUNT状
态下进行。在不完全恢复成功之后,通常需要使用 resetlogs 选项来打开数据库。当使用resetlogs后,SCN 计数器不会被重置,原来的日
志序号 log sequence 会结束,从新开始新的日志序列号。在Oracle里称之为产生一个新的incarnation。同时Oracle还会重置联机重做日
志内容,因此resetlogs之后建议重新全备数据库。
2、不完全恢复的情形
介质故障(media failure)导致部分或全部联机重做日志(online redo log)损坏
用户操作失误(user error)导致数据丢失,例如,用户由于疏忽而移除了表,提交了无效的数据到表
由于归档重做日志(archived redo log)丢失而无法进行完全恢复(complete recovery)
当前控制文件(control file)丢失,必须使用备份的控制文件打开(open)数据库
3、不完全恢复的步骤
关闭数据库并备份数据库(以防止恢复失败)
启动数据库到mount 状态
还原所有数据文件,同时可以选择还原控制文件(注意需要还原所有数据文件,而不仅仅是受损文件)
将数据库恢复至某个时间点、序列、或系统改变号
使用RESETLOGS关键字打开数据库
4、注意
不完全恢复的前提条件是Oracl数据库够到mount状态,即参数文件,控制文件存在并且可用
在做不完全恢复前建议在恢复前后做一次备份,避免恢复失败导致不必要的损失
不完全恢复完成后,需要使用OPEN RESETLOGS 方式打开(open)数据库,并产生一个新的incarnation
检查数据库是否恢复到正确的时间点,也就是能否找到所需的数据,如果恢复的时间点有误,需要重置数据库incarnation进行再次恢复
5、不完全介质恢复的几种类型
基于时间的恢复(Time-based recovery) 将数据恢复到指定的时间点
用户控制的恢复(Cancel-based recovery) 当用户提交CANCEL后停止恢复(此选项在使用RMAN时无效)
基于SCN 的恢复(Change-based recovery) 将数据恢复到指定的SCN
按重做日志序号恢复(Log sequence recovery)将数据恢复到指定的重做日志序号(仅使用RMAN时有效)
不完全恢复的演示
A. until time 恢复(恢复到指定时间点)
--对数据库进行热备
SQL> conn /as sysdba
Connected.
SQL>
SQL> archive log list; --查看数据库是否开启归档
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u03/ARCHLOG/
Oldest online log sequence 1
Next log sequence to archive 1
Current log sequence 1
SQL> @/u03/backup/hotbak.sql --进行热备 alter tablespace BK2 begin backup ;
ho cp /u01/app/oracle/oradata/orcl/bk02.dbf /u03/backup/hotbak
alter tablespace BK2 end backup; alter tablespace SYSAUX begin backup ;
ho cp /u01/app/oracle/oradata/orcl/sysaux01.dbf /u03/backup/hotbak
alter tablespace SYSAUX end backup; alter tablespace SYSTEM begin backup ;
ho cp /u01/app/oracle/oradata/orcl/system01.dbf /u03/backup/hotbak
alter tablespace SYSTEM end backup; alter tablespace TEST begin backup ;
ho cp /u01/app/oracle/oradata/orcl/test.dbf /u03/backup/hotbak
alter tablespace TEST end backup; alter tablespace UNDOTBS1 begin backup ;
ho cp /u01/app/oracle/oradata/orcl/undotbs01.dbf /u03/backup/hotbak
alter tablespace UNDOTBS1 end backup; alter tablespace USERS begin backup ;
ho cp /u01/app/oracle/oradata/orcl/users01.dbf /u03/backup/hotbak
alter tablespace USERS end backup;
测试
SQL> set time on; --开启时间显示
22:50:52 SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; --查看当前时间 TO_CHAR(SYSDATE,'YY
-------------------
2016-12-20 22:51:01
22:54:12 SQL> create table dept as select * from scott.dept;
22:54:36 SQL> create table emp as select * from scott.emp;
22:54:44 SQL> truncate table emp;
22:54:56 SQL> insert into dept select 50,'DEV','SZ' from dual; --设置这一时刻为要恢复的时间点
22:55:03 SQL> insert into dept select 60,'HR','GZ' from dual;
22:55:11 SQL> commit;
22:55:19 SQL> alter system checkpoint;
22:55:29 SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
22:56:32 SQL> startup mount;
ORACLE instance started.
Total System Global Area 805875712 bytes
Fixed Size 2217672 bytes
Variable Size 595593528 bytes
Database Buffers 201326592 bytes
Redo Buffers 6737920 bytes
Database mounted.
22:57:30 SQL> host cp /u03/backup/hotbak/*dbf /u01/app/oracle/oradata/orcl/ 22:59:58 SQL> recover database until time '2016-12-20 22:54:56'; --恢复到指定的时间点
Media recovery complete.
23:01:47 SQL> alter database open resetlogs; --以restlogs的方式开启数据库
23:02:45 SQL> select count(*) from emp; --可以看出在指定时间点前的数据都被恢复了
0
23:02:53 SQL> select * from dept; --插入的数据丢失
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
unitl scn 恢复(基于系统改变号的恢复)
执行热备
SQL> @/u03/backup/hotbak.sql
测试
SQL> create table dept as select * from scott.dept;
SQL> select * from dept;
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> insert into dept select 50,'DEV','SZ' from dual;
SQL> commit;
SQL> select current_scn from v$database;
1079190
SQL> insert into dept select 60,'HR','GZ' from dual;
SQL> commit;
SQL> select current_scn from v$database;
1079464
SQL> alter system checkpoint;
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 805875712 bytes
Fixed Size 2217672 bytes
Variable Size 595593528 bytes
Database Buffers 201326592 bytes
Redo Buffers 6737920 bytes
Database mounted.
SQL> host cp /u03/backup/hotbak/*dbf /u01/app/oracle/oradata/orcl/ SQL> recover database until change 1079190 --恢复到指定的SCN
Media recovery complete.
SQL> alter database open resetlogs;
SQL> select * from dept;
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 DEV SZ
until cancel 恢复(基于放弃的恢复)
这个我没亲自做实验了,可以参考
Oracle在归档模式下恢复
--> Author: Robinson Cheng --> Blog: http://blog.csdn.net/robinson_0612
sys@SYBO2SZ> @db_hot_bak -->热备数据库
sys@SYBO2SZ> select * from dept; DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 DEV SZ sys@SYBO2SZ> ho ls -hltr /u02/database/SYBO2SZ/archive -->当前已经存在的归档日志
total 348K
-rw-r----- 1 oracle oinstall 340K 2012-08-22 17:01 arch_792003491_1_1.arc
-rw-r----- 1 oracle oinstall 2.0K 2012-08-22 17:01 arch_792003491_1_2.arc
-rw-r----- 1 oracle oinstall 1.0K 2012-08-22 17:02 arch_792003491_1_3.arc sys@SYBO2SZ> alter system switch logfile; -->切换日志 System altered. sys@SYBO2SZ> ho ls -hltr /u02/database/SYBO2SZ/archive -->可以看到新增了arch_792003491_1_4.arc
total 416K
-rw-r----- 1 oracle oinstall 340K 2012-08-22 17:01 arch_792003491_1_1.arc
-rw-r----- 1 oracle oinstall 2.0K 2012-08-22 17:01 arch_792003491_1_2.arc
-rw-r----- 1 oracle oinstall 1.0K 2012-08-22 17:02 arch_792003491_1_3.arc
-rw-r----- 1 oracle oinstall 66K 2012-08-22 17:04 arch_792003491_1_4.arc sys@SYBO2SZ> insert into dept select 60,'HR','SHANGHAI' from dual; sys@SYBO2SZ> insert into dept select 70,'INFRA','HONGKONG' from dual; sys@SYBO2SZ> commit; Commit complete. sys@SYBO2SZ> alter system checkpoint; -->切换日志 System altered. sys@SYBO2SZ> alter system archive log current; System altered. sys@SYBO2SZ> ho ls -hltr /u02/database/SYBO2SZ/archive
total 420K
-rw-r----- 1 oracle oinstall 340K 2012-08-22 17:01 arch_792003491_1_1.arc
-rw-r----- 1 oracle oinstall 2.0K 2012-08-22 17:01 arch_792003491_1_2.arc
-rw-r----- 1 oracle oinstall 1.0K 2012-08-22 17:02 arch_792003491_1_3.arc
-rw-r----- 1 oracle oinstall 66K 2012-08-22 17:04 arch_792003491_1_4.arc
-rw-r----- 1 oracle oinstall 2.5K 2012-08-22 17:07 arch_792003491_1_5.arc sys@SYBO2SZ> insert into dept select 80,'MARKET','BEIJING' from dual; sys@SYBO2SZ> commit; Commit complete. sys@SYBO2SZ> alter system archive log current; System altered. sys@SYBO2SZ> ho ls -hltr /u02/database/SYBO2SZ/archive -->系统又新增了几个归档日志
total 424K
-rw-r----- 1 oracle oinstall 340K 2012-08-22 17:01 arch_792003491_1_1.arc
-rw-r----- 1 oracle oinstall 2.0K 2012-08-22 17:01 arch_792003491_1_2.arc
-rw-r----- 1 oracle oinstall 1.0K 2012-08-22 17:02 arch_792003491_1_3.arc
-rw-r----- 1 oracle oinstall 66K 2012-08-22 17:04 arch_792003491_1_4.arc
-rw-r----- 1 oracle oinstall 2.5K 2012-08-22 17:07 arch_792003491_1_5.arc
-rw-r----- 1 oracle oinstall 2.0K 2012-08-22 17:08 arch_792003491_1_6.arc sys@SYBO2SZ> ho strings /u02/database/SYBO2SZ/archive/arch_792003491_1_5.arc | grep HONGKONG -->新记录已存在于归档日志
HONGKONG sys@SYBO2SZ> ho strings /u02/database/SYBO2SZ/archive/arch_792003491_1_6.arc | grep BEIJING
BEIJING sys@SYBO2SZ> ho rm /u02/database/SYBO2SZ/archive/arch_792003491_1_5.arc -->模拟部分归档日志丢失 sys@SYBO2SZ> ho ls -hltr /u02/database/SYBO2SZ/archive
total 420K
-rw-r----- 1 oracle oinstall 340K 2012-08-22 17:01 arch_792003491_1_1.arc
-rw-r----- 1 oracle oinstall 2.0K 2012-08-22 17:01 arch_792003491_1_2.arc
-rw-r----- 1 oracle oinstall 1.0K 2012-08-22 17:02 arch_792003491_1_3.arc
-rw-r----- 1 oracle oinstall 66K 2012-08-22 17:04 arch_792003491_1_4.arc
-rw-r----- 1 oracle oinstall 2.0K 2012-08-22 17:08 arch_792003491_1_6.arc sys@SYBO2SZ> shutdown immediate; sys@SYBO2SZ> startup mount;
ORACLE instance started. Total System Global Area 599785472 bytes
Fixed Size 2074568 bytes
Variable Size 243271736 bytes
Database Buffers 348127232 bytes
Redo Buffers 6311936 bytes
Database mounted.
sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/SYBO*.dbf /u02/database/SYBO2SZ/oradata/. -->还原数据库 sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/sys*.dbf /u02/database/SYBO2SZ/oradata/. sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/undotbs* /u02/database/SYBO2SZ/undo/. sys@SYBO2SZ> recover database until cancel; --> 基于 cancel 恢复数据库
ORA-00279: change 494124 generated at 08/22/2012 17:02:30 needed for thread 1
ORA-00289: suggestion : /u02/database/SYBO2SZ/archive/arch_792003491_1_4.arc
ORA-00280: change 494124 for thread 1 is in sequence #4 Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
/u02/database/SYBO2SZ/archive/arch_792003491_1_4.arc -->恢复到尾数为4的归档日志
ORA-00279: change 494189 generated at 08/22/2012 17:04:46 needed for thread 1
ORA-00289: suggestion : /u02/database/SYBO2SZ/archive/arch_792003491_1_5.arc
ORA-00280: change 494189 for thread 1 is in sequence #5
ORA-00278: log file '/u02/database/SYBO2SZ/archive/arch_792003491_1_4.arc' no longer needed for this recovery Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
cancel -->第5个日志文件丢失,输入cancel
Media recovery cancelled.
sys@SYBO2SZ> alter database open resetlogs; --> resetlogs 方式打开数据库 Database altered. sys@SYBO2SZ> select * from dept; -->由于归档日志5丢失,因此后续所有操作的数据丢失 DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 DEV SZ 5 rows selected.