需求描述:
由於使用了LOGRETAIN=ON資料庫配置,所以資料庫記錄檔將不會被資料庫從活動紀錄目錄中自動刪除或迴圈利用,活動紀錄目錄中的記錄檔將會越來越多,所以需要對該目錄下的記錄檔進行歸檔。
問題分析
將LOGRETAIN設定為ON後,資料庫將支援前滾恢複。此時,系統中將會存在三種類型的記錄檔:
活動紀錄:該日誌包含尚未提交或復原的事務單元的相關資訊,以及已提交但尚未寫入資料庫檔案的事務的資訊。
線上封存日誌:活動紀錄中所有改動對正常處理已不需要,即該日誌中所記錄的事務都已提交並寫入資料庫檔案時,該活動紀錄轉換為線上封存日誌。稱之為聯機,是由於它們與活動紀錄存放在同一個目錄下。
離線存檔日誌:將線上封存日誌從活動紀錄目錄下Copy到另外的地方存檔,就稱為離線存檔日誌。這些日誌可能在資料庫前滾恢複的時候仍然需要。
管理資料庫日誌的工作可以手工完成(即將線上封存日誌手工拷貝到某個archive目錄),也可由編寫USEREXIT程式自動維護。
由於DB2對所有平台都提供了相應的USEREXIT樣本程式,而且非常易於修改和使用,所以IBM建議使用者使用USEREXIT程式來管理記錄檔。
使用USEREXIT程式的方法
要使用User exit program自動化log檔案的archiving和retrieval過程:
1. 設定database cfg參數logarchmeth1 為USEREXIT。
connect to sample
update db cfg using logarchmeth1 USEREXIT
connect reset
2. 建立user exit programs。該程式必須是一個名為db2uext2的可執行檔。DB2在各種平台上都提供了User Exit的樣本程式,使用者可以通過直接修改該樣本檔案來實現自己的user exit程式。
在UNIX(R) based的系統中,該程式存放在sqllib/samples/c目錄下.
There are four sample user exit programs for UNIX based systems:
• db2uext2.ctsm
This sample uses Tivoli(R) Storage Manager to archive and retrieve database log files.
• db2uext2.ctape
This sample uses tape media to archive and retrieve database log files .
• db2uext2.cdisk
This sample uses the operating system COPY command and disk media to archive and retrieve database log files.
• db2uxt2.cxbsa
This sample works with the XBSA Draft 0.8 published by the X/Open group. It can be used to archive and retrieve database log files. This sample is only supported on AIX.
• 在Windows(R) 系統中,該檔案存放在sqllib/samples/c目錄下。
There are two sample user exit programs for Windows operating systems:
• db2uext2.ctsm
This sample uses Tivoli Storage Manager to archive and retrieve database log files.
• db2uext2.cdisk
This sample uses the operating system COPY command and disk media to archive and retrieve database log files.
3. 配置該樣本程式,指定要將記錄檔Archive到哪個系統目錄中(在windows平台下,程式中預設為c:/mylogs)。
#define ARCHIVE_PATH "c://mylogs//"
#define RETRIEVE_PATH "c://mylogs//"
#define AUDIT_ERROR_PATH "c://mylogs//"
4. 編譯該C檔案。
cl db2uext2.c
5. 拷貝db2uext2.exe到/SQLLIB/BIN目錄下。
UNIX環境下應該在/sqllib/adm下
6. 在db2cmd中鍵入archive log for db sample user db2admin using duan1980命令強制執行log日誌歸檔任務。查看c:/mylogs目錄下有無記錄檔歸檔,驗證是否成功。