[20170627]使用TSPITR恢復表空間.txt

来源:http://www.cnblogs.com/lfree/archive/2017/06/27/7083501.html
-Advertisement-
Play Games

[20170627]使用TSPITR恢復表空間.txt--//RMAN提供了一種實現所謂TSPITR(Tablespace Point-In-Time Recovery)的技術,通過簡單的一個語句,就可以在主庫不停庫(很吸引人)--//的情況下,利用備份集和連續的歸檔日誌,實現表空間級別的定點恢復。 ...


[20170627]使用TSPITR恢復表空間.txt

--//RMAN提供了一種實現所謂TSPITR(Tablespace Point-In-Time Recovery)的技術,通過簡單的一個語句,就可以在主庫不停庫(很吸引人)
--//的情況下,利用備份集和連續的歸檔日誌,實現表空間級別的定點恢復。

--//實際上rman就是把人工一步一步執行的命令打包,執行恢復工作,減輕dba負擔.我一直認為這個不是很實用,前幾天做了利用傳輸表空
--//間的測試,感覺那個更實用一些.
--//blog.itpub.net/267265/viewspace-2141166/=>[20170623]利用傳輸表空間恢復部分數據.txt

--//因為當誤操作發生時,許多事務還是作用在這個表空間,為了取消這個誤操作,要恢復到表空間特定的時間點.而丟失一些事務,
--//總之dba要選擇這樣的操作時要權衡利弊.

--//實際上,以前也做過類似的測試,只不過沒有寫出來,今天完善這方面的內容,做一個測試:

1.環境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SCOTT@book> create table emp2 tablespace tea as select * from emp ;
Table created.

SCOTT@book> create table dept2 tablespace tea as select * from dept ;
Table created.

SCOTT@book> create table t tablespace tea as select * from all_objects ;
Table created.

SCOTT@book> alter system archive log current ;
System altered.

2.做一次rman全備份:
backup database format '/home/oracle/backup/full_20170627_%U.bak';
backup archivelog all  format '/home/oracle/backup/archive_20170627_%U';

3.開始模擬誤操作.

SCOTT@book> set numw 12
SCOTT@book> select current_scn,sysdate from v$database;
 CURRENT_SCN SYSDATE
------------ -------------------
 13276934358 2017-06-27 09:05:36


SCOTT@book> truncate table emp2;
Table truncated.

SCOTT@book> drop table  dept2 purge ;
Table dropped.

SCOTT@book> select count(*) from t ;
    COUNT(*)
------------
       84762

SCOTT@book> delete from t where owner='SYS';
37354 rows deleted.

SCOTT@book> commit;
Commit complete.

SCOTT@book> select count(*) from t ;
    COUNT(*)
------------
       47408

4.開始恢復看看:
$ mkdir /home/oracle/aux/

--//執行命令: recover tablespace tea until scn 13276934358 auxiliary destination '/home/oracle/aux';

RMAN> recover tablespace tea until scn 13276934358 auxiliary destination '/home/oracle/aux';

Starting recover at 2017-06-27 09:08:39
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=14 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=26 device type=DISK
allocated channel: ORA_DISK_3
channel ORA_DISK_3: SID=37 device type=DISK
RMAN-05026: WARNING: presuming following set of tablespaces applies to specified point-in-time

List of tablespaces expected to have UNDO segments
Tablespace SYSTEM
Tablespace UNDOTBS1

Creating automatic instance, with SID='Bndc'

initialization parameters used for automatic instance:
db_name=BOOK
db_unique_name=Bndc_tspitr_BOOK
compatible=11.2.0.4.0
db_block_size=8192
db_files=200
sga_target=1G
processes=80
db_create_file_dest=/home/oracle/aux
log_archive_dest_1='location=/home/oracle/aux'
#No auxiliary parameter file used


starting up automatic instance BOOK

Oracle instance started

Total System Global Area    1068937216 bytes

Fixed Size                     2260088 bytes
Variable Size                285213576 bytes
Database Buffers             771751936 bytes
Redo Buffers                   9711616 bytes
Automatic instance created
Running TRANSPORT_SET_CHECK on recovery set tablespaces
TRANSPORT_SET_CHECK completed successfully

