Oracle檔案異常資料還原

Oracle檔案異常資料還原(temp,redo,index,file,password,control block error)

標籤: 
檔案異常資料還原(11gAW2 Cheaper6,7):
一、Temp file error
二、REDO Log file error:
三、Index error
四、File error
五、Password file error
六、Control file error
七、Block Error
 
Oracle Version:10g/11g
 

 
Temp file error:
分可以重啟及不可以重啟兩種狀況:
可重啟:把file 刪除掉,重新啟動oracle即可
不可重啟:建立tempfile,刪除舊有的tempfile,以下分兩種建立方式:
 
一、
SQL> select file_name from dba_temp_files where tablespace_name='TEMP';  –找出temp tablespace所使用的tempfiles
 
 
SQL> select tablespace_name from dba_tablespaces where contents='TEMPORARY';  –找出所有可用的temporary tablespace
 
–建議建立一個新temporary tablespace取代已經毀損的temporary tablespace
SQL> create temporary tablespace temp2 tempfile '/u02/oradata/orcl/temp2.dbf' size 100M  AUTOEXTEND ON NEXT 8K maxsize unlimited;;
 
–變更資料庫的預設temporary tablespace為temp2
SQL> alter database default temporary tablespace temp2;
 
查詢User的temp tablespace
SQL> select temporary_tablespace from dba_users where username='HR';
 
刪除壞掉的temp
SQL> drop tablespace temp INCLUDING CONTENTS AND DATAFILES;;
<====在原本的user session 沒斷前可能會卡住,須等session斷掉,或手動kill session才會成功drop
 
二、
SQL> alter tablespace temp add tempfile '/home/oracle/temp01.dbf' size 100M;    –add new tempfile到temp tablespace
 
SQL> alter tablespace temp drop tempfile '/u02/oradata/orcl/temp01.dbf';  –drop corrupted tempfile
 
SQL> select file_name from dba_temp_files where tablespace_name='TEMP';  –找出temp tablespace所使用的tempfiles
 
 
*********************************************************************
REDO Log file error:
如果是inactive,現在不影響運作狀況,等待轉換的時候,才會受影響
–一個database至少要有兩個logfile groups
–logfile group的特色,只要logfile group裡尚有一個以上的logfile member存在,則此logfile group還是維持正常的操作
–每個logfile group裡至少需要一個member,最多只能有5個members.建議各個member應該放在不同disk controller所管理的不同disk

Redo log擺放路徑
SQL> select group#,member from v$logfile order by group#;

redo log的狀態
SQL> select group#,sequence#,members,to_char(first_time,'YYYY-MM-DD:HH24:MI:SS'),status,archived from v$log;
current->lgwr正在使用的logfile group(如果需要將log buffer的redo entry寫到logfile group時,lgwr會把redo entry寫到此logfile group的所有logfile member之中)
inactive->如果現在發生instance crash.當instance重新啟動並進行rollforward時,不需要使用此logfile group的內容來復原dirty buffers
active->如果現在發生instance crash.當instance重新啟動並進行rollforward時,將需要使用此logfile group的內容,將dirty buffers復原

SQL> alter system checkpoint;  –要求將目前所有的dirty buffers全數寫回datafiles(Buffer cache寫入資料檔中).因此目前所有為ACTIVE的logfile group,在此指令完成後,都將變成INACTIVE

