DB2 - 備份與恢復



本章描述資料庫的備份和恢復方法。

backup

簡介

備份和恢復方法旨在確保資訊安全。您可以使用命令列介面 (CLI) 或圖形使用者介面 (GUI) 中的備份和恢復實用程式來備份或恢復 DB2 UDB 資料庫中的資料。

日誌記錄

日誌檔案包含錯誤日誌,用於從應用程式錯誤中恢復。日誌記錄資料庫更改的記錄。下面描述兩種型別的日誌記錄:

迴圈日誌記錄

這是一種方法,當需要分配新的事務日誌檔案時,舊的事務日誌會被覆蓋,從而擦除日誌檔案的序列並重復使用它們。您只能在離線模式下進行完全備份,即資料庫必須離線才能進行完全備份。

歸檔日誌記錄

此模式支援使用稱為前滾恢復的日誌檔案的聯機備份和資料庫恢復。可以透過將 logretain 或 userexit 設定為 ON 來更改從迴圈日誌記錄到歸檔日誌記錄的模式。對於歸檔日誌記錄,備份設定資料庫需要一個 DB2 程序可寫入的目錄。

備份

使用備份命令,您可以複製整個資料庫。此備份副本包括資料庫系統檔案、資料檔案、日誌檔案、控制資訊等等。

您可以在離線和聯機狀態下進行備份。

離線備份

語法:[列出活動應用程式/資料庫]

db2 list application  

輸出

Auth Id  Application    Appl.      Application Id                                                
DB       # of   
         Name           Handle              
Name    Agents  
-------- -------------- ---------- ---------------------
----------------------------------------- -------- -----  
DB2INST1 db2bp          39         
*LOCAL.db2inst1.140722043938                                   
ONE      1  

語法:[強制使用 app.Handled id 的應用程式]

db2 "force application (39)"   

輸出

DB20000I  The FORCE APPLICATION command completed 
successfully.  

DB21024I  This command is asynchronous and may not 
be effective immediately. 

語法:[終止資料庫連線]

db2 terminate  

語法:[停用資料庫]

db2 deactivate database one   

語法:[建立備份檔案]

db2 backup database <db_name> to <location>   

示例

db2 backup database one to /home/db2inst1/ 

輸出

Backup successful. The timestamp for this backup image is : 
20140722105345  

聯機備份

要開始,您需要將模式從迴圈日誌記錄更改為歸檔日誌記錄

語法:[檢查資料庫是使用迴圈日誌記錄還是歸檔日誌記錄]

db2 get db cfg for one | grep LOGARCH   

輸出

First log archive method (LOGARCHMETH1) = OFF  
 Archive compression for logarchmeth1  (LOGARCHCOMPR1) = OFF 
 Options for logarchmeth1              (LOGARCHOPT1) =   
 Second log archive method             (LOGARCHMETH2) = OFF  
 Archive compression for logarchmeth2  (LOGARCHCOMPR2) = OFF  
 Options for logarchmeth2              (LOGARCHOPT2) =   

在上面的輸出中,突出顯示的值是配置檔案中處於關閉模式的[logarchmeth1 和 logarchmeth2],這意味著當前資料庫處於“迴圈日誌記錄”模式。如果您需要使用“歸檔日誌記錄”模式,則需要更改或新增 logarchmeth1 和 logarchmeth2 變數中存在的路徑。

使用所需的歸檔目錄更新 logarchmeth1

語法:[建立目錄]

mkdir backup 
mkdir backup/ArchiveDest    

語法:[為資料夾提供使用者許可權]

chown db2inst1:db2iadm1 backup/ArchiveDest 

語法:[更新配置 LOGARCHMETH1]

db2 update database configuration for one using LOGARCHMETH1 
'DISK:/home/db2inst1/backup/ArchiveDest'

您可以進行離線備份以確保安全,然後啟用資料庫並連線到它。

語法:[進行聯機備份]

db2 backup database one online to 
/home/db2inst1/onlinebackup/ compress include logs   

輸出

db2 backup database one online to 
/home/db2inst1/onlinebackup/ compress include logs    

使用以下命令驗證備份檔案

語法

db2ckbkp <location/backup file>   

示例

db2ckbkp 
/home/db2inst1/ONE.0.db2inst1.DBPART000.20140722112743.001 

列出備份檔案歷史記錄

語法

db2 list history backup all for one    

輸出

                    List History File for one 
  