contents of Memory Script:
{
# set requested point in time
set until  scn 13276934358;
# restore the controlfile
restore clone controlfile;
# mount the controlfile
sql clone 'alter database mount clone database';
# archive current online log
sql 'alter system archive log current';
# avoid unnecessary autobackups for structural changes during TSPITR
sql 'begin dbms_backup_restore.AutoBackupFlag(FALSE); end;';
}
executing Memory Script

executing command: SET until clause

Starting restore at 2017-06-27 09:08:47
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=127 device type=DISK
allocated channel: ORA_AUX_DISK_2
channel ORA_AUX_DISK_2: SID=133 device type=DISK
allocated channel: ORA_AUX_DISK_3
channel ORA_AUX_DISK_3: SID=139 device type=DISK

channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: restoring control file
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/fast_recovery_area/BOOK/autobackup/2017_06_27/o1_mf_s_947754283_do3csclx_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/BOOK/autobackup/2017_06_27/o1_mf_s_947754283_do3csclx_.bkp tag=TAG20170627T090443
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/home/oracle/aux/BOOK/controlfile/o1_mf_do3d10mf_.ctl
Finished restore at 2017-06-27 09:08:49

sql statement: alter database mount clone database

sql statement: alter system archive log current

sql statement: begin dbms_backup_restore.AutoBackupFlag(FALSE); end;

contents of Memory Script:
{
# set requested point in time
set until  scn 13276934358;
plsql <<<-- tspitr_2
declare
  sqlstatement       varchar2(512);
  offline_not_needed exception;
  pragma exception_init(offline_not_needed, -01539);
begin
  sqlstatement := 'alter tablespace '||  'TEA' ||' offline immediate';
  krmicd.writeMsg(6162, sqlstatement);
  krmicd.execSql(sqlstatement);
exception
  when offline_not_needed then
    null;
end; >>>;
# set destinations for recovery set and auxiliary set datafiles
set newname for clone datafile  1 to new;
set newname for clone datafile  3 to new;
set newname for clone datafile  2 to new;
set newname for clone tempfile  1 to new;
set newname for datafile  6 to
 "/mnt/ramdisk/book/tea01.dbf";
# switch all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  1, 3, 2, 6;
switch clone datafile all;
}
executing Memory Script

executing command: SET until clause
sql statement: alter tablespace TEA offline immediate

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

renamed tempfile 1 to /home/oracle/aux/BOOK/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 2017-06-27 09:08:54
using channel ORA_AUX_DISK_1
using channel ORA_AUX_DISK_2
using channel ORA_AUX_DISK_3

channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_1: restoring datafile 00003 to /home/oracle/aux/BOOK/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00006 to /mnt/ramdisk/book/tea01.dbf
channel ORA_AUX_DISK_1: reading from backup piece /home/oracle/backup/full_20170627_f8s7r50e_1_1.bak
channel ORA_AUX_DISK_2: starting datafile backup set restore
channel ORA_AUX_DISK_2: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_2: restoring datafile 00001 to /home/oracle/aux/BOOK/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_2: reading from backup piece /home/oracle/backup/full_20170627_f9s7r50e_1_1.bak
channel ORA_AUX_DISK_3: starting datafile backup set restore
channel ORA_AUX_DISK_3: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_3: restoring datafile 00002 to /home/oracle/aux/BOOK/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_3: reading from backup piece /home/oracle/backup/full_20170627_f7s7r50e_1_1.bak
channel ORA_AUX_DISK_1: piece handle=/home/oracle/backup/full_20170627_f8s7r50e_1_1.bak tag=TAG20170627T085958
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:15
channel ORA_AUX_DISK_2: piece handle=/home/oracle/backup/full_20170627_f9s7r50e_1_1.bak tag=TAG20170627T085958
channel ORA_AUX_DISK_2: restored backup piece 1
channel ORA_AUX_DISK_2: restore complete, elapsed time: 00:00:15
channel ORA_AUX_DISK_3: piece handle=/home/oracle/backup/full_20170627_f7s7r50e_1_1.bak tag=TAG20170627T085958
channel ORA_AUX_DISK_3: restored backup piece 1
channel ORA_AUX_DISK_3: restore complete, elapsed time: 00:00:15
Finished restore at 2017-06-27 09:09:09

datafile 1 switched to datafile copy
input datafile copy RECID=16 STAMP=947754549 file name=/home/oracle/aux/BOOK/datafile/o1_mf_system_do3d16td_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=17 STAMP=947754549 file name=/home/oracle/aux/BOOK/datafile/o1_mf_undotbs1_do3d16sz_.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=18 STAMP=947754549 file name=/home/oracle/aux/BOOK/datafile/o1_mf_sysaux_do3d16tf_.dbf

