第六章: 不完全恢复--FlashBack
flashback database:利用flashback log 对整个database 做回退到过去的某个时间点(用于DDL 的误操作如drop 和 truncate)
1)查看flashback database
07:21:27 SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
NO
07:21:33 SQL> show parameter recover
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /u01/app/oracle/flash_recovery
_area
db_recovery_file_dest_size big integer 2G
recovery_parallelism integer 2
07:22:08 SQL> !
[oracle@work ~]$ mkdir -p /disk1/recovery/prod
[oracle@work ~]$ !sql
sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Wed Aug 17 07:22:49 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
07:22:49 SQL>
07:22:49 SQL> alter system set db_recovery_file_dest='/disk1/recovery/prod' scope=spfile;
System altered.
-------存放flashback log(闪回日志)
--------启用flashback database 功能(database 必须是归档模式)
07:24:22 SQL> startup mount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 71304592 bytes
Database Buffers 239075328 bytes
Redo Buffers 2973696 bytes
Database mounted.
07:24:35 SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /disk1/arch/prod
Oldest online log sequence 0
Next log sequence to archive 1
Current log sequence 1
07:25:06 SQL> alter database flashback on;
Database altered.
07:25:29 SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
YES
07:25:38 SQL> alter database open;
Database altered.
07:25:49 SQL>
------------flashback database 恢复DDL 误操作
1)模拟环境
07:26:30 SQL> select * from test;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
16 rows selected.
07:26:36 SQL> drop table test purge;
Table dropped.
07:27:20 SQL> create table test as select * from emp where rownum=1;
Table created.
07:27:25 SQL> select * from test;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
07:27:29 SQL>
--------flashback 日志
[oracle@work ~]$ ls /disk1/recovery/prod/PROD/flashback/
o1_mf_74q999lb_.flb
[oracle@work ~]$
--------在mount 下闪回
07:29:22 SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
07:29:53 SQL> startup mount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 71304592 bytes
Database Buffers 239075328 bytes
Redo Buffers 2973696 bytes
Database mounted.
07:30:02 SQL> flashback database to scn 1264788;
Flashback complete.
---------把database 以read only 方式打开,先验证下恢复是否成功,如果不成功,再从新进入mount ,恢复
07:31:29 SQL> alter database open read only;
Database altered.
07:31:34 SQL> select * from scott.test;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
16 rows selected.
---------恢复成功,重新以resetlogs的方式open database
07:31:40 SQL> shutdown immedaite
SP2-0717: illegal SHUTDOWN option
07:31:45 SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
07:32:02 SQL> startup mount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 71304592 bytes
Database Buffers 239075328 bytes
Redo Buffers 2973696 bytes
Database mounted.
07:32:10 SQL> alter database open resetlogs;
Database altered.
--------验证:
07:32:25 SQL> select * from scott.test;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
16 rows selected.
--------------基于timestamp 的flashback database
1)查看flashback 参数
03:43:58 SQL> show parameter db_recovery
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string
db_recovery_file_dest_size big integer 0
2)设置flashback 参数
03:44:09 SQL> alter system set db_recovery_file_dest='/disk1/flash_area' scope=spfile;
System altered.
03:45:00 SQL> alter system set db_recovery_file_dest_size=1G scope=spfile;
System altered.
3)激活fashback (必须要database 干净的关闭才行)
03:45:20 SQL> startup force
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 180356496 bytes
Database Buffers 130023424 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
03:45:36 SQL> show parameter db_recovery
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /disk1/flash_area
db_recovery_file_dest_size big integer 1G
03:46:06 SQL> startup force mount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 180356496 bytes
Database Buffers 130023424 bytes
Redo Buffers 2973696 bytes
Database mounted.
03:46:21 SQL> alter database flashback on; ----非正常关库,无法打开flashback
alter database flashback on
*
ERROR at line 1:
ORA-38706: Cannot turn on FLASHBACK DATABASE logging.
ORA-38714: Instance recovery required.
03:47:03 SQL> show parameter db_recovery
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /disk1/flash_area
db_recovery_file_dest_size big integer 1G
03:47:39 SQL> shutdown immediate
ORA-01109: database not open
Database dismounted.
ORACLE instance shut down.
03:49:33 SQL> startup
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 180356496 bytes
Database Buffers 130023424 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
03:49:43 SQL> show parameter db_recovery
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /disk1/flash_area
db_recovery_file_dest_size big integer 1G
03:49:52 SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
03:50:37 SQL> startup mount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 180356496 bytes
Database Buffers 130023424 bytes
Redo Buffers 2973696 bytes
Database mounted.
03:51:20 SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /disk1/arch
Oldest online log sequence 46
Next log sequence to archive 51
Current log sequence 51
03:51:25 SQL> alter database flashback on; ----干净关闭下,开启flashback。
Database altered.
03:51:43 SQL> select name,current_scn ,flashback_on from v$database;
NAME CURRENT_SCN FLASHBACK_ON
--------- ----------- ------------------
ORCL 0 YES
03:52:46 SQL> show parameter flashback
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_flashback_retention_target integer 1440
03:53:47 SQL> alter database open;
Database altered.
4) flashback 验证
04:04:28 SQL> show user;
USER is "SYS"
04:08:04 SQL> conn scott/tiger
Connected.
04:08:13 SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'YY
-------------------
2011-03-18 04:08:42
04:09:02 SQL> conn /as sysdba
Connected.
04:09:07 SQL> select current_scn from v$database;
CURRENT_SCN
-----------
1437597
04:09:10 SQL> conn scott/tiger
Connected.
04:09:29 SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
EMP TABLE
DEPT TABLE
BONUS TABLE
SALGRADE TABLE
QUEST_SL_TEMP_EXPLAIN1 TABLE
EMP1 TABLE
ERRLOG TABLE
PART_SALES TABLE
T01 TABLE
DEPT1 TABLE
10 rows selected.
select * from t01;
ID NA
---------- --
1 TM
04:10:07 SQL> insert into t01 values(2,'aa');
1 row created.
04:10:17 SQL> insert into t01 values(3,'bb');
1 row created.
04:10:26 SQL> commit;
Commit complete.
04:10:30 SQL> drop table t01;
Table dropped.
04:10:41 SQL> shutdown immediate
ORA-01031: insufficient privileges
04:10:59 SQL> conn /as sysdba
Connected.
04:11:03 SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
04:11:28 SQL> startup mount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 184550800 bytes
Database Buffers 125829120 bytes
Redo Buffers 2973696 bytes
Database mounted.
04:12:07 SQL> flashback database to timestamp to_timestamp('2011-03-18 04:10:26','yyyy-mm-dd hh24:mi:ss');
Flashback complete.
04:13:34 SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
04:14:10 SQL> alter database open noresetlogs;
alter database open noresetlogs
*
ERROR at line 1:
ORA-01610: recovery using the BACKUP CONTROLFILE option must be done
04:14:18 SQL> alter database open resetlogs;
Database altered.
04:15:35 SQL> select * from v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- -------------------
1 1 0 52428800 2 YES UNUSED 0
2 1 0 52428800 2 YES UNUSED 0
3 1 0 52428800 2 YES UNUSED 0
4 1 0 52428800 2 YES UNUSED 0
5 1 0 52428800 2 YES UNUSED 0
6 1 1 52428800 2 NO CURRENT 1437637 2011-03-18 04:14:27
6 rows selected.
04:15:59 SQL> conn scott/tiger
Connected.
04:16:18 SQL> select * from t01;
ID NA
---------- --
1 TM
04:34:35 SQL> desc v$flashback_database_log;
Name Null? Type
----------------------------------------------------------------------------------- -------- - OLDEST_FLASHBACK_SCN NUMBER
OLDEST_FLASHBACK_TIME DATE
RETENTION_TARGET NUMBER
FLASHBACK_SIZE NUMBER
ESTIMATED_FLASHBACK_SIZE NUMBER
04:34:37 SQL> select * from v$flashback_database_log;
OLDEST_FLASHBACK_SCN OLDEST_FLASHBACK_TI RETENTION_TARGET FLASHBACK_SIZE TIMATED_FLASHBACK_SIZE
-------------------- ------------------- ---------------- -------------- ----------------------
1436931 2011-03-18 03:51:43 1440 8192000 115703808