您好,登錄后才能下訂單哦!
很多時候人為誤操作會對Oracle的數據造成影響,比如誤刪數據。當誤操作發生時,通常有四個解決方法:
1.閃回數據庫或閃回表
2.備份恢復
3.logminer挖掘redo
4.使用延時庫
前三種方法都不是很完美:
閃回數據庫影響較大,且事先需要開啟庫上的閃回。
閃回表或閃回版本查詢使用方便,但在繁忙的數據庫中很容易快照過舊。
備份恢復耗時長、影響大
logminer挖掘日志耗時耗力
此時如果事先有搭建延時備庫的話,則可以很容易的從備庫得到誤刪的數據。以下進行演示。
主庫:
SQL> show parameter instance_name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
instance_name string BDDEV2
備庫:
SQL> show parameter instance_name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
instance_name string BDTEST
現有表及數據如下:
SQL> select * from scott.tb_test;
ID AGE TM
---------- ---------- -----------------
1 2 20181203 14:22:14
2 3 20181203 14:22:14
3 4 20181203 14:22:14
4 5 20181203 14:22:14
5 6 20181203 14:22:14
6 7 20181203 14:22:14
7 8 20181203 14:22:14
8 9 20181203 14:22:14
9 10 20181203 14:22:14
10 11 20181203 14:22:14
10 rows selected.
此時備庫是實時應用的:
SQL> SELECT * FROM V$DATAGUARD_STATS;
NAME VALUE UNIT TIME_COMPUTED DATUM_TIME
------------------------------ ------------------------------ ---------------------------------------- ------------------------------ ------------------------------
transport lag +00 00:00:00 day(2) to second(0) interval 12/03/2018 14:23:33 12/03/2018 14:23:33
apply lag +00 00:00:00 day(2) to second(0) interval 12/03/2018 14:23:33 12/03/2018 14:23:33
apply finish time +00 00:00:00.000 day(2) to second(3) interval 12/03/2018 14:23:33
estimated startup time 7 second 12/03/2018 14:23:33
備庫轉為延時庫,延時60分鐘:
SQL> alter database recover managed standby database cancel;
Database altered.
SQL> alter database recover managed standby database disconnect from session delay 60;
Database altered.
主庫修改記錄:
SQL> update scott.tb_test set age = 100,tm = sysdate where id = 10;
1 row updated.
SQL> commit;
Commit complete.
SQL> select * from scott.tb_test;
ID AGE TM
---------- ---------- -----------------
1 2 20181203 14:22:14
2 3 20181203 14:22:14
3 4 20181203 14:22:14
4 5 20181203 14:22:14
5 6 20181203 14:22:14
6 7 20181203 14:22:14
7 8 20181203 14:22:14
8 9 20181203 14:22:14
9 10 20181203 14:22:14
10 100 20181203 14:26:50
10 rows selected.
可看到,修改數據的時間為' 20181203 14:26:50 '。
此時刪除表數據:
SQL> select sysdate from dual;
SYSDATE
-----------------
20181203 14:28:18
SQL> truncate table scott.tb_test;
Table truncated.
SQL> alter system switch logfile;
System altered.
此時主庫中數據已經刪除,備庫中數據還停留在主庫修改數據之前:
SQL> select * from scott.tb_test;
ID AGE TM
---------- ---------- -----------------
1 2 20181203 14:22:14
2 3 20181203 14:22:14
3 4 20181203 14:22:14
4 5 20181203 14:22:14
5 6 20181203 14:22:14
6 7 20181203 14:22:14
7 8 20181203 14:22:14
8 9 20181203 14:22:14
9 10 20181203 14:22:14
10 11 20181203 14:22:14
如果我們要恢復數據到update之后、truncate之前,則要手動前滾備庫到指定時間點:
--備庫操作
重啟備庫到mount:
SQL> startup force mount;
ORACLE instance started.
Total System Global Area 2.1379E+10 bytes
Fixed Size 2262656 bytes
Variable Size 2.0401E+10 bytes
Database Buffers 939524096 bytes
Redo Buffers 36073472 bytes
Database mounted.
手動前滾備庫到指定時間點:
SQL> recover standby database until time '20181203 14:27:00';
ORA-00279: change 108456269 generated at 12/03/2018 14:26:50 needed for thread 1
ORA-00289: suggestion : /opt/app/oracle/archivelog_bdtest/1_248_987097214.dbf
ORA-00280: change 108456269 for thread 1 is in sequence #248
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
Log applied.
Media recovery complete.
前滾完成,打開備庫:
SQL> alter database open;
Database altered.
SQL> alter session set nls_date_format='yyyymmdd hh34:mi:ss';
Session altered.
SQL> select * from scott.tb_test;
ID AGE TM
---------- ---------- -----------------
1 2 20181203 14:22:14
2 3 20181203 14:22:14
3 4 20181203 14:22:14
4 5 20181203 14:22:14
5 6 20181203 14:22:14
6 7 20181203 14:22:14
7 8 20181203 14:22:14
8 9 20181203 14:22:14
9 10 20181203 14:22:14
10 100 20181203 14:26:50
10 rows selected.
此時就得到了所需數據,只需把數據重新導入到主庫中即可。
待處理完成后,重新打開備庫的redo應用:
SQL> alter database recover managed standby database disconnect from session delay 60;
Database altered.
此時備庫又變為了正常的延時庫。
注意:
1.備庫DELAY時間指的是當主庫日志歸檔后,再延時DELAY指定的時間應用日志,比如:設置DELAY參數為20分鐘,10:00主庫日志歸檔,則10:20備庫才會應用此歸檔日志
2.也可以在LOG_ARCHIVE_DEST_2指定DELAY參數達到同樣的延時目的:
ALTER SYSTEM SET LOG_ARCHIVE_DEST_2='SERVICE=DEVSTBY LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=DEVSTBY DELAY=60'
3.備庫不能進行實時應用,因為實時應用會使DELAY參數失效,備庫變為ADG
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。