DB2 歸檔日誌資訊記錄在恢復歷史檔案中。PRUNE HISTORY 指令可清理歷史條目,搭配 AND DELETE 同步刪除實體日誌檔案。本文記錄完整清理流程與關鍵參數行為。
db2 list history archive log all for <dbname>
輸出包含:操作類型、時間戳、最早/目前日誌檔案、備份 ID、實體檔案路徑 (EID Location)。
# 必須先連線資料庫
db2 connect to <dbname>
# 清理指定時間戳及之前的所有歷史條目
db2 prune history 20100103235553
DB20000I The PRUNE command completed successfully.
預設保護機制:不會清理最近一次完整備份之後的歸檔日誌條目。
db2 prune history 20100103235608 AND DELETE
DB20000I The PRUNE command completed successfully.
# 驗證實體檔案已刪除
ls -l /data/palog/db2inst1/GGYY/NODE0000/C0000007/
# S0000039.LOG 已消失
# 嘗試清理含完整備份後的日誌 (預設失敗)
db2 prune history 20100104004127 AND DELETE
-- S0000041.LOG 仍存在,因為它在完整備份之後生成
# 強制清理 (連同備份歷史條目一併刪除)
db2 prune history 20100104004127 WITH FORCE OPTION AND DELETE
DB20000I The PRUNE command completed successfully.
# 結果:所有歸檔日誌條目清空,實體檔案僅剩備份前的 S0000038.LOG
# 注意:備份歷史條目也被刪除,但備份實體檔案仍保留在磁碟
| 指令 | 清理歷史條目 | 刪除實體日誌檔 | 受備份保護 | 刪除備份條目 |
|---|---|---|---|---|
| PRUNE HISTORY timestamp | ✅ | ❌ | 是 (保留備份後) | ❌ |
| PRUNE HISTORY timestamp AND DELETE | ✅ | ✅ | 是 | ❌ |
| PRUNE HISTORY timestamp WITH FORCE OPTION | ✅ | ❌ | 否 (強制清理) | ✅ |
| PRUNE HISTORY timestamp WITH FORCE OPTION AND DELETE | ✅ | ✅ | 否 | ✅ |
db2 backup db <db> online to <path>PRUNE HISTORY <備份前時間戳> AND DELETE 清理備份前日誌# 僅清理作用中日誌目錄下的線上歸檔日誌
# 對離線歸檔日誌目錄無效
db2 prune logfile prior to S0000041.LOG
LIST HISTORY BACKUP 看不到備份,但實體備份檔仍在 — 需手動記錄備份資訊AUTO_DEL_REC_OBJ 參數自動清理 (需測試驗證)規律性執行 PRUNE HISTORY ... AND DELETE 是 DB2 日誌空間管理的核心運維任務。