contents of Memory Script:
{
# set requested point in time
set until  scn 13276934358;
# online the datafiles restored or switched
sql clone "alter database datafile  1 online";
sql clone "alter database datafile  3 online";
sql clone "alter database datafile  2 online";
sql clone "alter database datafile  6 online";
# recover and open resetlogs
recover clone database tablespace  "TEA", "SYSTEM", "UNDOTBS1", "SYSAUX" delete archivelog;
alter clone database open resetlogs;
}
executing Memory Script

executing command: SET until clause

sql statement: alter database datafile  1 online

sql statement: alter database datafile  3 online

sql statement: alter database datafile  2 online

sql statement: alter database datafile  6 online

Starting recover at 2017-06-27 09:09:10
using channel ORA_AUX_DISK_1
using channel ORA_AUX_DISK_2
using channel ORA_AUX_DISK_3

starting media recovery
archived log for thread 1 with sequence 696 is already on disk as file /u01/app/oracle/archivelog/book/1_696_896605872.dbf
archived log for thread 1 with sequence 697 is already on disk as file /u01/app/oracle/archivelog/book/1_697_896605872.dbf
archived log for thread 1 with sequence 698 is already on disk as file /u01/app/oracle/archivelog/book/1_698_896605872.dbf
archived log file name=/u01/app/oracle/archivelog/book/1_696_896605872.dbf thread=1 sequence=696
archived log file name=/u01/app/oracle/archivelog/book/1_697_896605872.dbf thread=1 sequence=697
archived log file name=/u01/app/oracle/archivelog/book/1_698_896605872.dbf thread=1 sequence=698
media recovery complete, elapsed time: 00:00:00
Finished recover at 2017-06-27 09:09:11

database opened

contents of Memory Script:
{
# make read only the tablespace that will be exported
sql clone 'alter tablespace  TEA read only';
# create directory for datapump import
sql "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/home/oracle/aux''";
# create directory for datapump export
sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/home/oracle/aux''";
}
executing Memory Script

sql statement: alter tablespace  TEA read only

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle/aux''

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle/aux''

Performing export of metadata...
   EXPDP> Starting "SYS"."TSPITR_EXP_Bndc":
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   EXPDP> Master table "SYS"."TSPITR_EXP_Bndc" successfully loaded/unloaded
   EXPDP> ******************************************************************************
   EXPDP> Dump file set for SYS.TSPITR_EXP_Bndc is:
   EXPDP>   /home/oracle/aux/tspitr_Bndc_82023.dmp
   EXPDP> ******************************************************************************
   EXPDP> Datafiles required for transportable tablespace TEA:
   EXPDP>   /mnt/ramdisk/book/tea01.dbf
   EXPDP> Job "SYS"."TSPITR_EXP_Bndc" successfully completed at Tue Jun 27 09:09:53 2017 elapsed 0 00:00:35
Export completed


contents of Memory Script:
{
# shutdown clone before import
shutdown clone immediate
# drop target tablespaces before importing them back
sql 'drop tablespace  TEA including contents keep datafiles cascade constraints';
}
executing Memory Script
contents of Memory Script:
{
# shutdown clone before import
shutdown clone immediate
# drop target tablespaces before importing them back
sql 'drop tablespace  TEA including contents keep datafiles cascade constraints';
}
executing Memory Script

database closed
database dismounted
Oracle instance shut down

sql statement: drop tablespace  TEA including contents keep datafiles cascade constraints

Performing import of metadata...
   IMPDP> Master table "SYS"."TSPITR_IMP_Bndc" successfully loaded/unloaded
   IMPDP> Starting "SYS"."TSPITR_IMP_Bndc":
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   IMPDP> Job "SYS"."TSPITR_IMP_Bndc" successfully completed at Tue Jun 27 09:10:12 2017 elapsed 0 00:00:03
Import completed


contents of Memory Script:
{
# make read write and offline the imported tablespaces
sql 'alter tablespace  TEA read write';
sql 'alter tablespace  TEA offline';
# enable autobackups after TSPITR is finished
sql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;';
}
executing Memory Script

sql statement: alter tablespace  TEA read write

sql statement: alter tablespace  TEA offline

