1. 程式人生 > >oracle備份與恢復--閃回技術

oracle備份與恢復--閃回技術

一•啟用閃回資料庫
1.啟用歸檔模式
sql>shutdown immediate;
sql>startup mount;
sql>alter database archivelog;
2.建立閃回區
sql>alter system set db_recovey_file_dest='/opt/oracle/flash_recovery_area' scope=both;
sql>alter system set db_recovery_file_dest_size=3g scope=both;
3.設定閃回資料庫的資料保留週期為一天,以min為單位
sql>alter system set db_flashback_retention_target=1440;
4.啟用閃回日誌
sql>alter database flashback on;
sql>alter database open;

二•使用scn閃回資料庫
1.查詢資料庫系統當前的scn(記下此scn)
sql>select current_scn from v$database;
2.改變資料庫的當前狀態,模擬建立表test1,並插入1條資料
create table test1(id number,name char(20));
insert into test1 values(1,'data');
commit;
3.進行閃回資料庫恢復,將資料庫恢復到建立表之前的狀態
SQL> select OLDEST_FLASHBACK_SCN from v$FLASHBACK_DATABASE_LOG;(檢視scn)
shutdown immediate
startup mount;
flashback database to scn 之前查到的scn
4.最後使用resetlogs選項開啟資料庫
alter database open resetlogs;
5.驗證資料庫的狀態(test1表應該不存在)
SQL> select from test1;
select

from test1
*
ERROR at line 1:
ORA-00942: table or view does not exist

6.按照指定時間閃回資料庫
1)設定顯示日期格式
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
select sysdate from dual;(檢視系統時間)
2)改變資料庫的當前狀態,模擬建立表test2,並插入1條記錄
sql>set time on;
sql>create table test2(id number,name char(20));
sql>insert into test2 values(1,'data');
sql>commit;
sql>shutdown immediate;
sql>startup mount;
sql>flashback database to timestamp(to_timestamp('......'));(....即為前面檢視到系統時間)
sal>alter database open resetlogs;
sql>select * from test2;
ERROR at line 1:
ORA-00942: table or view does not exist(顯示test2表不存在)
說明:閃回資料庫操作的限制
1)資料檔案損壞或丟失等介質故障不能使用閃回資料庫進行恢復。閃回資料庫只能基於當前正常執行的資料檔案。
2)閃回資料庫功能啟動後,如果發生資料庫控制檔案重建或利用備份恢復控制檔案,則不能使用閃回資料庫
3)不能使用閃回資料庫進行資料檔案收縮操作
4)不能使用閃回資料庫將資料庫恢復到閃回日誌中可獲得最早的SCN之前的SCNM,因為閃回日誌檔案在一定條件下被刪除,而不是始終儲存在閃回恢復區中

三、閃回表
閃回表可將表恢復到特定的時間點或者指定的SCN
閃回表實際上是對錶進行DML操作的過程
資料庫保持聯機
為了使用資料庫閃回表功能,必須滿足下列條件:
使用者具有FLASHBACK ANY TABLE系統許可權,或者具有所操作表的FLASHBACK物件許可權
使用者具有所操作表的SELECT,INSERT,DELETE,ALTER物件許可權
啟動被操作表的ROW MOVEMENT特性,可以採用下列方式進行
ALTER TABLE table ENABLE ROW MOVEMENT;
注意
SYS使用者或以AS SYSDBA身份登入的使用者不能執行閃回表操作

1.使用scott使用者登入
sql>alter user scott account unlock;
sql>alter user scott identified by 123456;
sql>grant connect,resource to scott;
sql>conn scott/123456;
set time on;
sql>create table test3(id number,name char(20));
sql>insert into test3 values(1,'zhang');
commit;
sql>insert into test3 values(2,'zhao');
commit;
sql>insert into test3 values(3,'wang');
commit;
以sys身份連線資料庫,並授予scott使用者select any dictionary的許可權,並最後以scott身份檢視當前的scn
conn / as sysdba
grant select any dictionary to scott;
SQL>set time on
sql>conn scott/123456;
sql>select current_scn from v$database;記下此scn
sql>update test3 set name='liu' where id=1;
sql>commit;
sql>select from test3;
sql>delete from test3 where id=3;
sql>commit;
sql>select
from test3
2.啟動test3表的row movement特性
sql>alter table test3 enable row movement;
sql>flashback table test3 to timestamp to_timestamp(。。。。);

四、閃回刪除
閃回刪除可恢復使用DROP TABLE語句刪除的表,是一種對意外刪除的表的恢復機制
閃回刪除功能的實現主要是通過資料庫中的“回收站”(Recycle Bin)技術實現的
為了使用閃回刪除技術,必須開啟資料庫的“回收站”
啟動“回收站”
檢視“回收站”
閃回刪除的基本語法為
FLASHBACK TABLE [schema.]table TO BEFORE DROP [RENAME TO table]
注意
不支援SYS使用者,SYSTEM表空間下的物件也不能從回收站裡拿到。故使用SYS或者SYSTEM使用者登入時,查詢為空

1.啟動回收站,將引數recyclebin設定為on,在預設情況下回收站已經啟動
SQL> conn / as sysdba;
sql>show parameter recyclebin;
sql>alter system set recyclebin=on deferred;
2.查看回收站,不支援從sys使用者,system表空間的物件也不能從回收站裡拿到,所以要切換到scott使用者測試
sql>conn scott/123456;
create table test4(id number,name char(20));
sql>insert into test4 values(3,'wang')
sql>commit;
sql>drop table test4;
sql>select object_name,original_name,type from user_recyclebin;
閃回表並重命名為new_test4
sql>flashback table test4 to before drop rename to new_test4;

五•閃回查詢
sql>conn scott/123456;
sql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
sql>set time on;
sql>select empno,sal from scott.emp where empno=7844;
sql>update scott.emp set sal=2000 where empno=7844;
sql>commit;
sql>update scott.emp set sal=2500 where empno=7844;
sql>update scott.emp set sal=3000 where empno=7844;
sql>commit;
查詢7844員工前一個小時的工資值
sql>select empno,sal from scott.emp as of timestamp sysdate-1/24 where empno=7844