• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            DB2 rollforward 命令使用詳解

            Posted on 2010-03-23 21:30 Prayer 閱讀(1460) 評(píng)論(0)  編輯 收藏 引用 所屬分類: DB2
            http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-1003wucx/

            引言

            在數(shù)據(jù)庫成為存儲(chǔ)企業(yè)數(shù)據(jù)的載體時(shí),如何恢復(fù)數(shù)據(jù)成為大家比較關(guān)心的話題。在 DB2 中恢復(fù)數(shù)據(jù)的種類很多,本文主要介紹數(shù)據(jù) DB2 rollforward 的使用說明,并使用 DB2 V97 做了一些實(shí)例分析,使讀者更好的了解和掌握 rollforward 的使用。


            Recovery 介紹

            DB2 中有四種恢復(fù)數(shù)據(jù)庫的方式,以下對(duì)其進(jìn)行介紹說明:

            • Crash Recovery 是指在事務(wù)處理過程中被中斷,從而可能造成數(shù)據(jù)不一致,不可用。這時(shí) Crash Recovery 就會(huì)保護(hù)數(shù)據(jù)庫,避免造成數(shù)據(jù)不一致,不可用的情況。
            • High Availability Disaster Recovery 是指高可用性災(zāi)難恢復(fù)(HADR),其通過將數(shù)據(jù)從源數(shù)據(jù)庫復(fù)制到目標(biāo)數(shù)據(jù)庫來防止災(zāi)難性的數(shù)據(jù)丟失。
            • Version Recovery 是對(duì)備份介質(zhì)的恢復(fù),主要是 restore 命令。
            • Rollforward Recovery,一般是當(dāng)數(shù)據(jù)庫備份后,又執(zhí)行了一些新事務(wù),如果這時(shí)發(fā)生了存儲(chǔ)故障或誤操作,在 restore 之后使用 rollforward 就可以對(duì)這些新事務(wù)進(jìn)行修復(fù),本文主要介紹這種恢復(fù)方式。

            rollforward 準(zhǔn)備工作

            rollforward 只能在數(shù)據(jù)庫 recoverable 狀態(tài)下使用,即采用歸檔日志,參數(shù) logarchmeth1 或者 logarchmeth2 為非 OFF 的狀態(tài)。

            首先需要修改參數(shù),使得數(shù)據(jù)庫在 recoverable 狀態(tài)下。


            清單 1. 修改參數(shù) LOGARCHMETH1
                        db2 "update db cfg for $db using logfilsiz 4 LOGARCHMETH1 disk:$archivePath"
                        

            如果修改參數(shù)前 logarchmeth1 為 OFF,修改參數(shù)后,數(shù)據(jù)庫處于 backup pending 狀態(tài),需要進(jìn)行 offline backup 來使數(shù)據(jù)庫可用。


            清單 2. 備份數(shù)據(jù)庫
                        db2 "backup db $db to $backupPath"
                        

            備份好數(shù)據(jù)庫后執(zhí)行一些事務(wù),例如簡單事務(wù)操作 create table,insert,update,delete 等等。然后 Restore 數(shù)據(jù)庫,如果是 tablespace 級(jí)別的 rollforward,還可以用其他方式來使表空間處于 rollforward-pending 狀態(tài)。


            清單 3. 恢復(fù)數(shù)據(jù)庫
                        db2 "restore db $db from $backdir without prompting"
                        

            如果使用 restore db 時(shí)指定 WITHOUT ROLLING FORWARD 參數(shù),則不能使數(shù)據(jù)庫處于 rollforward-pending 狀態(tài),從而不能使用 rollforward。這里不指定 WITHOUT ROLLING FORWARD 參數(shù),當(dāng)數(shù)據(jù)庫或表空間處于 rollforward-pending 狀態(tài),就可以對(duì)其進(jìn)行 rollforward 操作了。


            Rollforward 使用介紹

            在數(shù)據(jù)庫日志中,記錄了對(duì)該數(shù)據(jù)庫的所有操作,用 rollforward 可以通過前滾日志把數(shù)據(jù)庫恢復(fù)到備份后有日志記錄的某一時(shí)間點(diǎn),或者活動(dòng)日志的末尾。

            常用的 rollforward 參數(shù)有以下幾個(gè):

            • QUERY STATUS,主要查詢數(shù)據(jù)庫當(dāng)前狀態(tài)。
            • STOP,特指 rollforward 完成,這樣就不能在執(zhí)行任何別的 rollforward 操作了,除非重新 restore 然后 rollforward。
            • CANCEL,取消 rollforward 操作,使進(jìn)行 rollforward 中的數(shù)據(jù)庫退出 rollforward pending 狀態(tài)。
            • POINT IN TIME,rollforward 到某一特定時(shí)間點(diǎn)。
            • END OF LOGS,rollforward 到活動(dòng)日志的末尾。
            • ONLINE,使表空間級(jí)的 rollforward 在執(zhí)行時(shí)處于 online 狀態(tài),允許其他指向數(shù)據(jù)庫的連接。

            在分區(qū)數(shù)據(jù)環(huán)境中,rollforward 操作必須執(zhí)行在某個(gè)分區(qū)上,以下簡單進(jìn)行介紹:

            • point-in-time rollforward 會(huì)在所有的 partition 上執(zhí)行。
            • END OF LOGS rollforward,如果有 ON DATABASE PARTITION 參數(shù),rollforward 只在指定 partition 上運(yùn)行,如果沒有指定 partition,那么會(huì)在所有 partition 上生效。
            • end of backup 會(huì)影響所有的 partition。

            綜合上述參數(shù),可以把 rollforward 分為兩種類型,數(shù)據(jù)庫級(jí)別的 rollforward 和表空間級(jí)的 rollforward。數(shù)據(jù)庫級(jí)的 rollforward 需要數(shù)據(jù)庫首先 restore,從而使數(shù)據(jù)庫處于 rollforward pending 狀態(tài)。而表空間級(jí)的 rolllforward 并不是只有 restore 才能成為 rollforward pending 狀態(tài),比如突然斷電或者其他情況等等也可能使表空間進(jìn)行 rollforward 操作。

            當(dāng)執(zhí)行 rollforward 時(shí),如果是數(shù)據(jù)庫在 rollforward pending 狀態(tài),則進(jìn)行數(shù)據(jù)庫級(jí) rollforward。如果執(zhí)行完后某些表空間還是 rollforward pending 狀態(tài),則要執(zhí)行表空間級(jí)的 rollforward 來使這些表空間恢復(fù)。在表空間級(jí)的 rollforward 中,可以指定表空間來進(jìn)行 rollforward,如果未指定,則所有處于此狀態(tài)的表空間都會(huì)進(jìn)行 rollforward。如果在 backup 后,更改了一個(gè)表空間的名字,那么在 rollforward 該表空間時(shí)需要使用新的表空間名字。

            數(shù)據(jù)庫級(jí) rollforward 和表空間級(jí)還有一點(diǎn)不同就是,數(shù)據(jù)庫級(jí)進(jìn)行 rollforward 時(shí)該數(shù)據(jù)庫不能進(jìn)行其他連接操作,也就是必須斷開其他連接才能進(jìn)行數(shù)據(jù)庫級(jí)的 rollforward。而表空間級(jí)的可以選擇其他操作能否進(jìn)行。

            不能取消正在進(jìn)行的 rollfoward 操作,只能在 rollfoward 完成后,使用 rollforward cancel 來取消還沒有 stop 的 rollforward,如果已經(jīng)有 stop 了,就不能 cancel 了。


            rollforward 案例分析

            Tablespace rollforward 實(shí)例

            當(dāng)數(shù)據(jù)庫的數(shù)據(jù)量較大時(shí),數(shù)據(jù)庫的全備份和恢復(fù)都非常消耗時(shí)間,這時(shí)通過表空間備份可以快速恢復(fù)數(shù)據(jù)庫。

            在這個(gè)例子中,Restore 三個(gè)表空間,使用 rollforward to the end of the logs 前滾一個(gè)表空間到活動(dòng)日志的末尾,然后使用 to the end of the logs and stop 前滾另外兩個(gè)表空間到活動(dòng)日志的末尾。

            作為 restore 和 rollforward 的先決條件,首先需要 backup 每一個(gè)表空間,如清單 4 所示。


            清單 4. Backup 表空間
                        db2 "update db cfg for test using logfilsiz 4 LOGARCHMETH1 disk:$archivePath"
                        db2 "backup db test to $backupPath"
                        db2 "connect to test"
                        db2 "create tablespace tbs1"
                        db2 "create tablespace tbs2"
                        db2 "create tablespace tbs3"
                        db2 "backup db test tablespace(tbs1) to $backupPath/tbs1"
                        db2 "backup db test tablespace(tbs2) to $backupPath/tbs2"
                        db2 "backup db test tablespace(tbs3) to $backupPath/tbs3"
                        

            圖 1 顯示了備份結(jié)果。


            圖 1. Backup tablespace
            圖 1. Backup tablespace

            備份之后可以在指定的目錄中看到相應(yīng)的備份影像。

            清單 5 顯示了對(duì)三個(gè)表空間創(chuàng)建表和插入數(shù)據(jù)的操作清單。


            清單 5. 操作表空間
                        db2 "connect to test"
                        db2 "create table t1(a int) in tbs1"
                        db2 "create table t2(a int) in tbs2"
                        db2 "create table t3(a int) in tbs3"
                        db2 "insert int t1 values(1)"
                        db2 "insert int t2 values(2)"
                        db2 "insert int t3 values(3)"
                        

            操作完成后,可以通過剛才的備份影象對(duì)三個(gè)表空間進(jìn)行 restore。要注意恢復(fù)的備份影像一定要和所恢復(fù)的表空間對(duì)應(yīng),不然就會(huì)恢復(fù)失敗,如清單 6 所示。


            清單 6. Restore 三個(gè)表空間
                        db2 "restore db test tablespace(tbs1) from $backupPath/tbs1 without prompting"
                        db2 "restore db test tablespace(tbs2) from $backupPath/tbs2 without prompting"
                        db2 "restore db test tablespace(tbs3) from $backupPath/tbs3 without prompting"
                        

            圖 2 顯示了操作表空間和恢復(fù)表空間的結(jié)果。


            圖 2. 插入數(shù)據(jù)后恢復(fù)表空間
            圖 2. 插入數(shù)據(jù)后恢復(fù)表空間

            恢復(fù)成功之后,三個(gè)表空間都處于 rollforward pending 狀態(tài),用戶無法訪問,狀態(tài)如圖 3 所示。


            圖 3. 驗(yàn)證表空間 rollforward-pending 狀態(tài)
            圖 3. 驗(yàn)證表空間 rollforward-pending 狀態(tài)

            清單 7 顯示了訪問表空間的命令。


            清單 7. 訪問表空間
                        db2 "connect to test"
                        db2 "select * from t1"
                        db2 "select * from t2"
                        db2 "select * from t3"
                        

            此時(shí)需要做前滾操作使表空間回到可用狀態(tài),如清單 8 所示。


            清單 8. 前滾表空間并驗(yàn)證結(jié)果
                        db2 "rollforward db test to end of logs tablespace(tbs1) online"
                        db2 "rollforward db test to end of logs and stop tablespace(tbs2, tbs3) online"
                        db2 "connect to test"
                        db2 "select * from t1"
                        db2 "select * from t2"
                        db2 "select * from t3"
                        

            圖 4 正確返回對(duì)表的操作結(jié)果,說明 rollforward 成功。


            圖 4. 驗(yàn)證結(jié)果
            圖 4. 驗(yàn)證結(jié)果

            rollforward query status 實(shí)例

            rollforward 命令的 query status 選項(xiàng)可用于列出如下一些當(dāng)前數(shù)據(jù)庫的信息。

            1. DB2 已經(jīng)前滾的日志文件。
            2. 需要的下一個(gè)歸檔日志文件。
            3. 自前滾處理開始以來最近提交的事務(wù)的時(shí)間戳。

            下面使用清單 9 來查詢數(shù)據(jù)庫狀態(tài)。


            清單 9. 前滾數(shù)據(jù)庫并查詢狀態(tài)
                        db2 "rollforward db test query status"
                        

            圖 5 中顯示 DB pending 狀態(tài)的數(shù)據(jù)庫,實(shí)際上是處于 rollforward pending 狀態(tài)。需要前滾數(shù)據(jù)庫,使數(shù)據(jù)庫回到正常狀態(tài)。


            圖 5. Check Rollforward Pending Status
            圖 5. Check Rollforward Pending Status

            rollforward cancel 實(shí)例

            在進(jìn)行 cancel 操作前,先執(zhí)行最基本的操作,db2 rollforward db $db to end of logs,操作完成后所有在數(shù)據(jù)庫備份后寫的日志文件都會(huì)前滾,如圖 6 所示。


            圖 6. Rollforward to end of logs
            圖 6. Rollforward to end of logs

            接下來執(zhí)行 db2 rollforward db $db cancel 操作,即取消前滾操作。執(zhí)行之后前面所做的所有前滾操作都將回滾,數(shù)據(jù)庫重新處于 restore-pending 狀態(tài),此時(shí)任何連接都被拒絕。

            清單 10 列出了取消前滾并進(jìn)行 restore 的命令。


            清單 10. cancel rollforward and restore
                        db2 "rollforward db test cancel"
                        db2 "connect to test"
                        db2 "restore db test from $backupPath without prompting"
                        db2 "connect to test"
                        

            當(dāng)剛?cè)∠皾L時(shí),由于數(shù)據(jù)庫處于 restore pending 狀態(tài),無法連接上數(shù)據(jù)庫,如圖 7 所示。


            圖 7. Restore Pending 狀態(tài)
            圖 7. Restore Pending 狀態(tài)

            重新執(zhí)行 restore 操作,db2 restore db $db from $backdir without prompting,

            此時(shí)數(shù)據(jù)庫處于 rollforward-pending 狀態(tài),此時(shí)任何連接都被拒絕,如圖 8 所示


            圖 8. Rollforward Pending 狀態(tài)
            圖 8. Rollforward Pending 狀態(tài)

            Rollforward to end of logs and complete 實(shí)例

            接著上面的實(shí)例,執(zhí)行前滾操作來使數(shù)據(jù)庫可以恢復(fù)正常狀態(tài)。執(zhí)行 db2 rollforward db $db to end of logs and complete 操作,將前滾到日志的最后,這意味著所有歸檔的日志和活動(dòng)日志都被遍歷。如清單 11 和圖 9 所示。


            清單 11. 前滾到活動(dòng)日志末尾并完成前滾
                        db2 "rollforward db test to end of logs and complete"
                        


            圖 9. Rollforward Completed
            圖 9. Rollforward Completed

            rollforward interrupted 實(shí)例

            如果在執(zhí)行 rollforward to end of logs 中,用戶誤操作或者其他原因不小心中斷了前滾操作,那么再次執(zhí)行 rollforward to end of logs 時(shí),會(huì)接著上次的中斷狀態(tài),繼續(xù)執(zhí)行以至完成。例如:db2 rollforward db $db to end of logs 在執(zhí)行過程中按 Ctrl+C,使前滾操作執(zhí)行中斷,此時(shí)數(shù)據(jù)庫仍然處于 rollforward-pending 狀態(tài)。清單 12 列出了這個(gè)實(shí)例的命令。


            清單 12. 前滾中斷
                        db2 "rollforward db test to end of logs"
                        Push Ctrl+C
                        db2 "rollforward db test to end of logs and stop"
                        db2 "connect to test"
                        

            圖 10 顯示了中斷 rollfoward 命令的結(jié)果。


            圖 10. Rollforward Pending 狀態(tài)
            圖 10. Rollforward Pending 狀態(tài)

            此時(shí)執(zhí)行前滾操作 db2 rollforward db $db to end of logs and stop,rollforward 繼續(xù)執(zhí)行至完成,這時(shí)數(shù)據(jù)庫連接成功,如圖 11 所示。


            圖 11. Rollforward Completed
            圖 11. Rollforward Completed

            rollforward noretrieve 實(shí)例

            noretrieve 參數(shù)表明不需要取回已經(jīng)歸檔的日志,所以 rollforward db $db to end of logs and stop noretrieve 成功與否取決與 rollforward 是否需要取回歸檔日志文件。如果需要,因?yàn)榍皾L參數(shù)指定 noretrieve,那前滾會(huì)因?yàn)槿鄙龠@些日志文件而失敗。清單 13 和圖 12 列出了這個(gè)實(shí)例。


            清單 13. 不需要取回歸檔日志的前滾
                        db2 "rollforward db test to end of logs and stop noretrieve"
                        


            圖 12. Rollforward With noretrieve
            圖 12. Rollforward With noretrieve

            rollforward overflow log path 實(shí)例

            參數(shù) overflow log path 會(huì)覆蓋以前設(shè)置的 overflow log path,如果先前設(shè)置了 db2 update db cfg for test using overflowlogpath $path,那么使用 rollforward db $db to end of logs and complete overflow log path ($overflow) 命令,$overflow 會(huì)取代 $path 成為新的 overflow log path。清單 14 和圖 13 顯示了該實(shí)例。


            清單 14. 覆蓋 logpath 的前滾
                        db2 "rollforward db test to end of logs and complete overflow log path ($overflowPath2)"
                        


            圖 13. Rollforward With overflow log path
            圖 13. Rollforward With overflow log path

            在 rollforward 執(zhí)行過程中可以看到在新的目錄 $overflow 中的日志文件,可見參數(shù)生效,如圖 14 所示。


            圖 14. Overflow 日志列表
            圖 14. Overflow 日志列表

            rollforward backup 實(shí)例

            數(shù)據(jù)庫還可以進(jìn)行 rollforward backup 操作,如清單 15 所示。


            清單 15. 前滾備份
                        db2 "backup db test online to $backupPath include logs"
                        db2 "restore db test from $backupPath taken at 20091101125919 without prompting"
                        db2 "rollforward db test to end of backup"
                        

            圖 15 顯示了 online backup 的結(jié)果。


            圖 15. Online backup
            圖 15. Online backup

            然后執(zhí)行

             db2 restore db $db from $backupPath taken at $backupNum without prompting 

             db2 rollforward db $db to end of backup 

            就可以進(jìn)行恢復(fù)。Rollforward to the end of backup 可以前滾分區(qū)數(shù)據(jù)庫中所有的分區(qū)到最近的恢復(fù)點(diǎn)。要人工的定位這個(gè)恢復(fù)點(diǎn)很難,特別是對(duì)分區(qū)數(shù)據(jù)庫難上加難了,而使用 end of backup 卻很容易定位。圖 16 顯示了最終結(jié)果。


            圖 16. Rollforward to end of backup
            圖 16. Rollforward to end of backup

            結(jié)束語

            本文簡單的對(duì) rollforward 的使用進(jìn)行了介紹,并使用 DB2 V97 做了簡單的實(shí)例說明,從而能根據(jù)需要使用相應(yīng)的參數(shù),更好的運(yùn)用 rollforward。


            參考資料

            學(xué)習(xí)

            獲得產(chǎn)品和技術(shù)

            • 使用可直接從 developerWorks 下載的 IBM 產(chǎn)品評(píng)估試用軟件 構(gòu)建您的下一個(gè)開發(fā)項(xiàng)目。

            • 現(xiàn)在可以免費(fèi)使用 DB2。下載 DB2 Express-C,這是為社區(qū)提供的 DB2 Express Edition 的免費(fèi)版本,它提供了與 DB2 Express Edition 相同的核心數(shù)據(jù)特性,為構(gòu)建和部署應(yīng)用程序奠定了堅(jiān)實(shí)的基礎(chǔ)。

            討論

            作者簡介

            武翠霞的照片

            武翠霞,IBM 中國軟件開發(fā)中心軟件工程師,Business Intelligence 項(xiàng)目組,兩年商業(yè)智能項(xiàng)目經(jīng)驗(yàn),目前從事 DB2 產(chǎn)品的測(cè)試。

            李茂嘉的照片

            李茂嘉,IBM 中國軟件開發(fā)中心軟件工程師,Business Intelligence 項(xiàng)目組,兩年來一直從事 IBM 數(shù)據(jù)庫,數(shù)據(jù)倉庫系列產(chǎn)品的測(cè)試工作。

            国内精品免费久久影院| 久久精品一区二区三区中文字幕| segui久久国产精品| 国产福利电影一区二区三区久久久久成人精品综合 | 国产AV影片久久久久久| 久久久久国产一级毛片高清板| 久久亚洲日韩看片无码| 久久国产精品成人免费| 亚洲精品第一综合99久久 | 精品人妻久久久久久888| 精品熟女少妇aⅴ免费久久| 亚洲精品美女久久777777| 国产无套内射久久久国产| 无码人妻久久一区二区三区免费| 久久99精品国产麻豆婷婷| 久久国产精品77777| 婷婷久久五月天| 亚洲综合久久综合激情久久| 久久久久久久久66精品片| 国产综合成人久久大片91| 国产精品久久久久AV福利动漫| 亚洲一级Av无码毛片久久精品| 久久婷婷综合中文字幕| 伊人久久综合成人网| 久久久久亚洲AV综合波多野结衣| 国产精品午夜久久| 国产午夜免费高清久久影院| 波多野结衣久久一区二区| 国产视频久久| 狠狠久久综合| 国产无套内射久久久国产| 51久久夜色精品国产| 91久久精品91久久性色| 亚洲国产成人久久精品影视| 精品免费久久久久久久| 久久99精品久久久久久hb无码| 亚洲国产另类久久久精品小说| 色综合久久夜色精品国产| 超级97碰碰碰碰久久久久最新| 四虎国产精品成人免费久久| 久久久久精品国产亚洲AV无码|