sql statement: begin dbms_backup_restore.AutoBackupFlag(TRUE); end;

Removing automatic instance
Automatic instance removed
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_temp_do3d1rfz_.tmp deleted
auxiliary instance file /home/oracle/aux/BOOK/onlinelog/o1_mf_3_do3d1r3y_.log deleted
auxiliary instance file /home/oracle/aux/BOOK/onlinelog/o1_mf_2_do3d1qz3_.log deleted
auxiliary instance file /home/oracle/aux/BOOK/onlinelog/o1_mf_1_do3d1qr3_.log deleted
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_sysaux_do3d16tf_.dbf deleted
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_undotbs1_do3d16sz_.dbf deleted
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_system_do3d16td_.dbf deleted
auxiliary instance file /home/oracle/aux/BOOK/controlfile/o1_mf_do3d10mf_.ctl deleted
Finished recover at 2017-06-27 09:10:15

RMAN>

--//整個過程就是通過建立輔組實例,恢復部分文件到的特定時間點或者scn,然後利用傳輸表空間導出源數據.然後
--//有1個sql 'drop tablespace  TEA including contents keep datafiles cascade constraints';
--//刪除表空間.copy對應數據文件
--//倒回源數據到資料庫.

5.測試是否恢復正常.
--//註意結束時表空間是offline的.保險你可以read only看看.
SCOTT@book> alter tablespace tea online  ;
Tablespace altered.

SCOTT@book> alter tablespace tea  read only ;
Tablespace altered.

SCOTT@book> select count(*) from t ;
    COUNT(*)
------------
       84762

SCOTT@book> select count(*) from emp2 ;
    COUNT(*)
------------
          14

SCOTT@book> select count(*) from dept2 ;
    COUNT(*)
------------
           4

--//你可以發現完全恢復了誤操作的內容.我一直認為這樣操作不是太合理,如果表空間很大,恢復工作量也很大,
--//12c 增加了 recover table會更加好一些.


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Android表情開發 效果圖: 源碼下載-github:https://github.com/SiberiaDante/EmotionApp (覺得有用的給個星星,支持一下哦) ...
  • 自定義樣式 dialog,可設置界面外點擊屏幕外和返回鍵 是否消失 基本用法如下: Utils 工具類封裝: 效果圖: git 源碼下載 To get a Git project into your build: To get a Git project into your build: To ge ...
  • 由於經常會出現 Schema hasn't been registered for model "User" 這樣的問題,所以深入的尋找了一下原因,一點一點的向上查找,最後解決了。 首先是app.js裡面在使用之前要定義,和初始化註冊一次mongoose() 如圖: 因為下麵的index和users ...
  • 神經網路(neural network)是深度學習中一種非常重要的模型,關於神經網路更詳細的介紹呢,這裡就不介紹了,可以自行搜索瞭解。文章主要整理了7個神經網路的實戰項目,相信對神經網路學習者會有所幫助~ ...
  • 記錄一次生產庫遇到的4031錯誤,後來通過調整sga大小將問題解決了 報錯信息: ORA-04031: 無法分配 32 位元組的共用記憶體 ("shared pool","select user#,password,datats...","SQLA","tmp")Incident details in: ...
  • 問題描述: Hbase master起不來,hdfs沒有問題,起來後沒過一會進程又宕了,hdfs磁碟爆了。 解決過程: 開始懷疑是zookeepr宕了。查看配置發現是zookeeper在三台slaves上面,可能會造成資源搶占,zookeeper會被hbase擠掉。 所以將其中一臺slaves轉到m ...
  • 資料庫引擎是高度優化的閉環系統,基於執行計劃的反饋,查詢優化器在一定程度上自動優化現有的執行計劃。查詢優化的核心是索引優化,資料庫引擎通過計數器統計關於索引操作的數據,統計的信息包括:使用次數、物理存儲、底層操作的計數,以及缺失索引等,這些統計數據存儲在記憶體中,是資料庫引擎執行情況的真實反饋,高度概 ...
  • 1、查詢的時候把某一個欄位的值拼接成字元串 以下是演示數據。 第一種方式:使用自定義變數 這種方法有一個好處就是,拼接出來的字元串可以賦值給變數或直接插入表中指定欄位,可以適用於存儲過程之類的。 第二種方式:轉換為 XML 格式 使用這種方式有一個缺點就是,不能直接賦值給變數或插入表,適用於查看時使 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...