Number of matching file entries = 4 
 
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  
Backup ID  
 -- --- ------------------ ---- --- ------------ ------------ 
 --------------
  B  D  20140722105345001   F    D  S0000000.LOG S0000000.LOG 

 ------------------------------------------------------------ 
 ----------------   
 
 Contains 4 tablespace(s): 
 00001 SYSCATSPACE  
 
 00002 USERSPACE1
 
 00003 SYSTOOLSPACE 
 
 00004 TS1 
  ------------------------------------------------------------ 
  ---------------- 
  Comment: DB2 BACKUP ONE OFFLINE  
  
 Start Time: 20140722105345  
 
   End Time: 20140722105347
   
     Status: A
 ------------------------------------------------------------ 
 ---------------- 
 EID: 3 Location: /home/db2inst1 

 
 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  
 Backup ID
 -- --- ------------------ ---- --- ------------ ------------ 
 --------------  
  B  D  20140722112239000   N       S0000000.LOG S0000000.LOG   
 ------------------------------------------------------------ 
 ------------------------------------------------------------- 
 ------------------------------- 
 
 Comment: DB2 BACKUP ONE ONLINE  
 
 Start Time: 20140722112239 
 
   End Time: 20140722112240  
   
     Status: A 
 ------------------------------------------------------------ 
 ----------------  
  EID: 4 Location: 
SQLCA Information 
 
 sqlcaid : SQLCA     sqlcabc: 136   sqlcode: -2413   sqlerrml: 0 
 
 sqlerrmc:   
 sqlerrp : sqlubIni  
 sqlerrd : (1) 0                (2) 0                (3) 0 
 
           (4) 0                (5) 0                (6) 0  
		   
 sqlwarn : (1)      (2)      (3)      (4)        (5)       (6)  
 
           (7)      (8)      (9)      (10)       (11)  
 sqlstate: 
 
 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  
 Backup ID
  -- --- ------------------ ---- --- ------------ ------------ 
  -------------- 
   B  D  20140722112743001   F    D  S0000000.LOG S0000000.LOG   
 
 ------------------------------------------------------------ 
 ---------------- 
 Contains 4 tablespace(s): 
 
 00001 SYSCATSPACE 
 
 00002 USERSPACE1 
 
 00003 SYSTOOLSPACE 
 
 00004 TS1
  ------------------------------------------------------------- 
  ---------------- 
  Comment: DB2 BACKUP ONE OFFLINE 
  
 Start Time: 20140722112743 
 
   End Time: 20140722112743 
   
     Status: A 
 ------------------------------------------------------------- 
  ---------------- 
 EID: 5 Location: /home/db2inst1 
 
 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log 
 Backup ID   
  ------------------------------------------------------------- 
  ----------------
  
R  D  20140722114519001   F                                
20140722112743 

 ------------------------------------------------------------ 
 ----------------  
 Contains 4 tablespace(s):  
 
 00001 SYSCATSPACE 
 
  00002 USERSPACE1 
  
 00003 SYSTOOLSPACE  
 
 00004 TS1
 ------------------------------------------------------------ 
 ----------------  
Comment: RESTORE ONE WITH RF
  
 Start Time: 20140722114519 
 
   End Time: 20140722115015  
     Status: A  
	 
 ------------------------------------------------------------ 
 ----------------  
  EID: 6 Location:  

從備份恢復資料庫

要從備份檔案恢復資料庫,您需要遵循給定的語法

語法

db2 restore database <db_name> from <location> 
taken at <timestamp>    

示例

db2 restore database one from /home/db2inst1/ taken at 
20140722112743  

輸出

SQL2523W  Warning!  Restoring to an existing database that is 
different from  
 
the database on the backup image, but have matching names. 
The target database  
 
will be overwritten by the backup version.  The Roll-forward 
recovery logs

associated with the target database will be deleted.  

Do you want to continue ? (y/n) y 
 
DB20000I  The RESTORE DATABASE command completed successfully.   

向前滾動日誌目錄中所有日誌,包括磁碟驅動器故障之前的最新更改。

語法

db2 rollforward db <db_name> to end of logs and stop   

示例

db2 rollforward db one to end of logs and stop  

輸出

                                 Rollforward Status  
 Input database alias                   = one  
 Number of members have returned status = 1  
 Member ID                              = 0  
 Rollforward status                     = not pending  
 Next log file to be read               =  
 Log files processed                    = S0000000.LOG - 
 S0000001.LOG  
 Last committed transaction            = 2014-07-22- 
 06.00.33.000000 UTC  
DB20000I  The ROLLFORWARD command completed successfully. 
廣告