SQL> alter system switch logfile;  –lgwr更換current logfile group
                                   –並呼叫ckpt對舊的current logfile group進行checkpoint(dbwr將此log sequence#區間的dirty buffers變成clean buffer)
                                   –如果是archivelog mode,則也會呼叫archiver對舊的current logfile group進行archive

–針對尚未被archived的logfile group(若檔案遺失,會自動建立此file,db必需要經過alter system switch logfile才知道檔案移失,才會自動建立檔案)
SQL> alter database clear unarchived logfile group 2;

SQL>  select group#,sequence#,members,to_char(first_time,'YYYY-MM-DD:HH24:MI:SS') first_time,status,archived from v$log;

    GROUP#  SEQUENCE#    MEMBERS FIRST_TIME          STATUS           ARC
———- ———- ———- ——————- —————- —
         1         31          1 2010-10-12:04:47:06 INACTIVE         YES
         2          0          1 2010-10-12:04:48:37 UNUSED           YES
         3         33          1 2010-10-12:04:49:51 CURRENT          NO

查看archivelog sequence
SQL>  select sequence#,name from v$archived_log;

**此時應該立刻進行一次whole backup.這樣就不需要使用到log seq# 32,因為現在的current logfile sequence#為33
**不然如果現在有任何datafile發生media failure,當recovery它時,將需要log seq# 32

RMAN> backup database;

備註:
SQL> alter system set "_allow_resetlogs_corruption"=true scope=spfile;
設置此參數之后,在數據庫Open過程中,Oracle會跳過某些一致性檢查,從而使數據庫可能跳過不一致狀態。
注意_allow_resetlogs_corruption是Oracle中的一個隱含參數如果系統實在不能resetlogs方式打開的後只能出此下策
http://www.sadtojoy.com/aspx/Detail.aspx?id=3996

 
*********************************************************************
Index Error
純index tablespace發生media failure,可以採用以下的做法
1.當作一般的datafile,進行offline->restore->recover->online的復原操作
 
 
2.drop index tablespace,create new index tablespace,create index –因為index內容與table內容相依.所以只要table存在,便可以重建index
 
查詢User底下的tablespace的型態是否全部都為index
SQL> select segment_type,count(*) from dba_segments where tablespace_name='USERS' group by segment_type;
 
資料重建:
SQL> alter database datafile '/oradata1/testdb/index_ts01.dbf' offline drop;
 
SQL> drop tablespace index_ts including contents and datafiles;
 
SQL> create tablespace index_ts datafile '/oradata1/testdb/index_ts01.dbf' size 10M;
 
SQL> create index scott.t5_idx1 on scott.t5(object_id) tablespace index_ts;
 
透過rman回覆:
RMAN> run {
2> sql 'alter database datafile 4 offline';
3> restore datafile 4;
4> recover datafile 4;
5> sql 'alter database datafile 4 online';}
 
*********************************************************************
File Error
 
透過Rman的方式還原+Archive log還原,若是一般的User,不一定要停機,
[oracle@oracleDB ~]$ rman target /
RMAN>  run {
2> sql 'alter database datafile 6 offline';
3>  restore datafile 6;
4>  recover datafile 6;
5>  sql 'alter database datafile 6 online';}
 
 
*********************************************************************
Password file error
當user process要求以as sysdba或as sysoper身分建立session時,Oracle server使用下列2種方式驗證其身分
1.os驗證(user必須先登入os(Oracle Server所在的os),同時user所使用的os username必須屬於某個特殊的os group,即所謂的dba group(在安裝oracle software時指定的os group)
2.當os驗證失敗或使用remote登入時,則使用password file進行驗證
 
**重建密碼檔
[oracle@oracleDB ~]$ orapwd file=$ORACLE_HOME/dbs/orapworcl password=oracle entries=10  ignorecase=y –file為password file的名字,有特定的格式.orapw開頭,緊接著instance name,記得不需要副檔名                                                                                                     –password為sys的密碼(11g預設有區分大小寫)
 –ignorecase=y取消大小寫區分,可以向前相容.
 
SQL> select * from v$pwfile_users;  –顯示現在password file內的資訊
               
*********************************************************************
Control error(Control file的任一檔案壞掉,db就會有異常)
 
查看Control file 存放位置
SQL> select value from v$system_parameter where name='control_files';
 
1.如果只有部分的controlfile發生media failure,只需要將尚存好的controlfile複製給壞的controlfile即可,接著重新啟動即可(instance recovery自動發生)
  使用OS指命COPY即可
     [oracle@oracleDB ~]$ cp /u2/oradata/orcl/control01.ctl /u01/app/oracle/flash_recovery_area/orcl/control02.ctl
        SQL> alter database mount;
        SQL> alter database open; –將會進行instance recovery.因此無資料遺失.
 
2.controlfile全數毀損,則必須在nomount階段,restore之前所做的controlfile備份,
SQL>startup nomount;
–使用rman復原controlfile,目前instance已經在nomount階段
[oracle@oracleDB ~]$ rman target /
RMAN> restore controlfile from autobackup;   –由Oracle Server自行找尋最新的autobackup所產生的controlfile backup
——restore controlfile from '/home/oracle/orcl_control_20101020.ctl';  –DBA明確選擇使用某一個controlfile backup
——restore controlfile; –由RC的內容將controlfile復原–$ rman target / catalog rmanuser/oracle@192.168.56.1:1521/orcl
 
 
在mount下執行:
SQL>alter database mount;
SQL> recover database until cancel using backup controlfile;(應該會出錯…因還原出來的Control與System.dbf這個檔不一致)
 
因control遺失,所以要做restore database,從backup到現在的time,資料一併不見
RMAN> restore database;             
RMAN> recover database;
 
SQL> alter database open resetlogs;
 
查看現在的INSTANCE狀態
SQL> select status from v$instance;
 
–使用sql command備份controlfile為binary format
SQL> alter database backup controlfile to '/home/oracle/orcl_control_20101020.ctl';
 
*********************************************************************
Block Error
**主動使用rman指令驗證整個datafile,看看是否還有其他尚未發現的錯誤blocks
    –backup validate nochecksum datafile 4; –nochecksum將關閉自動physical corruption檢查
RMAN> backup validate datafile 4;  –只檢查physical corruption.預設自動檢查checksum(physical corruption)
    –backup check logical validate datafile 4; –同時檢查physical/logical corruption(check logical啟動logical corruption檢查)
 
 
查詢錯誤的block
SQL> select * from v$database_block_corruption;  –內容來自於v$backup_corruption與v$copy_corruption
                                                 –而v$backup_corruption的內容來自於執行rman的backup或backup validate所發現的corrupted blocks
                                                 –而v$copy_corruption的內容來自於執行rman的backup as copy或backup as copy validate所發現的corrupted blocks
 
**可以直接藉由跳過毀損block不讀取的方式,取得其他完好block的內容
SQL> execute dbms_repair.skip_corrupt_blocks('HR','T1',flags=>1); –可以不寫flags=>1,因為此選項為預設值
 
–blockrecover為10g的語法
–10g> blockrecover datafile 4 block 679;
RMAN> recover datafile 4 block 679;  –不需要offline datafile或tablespace(availability較高).僅需restore/recovery corrupted blocks(復原速度較快)
 
做完之後,再查詢一下是否有錯誤的block

Read more

How to document Home Lab and Network

運維機房和跨域的網路,會遇到各式需求與問題,用對工具才能分析問題,個人覺得最重要的是使用能處理問題的工具。 推薦目前想學和正在使用的平台與軟體,協助將公司/家用機房文件化 佈告欄任務管理 Focalboard 白板可管理任務指派 網路架構文件編寫 netbox 精細管理網路設備與連接線路 IP 資源管理 phpipam 專注網路IP分配 邏輯塊文件編寫 draw.io 視覺化概念圖 機房設備管理 ITDB 管理設備生命週期與使用者

By Phillips Hsieh

如何在Raspberry Pi4上安裝Proxmox for ARM64

第一步 準備好Raspberry Pi 4 / CM4 4GB RAM,這裡要留意CM4如果是買有內建eMMC storage會限制不能使用SD卡開機而限制本地空間容量,如果沒有NAS外接空間或使用USB開機的話,建議買CM4 Lite插上大容量SD卡 第二步 去Armbian官網下載最小化Debian bookworm image https://www.armbian.com/rpi4b/ Armbian 25.2.2 Bookworm Minimal / IOT 然後寫入SD/USB開機碟,寫入方法參考官方文件 https://github.com/raspberrypi/usbboot/blob/master/Readme.md Note: 官方提供的預先設定系統方法,可以在Armbian初次啟動自動化完成系統設定。連結在此 https://docs.armbian.com/User-Guide_Autoconfig/

By Phillips Hsieh

世界越快心越慢

在晚飯後的休息時間,我特別享受在客廳瀏灠youtube上各樣各式創作者的影音作品。很大不同於傳統媒體,節目多是針對大多數族群喜好挑選的,在youtube上我會依心情看無腦的動畫、一些旅拍記錄、新聞時事談論。 尤其在看了大量的Youtube的分享後,我真的感受到會限制我的是我的無知,特別是那些我想都沒想過的實際應用,在學習後大大幫助到我的生活和工作層面。 休息在家時,我喜歡想一些沒做過的菜,動手去設計生活和工作上的解決方案,自己是真的很難閒著沒事做。 如創作文章,陪養新的習慣都能感覺到成長的喜悅,是不同於吃喝玩樂的快樂的。 創作不去限制固定的形式,文字是創作、影像聲音也是創作,記錄生活也是創作,我想留下的就是創造—》實現—》回憶,這樣子的循環過程,在留下的足跡面看到自己一路上的成長、失敗、絕望、重新再來。 雖然大部份的時候去做這些創作也不明白有什麼特別的意義,但不去做也不會留下什麼,所以呀不如反事都去試試看,也許能有不一樣的水花也許有意想不到的結果,投資自己永遠不會是失敗的決定,不是嗎?先問問自己再開始計畫下一步,未來沒人說得準。 像最近看youtube仍大一群人在為DOS開

By Phillips Hsieh

知識管理的三個步驟:一小時學會把知識運用到生活上

摘錄瓦基「閱讀前哨站」文章作為自己學習知識管理的內容 Part1「篩選資訊」 如何從海量資訊中篩選出啟發性、實用性和相關性的精華,讓你在學習過程中不再迷失方向。 1. 實用性 2. 啟發性 Part2「提高理解」 如何通過譬喻法和應用法,將抽象的知識與日常生活和工作緊密結合,建立更深刻的理解。 1. 應用法 2. 譬喻法 Part3「運用知識」 如何連結既有知識,跟自己感興趣的領域和專案產生關聯,讓你在運用知識的路途上游刃有餘。 1. 跟日常工作專案、人際活動產生連結 # 為什麼要寫日記? * 寫日記是為了忘記,忘卻瑣碎事情,保持專注力 * 寫日記就像在翻譯這個世界,訓練自己的解讀能力 * 不只是透過日記來記錄生活,而是透過日記來發展生活 #如何寫日記? * 不要寫流水帳式的日記,而是寫覆盤式的日記 當我們試著記錄活動和感受之間的關聯,有助於辦認出真正快樂的事 日記的記錄方式要以過程為主,而非結果 * 感恩日記的科學建議,每日感恩的案例

By Phillips Hsieh