1. 程式人生 > >DB2備份+日誌恢復並前滾資料庫

DB2備份+日誌恢復並前滾資料庫

任何資料庫都是這樣的,我們會做資料庫的全量備份,增量備份,並記錄日誌。如果資料庫出現意外宕機,則可以用全備+增量備份+日誌來進行資料庫恢復。
本文主要展示如何使用備份+日誌來恢復DB2的資料庫,為了更清晰一點兒,使用兩臺機器151和152;在152機器上利用備份和日誌恢復到151的資料庫的狀態。

151機器上資料操作步驟

1.建立測試庫

#1.建立測試資料庫
db2 create database yynewlog
#2.啟用歸檔日誌
db2 update db cfg for yynewlog using logarchmeth1 DISK:/data/db2data
#3.啟用增量備份 db2 update db cfg for yynewlog using TRACKMOD YES #3.解除backup pending狀態 db2 backup database yynewlog #4.建立測試表 db2 connect to yynewlog db2 "create table student (id int not null, name varchar(20), sex smallint, phone varchar(12), address varchar(30) )" #5.填充資料:id=1,id=2 db2 "insert into student values(1,'charles ',0,'19089076756','BEIJING ')"
db2 "insert into student values(2,'zhouzhou ',0,'1076758886','BEIJING ')"

2.全量備份

  使用線上備份資料庫,with include logs保留日誌,這個選項好像是預設的,但是為了語句的可讀理解,最好還是加上。

[db2inst1@db22 ~]$ db2 backup db yynewlog online to /tmp include logs

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

  /tmp目錄下有了名為YYNEWLOG.0.db2inst1.DBPART000.20171030175924.001

的全量備份,全量備份裡面是id=1,id=2的記錄。

3.增量備份

#新插入id=3,id=4的資料。
db2 "insert into student values(3,'xiaohong',0,'67583886','TAIYUAN')"
db2 "insert into student values(4,'lianliang',0,'13667583886','SHIJIAZHAUNG')"

#增量備份
[[email protected] ~]$ db2 backup db yynewlog ONLINE INCREMENTAL to /tmp include logs

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

/tmp目錄下有了名為YYNEWLOG.0.db2inst1.DBPART000.20171030180235.001的備份檔案。
全量備份+增量備份裡面是id=1,id=2,id=3,id=4的資料。

語法

>--+------------------------+----------------------------------->
   '-INCREMENTAL--+-------+-'   
                  '-DELTA-'     
  • INCREMENTAL
    Specifies a cumulative (incremental) backup image. An incremental backup image is a copy of all database data that has changed since the most recent successful, full backup operation.
    指定一個累積的(增量)備份。一個INCREMENTAL備份映象是一個 從 距離最近的成功備份的全量備份 之後 所有改變的資料的副本。
    • DELTA
      Specifies a noncumulative (delta) backup image. A delta backup image is a copy of all database data that has changed since the most recent successful backup operation of any type.
      指定一個非 累積的(delta:差值)。一個delta備份映象是一個 自動最近的一次成功的任何型別的備份操作之後 所有改變的資料庫資料的副本。

4.增量delta備份

db2 "insert into student values(5,'shuang',0,'15067583886','SHENZHEN')"
db2 "insert into student values(6,'licy',0,'15683886675','XIAMEN')"
[[email protected] ~]$ db2 backup db yynewlog ONLINE INCREMENTAL DELTA to /tmp include logs

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

5.活動日誌

db2 "insert into student values(7,'shuang',0,'15067583886','SHENZHEN')"
db2 "insert into student values(8,'licy',0,'15683886675','XIAMEN')" 

  此時資料如下:


[[email protected] ~]$ db2 "select * from student"

ID          NAME                 SEX    PHONE        ADDRESS                       
----------- -------------------- ------ ------------ ------------------------------
          1 charles                   0 19089076756  BEIJING                       
          2 zhouzhou                  0 1076758886   BEIJING                       
          3 xiaohong                  0 67583886     TAIYUAN                       
          4 lianliang                 0 13667583886  SHIJIAZHAUNG                  
          5 shuang                    0 15067583886  SHENZHEN                      
          6 licy                      0 15683886675  XIAMEN                        
          7 shuang                    0 15067583886  SHENZHEN                      
          8 licy                      0 15683886675  XIAMEN                        

  8 record(s) selected.

152機器上的恢復步驟

1.拷貝檔案到目標機器

scp /tmp/YYNEWLOG.* db2inst1@192.168.225.152:/tmp

將拷貝YYNEWLOG.0.db2inst1.DBPART000.20171030175924.001 ,
YYNEWLOG.0.db2inst1.DBPART000.20171030180235.001 ,
YYNEWLOG.0.db2inst1.DBPART000.20171030181903.001 這三個檔案。

2.備份恢復步驟:

三個時間戳
20171030175924:全備
20171030180235:增量1
20171030181903:差異增量2

#1.指定通過增量備份恢復的形式最後要恢復到 20171030181903這個是時間戳。
[[email protected] ~]$ db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030181903 
DB20000I  The RESTORE DATABASE command completed successfully.
#2.第一步恢復全備
[[email protected] ~]$ db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030175924 logtarget /data/db2data/logs
DB20000I  The RESTORE DATABASE command completed successfully.
#3.第二步恢復備份1:
[[email protected] ~]$ db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030180235 logtarget /data/db2data/logs
SQL2580W  Warning! Restoring logs to a path which contains existing log files. 
Attempting to overwrite an existing log file during restore will cause the 
restore operation to fail.
Do you want to continue ? (y/n) y
DB20000I  The RESTORE DATABASE command completed successfully.
#4.第三步恢復差異增量備份2:
[[email protected] ~]$ db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030181903 logtarget /data/db2data/logs
SQL2580W  Warning! Restoring logs to a path which contains existing log files. 
Attempting to overwrite an existing log file during restore will cause the 
restore operation to fail.
Do you want to continue ? (y/n) y
DB20000I  The RESTORE DATABASE command completed successfully.

此時,logtarget下有前滾所需的日誌:

[[email protected] logs]# ls NODE0000/LOGSTREAM0000/
S0000007.LOG  S0000010.LOG  S0000013.LOG

3.確定所需的活動日誌:

3.1 151機器上的活動日誌:

[db2inst1@db22 ~]$ db2 get db cfg for yynewlog | grep -i 'First active log'
 First active log file                                   = S0000015.LOG

3.1 152機器上的活動日誌:

[db2inst1@db22 ~]$ db2 get db cfg for yynewlog | grep -i 'First active log'
 First active log file                                   = S0000013.LOG

3.3 確定所需的日誌

所以恢復的話是需要S0000014.LOG和S0000015.LOG的,151上的第一個活動日誌是S0000015.LOG,故S0000014.LOG已經歸檔了,要把該日誌拷貝到歸檔日誌的目錄中(恢復時的overflow log path
中,在此我們指定overflow log path為/data/db2data/logs下,剛剛restore時的logtarget):

3.3.1 拷貝歸檔日誌

#先找一下歸檔日誌路徑
[root@db22 tmp]# find / -name S0000014.LOG
/db/log/db2inst1/TESTMOVE/NODE0000/LOGSTREAM0000/C0000000/S0000014.LOG
/data/bak/db2inst1/SAMPLE/NODE0000/LOGSTREAM0000/C0000001/S0000014.LOG
/data/db2data/db2inst1/SCHOOL/NODE0000/LOGSTREAM0000/C0000000/S0000014.LOG
/data/db2data/db2inst1/YYNEWLOG/NODE0000/LOGSTREAM0000/C0000000/S0000014.LOG
/home/db2inst1/db2inst1/NODE0000/SQL00003/LOGSTREAM0000/S0000014.LOG
/home/db2inst1/db2inst1/NODE0000/SQL00002/LOGSTREAM0000/S0000014.LOG

#拷貝歸檔日誌:
scp /data/db2data/db2inst1/YYNEWLOG/NODE0000/LOGSTREAM0000/C0000000/S0000014.LOG  db2inst1@192.168.225.152:/data/db2data/logs/NODE0000/LOGSTREAM0000
#overflow log path 下有該日誌了
[root@db22 logs]# ls NODE0000/LOGSTREAM0000/
S0000007.LOG  S0000010.LOG  S0000013.LOG  S0000014.LOG

3.3.2 拷貝歸檔日誌

#152機器上的活動日誌目錄
[[email protected] ~]$ db2 get db cfg for yynewlog | grep -i 'log'

 Changed path to log files                  (NEWLOGPATH) = 
 Path to log files                                       = /home/db2inst1/db2inst1/NODE0000/SQL00003/LOGSTREAM0000/
 Overflow log path                     (OVERFLOWLOGPATH) = 
 Mirror log path                         (MIRRORLOGPATH) = 

所以152機器上的活動日誌目錄是:/home/db2inst1/db2inst1/NODE0000/SQL00003/LOGSTREAM0000/

拷貝活動日誌

[[email protected] tmp]# scp -r /home/db2inst1/db2inst1/NODE0000/SQL00005/LOGSTREAM0000 [email protected]:/home/db2inst1/db2inst1/NODE0000/[email protected]'s password: 
S0000024.LOG            100% 4104KB   4.0MB/s   00:00    
S0000020.LOG            100% 4104KB   4.0MB/s   00:00    
S0000022.LOG            100% 4104KB   4.0MB/s   00:01    
S0000015.LOG            100% 4104KB   4.0MB/s   00:00    
S0000017.LOG            100% 4104KB   4.0MB/s   00:00    
S0000026.LOG            100% 4104KB   4.0MB/s   00:01    
S0000023.LOG            100% 4104KB   4.0MB/s   00:00    
S0000027.LOG            100% 4104KB   4.0MB/s   00:01    
S0000021.LOG            100% 4104KB   4.0MB/s   00:00    
S0000019.LOG            100% 4104KB   4.0MB/s   00:00    
S0000016.LOG            100% 4104KB   4.0MB/s   00:01    
S0000025.LOG            100% 4104KB   4.0MB/s   00:01    
S0000018.LOG            100% 4104KB   4.0MB/s   00:01    

4.前滾恢復

[[email protected] ~]$ db2 "rollforward db yynewlog to end of logs overflow log path (/data/db2data/logs)"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = DB  working
 Next log file to be read               = S0000016.LOG
 Log files processed                    = S0000013.LOG - S0000014.LOG
 Last committed transaction             = 2017-10-31-08.43.15.000000 Local

DB20000I  The ROLLFORWARD command completed successfully.
[[email protected] ~]$ db2 "rollforward db yynewlog complete"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = not pending
 Next log file to be read               =
 Log files processed                    = S0000013.LOG - S0000014.LOG
 Last committed transaction             = 2017-10-31-08.43.15.000000 Local

DB20000I  The ROLLFORWARD command completed successfully.

5.校驗

[db2inst1@db22 ~]$ db2 "connect to yynewlog"

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.1.1
 SQL authorization ID   = DB2INST1
 Local database alias   = YYNEWLOG

[db2inst1@db22 ~]$ db2 "select * from student"

ID          NAME                 SEX    PHONE        ADDRESS                       
----------- -------------------- ------ ------------ ------------------------------
          1 charles                   0 19089076756  BEIJING                       
          2 zhouzhou                  0 1076758886   BEIJING                       
          3 xiaohong                  0 67583886     TAIYUAN                       
          4 lianliang                 0 13667583886  SHIJIAZHAUNG                  
          5 shuang                    0 15067583886  SHENZHEN                      
          6 licy                      0 15683886675  XIAMEN                        
          7 shuang                    0 15067583886  SHENZHEN                      
          8 licy                      0 15683886675  XIAMEN                        

6.成功

7.疑問解析

7.1 Log files processed = S0000013.LOG - S0000014.LOG,沒有15.LOG;是否並不需要活動日誌?

前滾恢復返回:

 Next log file to be read               = S0000016.LOG
 Log files processed                    = S0000013.LOG - S0000014.LOG

不拷貝活動日誌的情況下來試試恢復會如何

db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030181903 
db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030175924 
db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030180235 
db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030181903 logtarget /data/db2data/logs
#拷貝14.LOG日
[[email protected] logs]# ls NODE0000/LOGSTREAM0000/
S0000013.LOG  S0000014.LOG
[[email protected] logs]# ls /home/db2inst1/db2inst1/NODE0000/SQL00003/LOGSTREAM0000/
[[email protected] logs]# 

#前滾

[[email protected] ~]$ db2 "rollforward db yynewlog to end of logs overflow log path (/data/db2data/logs)"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = DB  working
 Next log file to be read               = S0000015.LOG
 Log files processed                    = S0000013.LOG - S0000013.LOG
 Last committed transaction             = 2017-10-30-10.19.11.000000 UTC

DB20000I  The ROLLFORWARD command completed successfully.
[[email protected] ~]$ db2 "rollforward db yynewlog complete"
SQL1273N  An operation reading the logs on database "YYNEWLOG" cannot continue 
because of a missing log file "S0000014.LOG" on database partition "0" and log 
stream "0".

由上面的實驗可以看到我們溢位目錄中是有14.LOG檔案的,但是卻報找不到日誌檔案的錯誤

將活動日誌拷貝過來之後:

[[email protected] ~]$ db2 "rollforward db yynewlog complete"
SQL1273N  An operation reading the logs on database "YYNEWLOG" cannot continue 
because of a missing log file "S0000014.LOG" on database partition "0" and log 
stream "0".
[[email protected] ~]$ db2 "rollforward db yynewlog to end of logs overflow log path (/data/db2data/logs)"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = DB  working
 Next log file to be read               = S0000016.LOG
 Log files processed                    = S0000013.LOG - S0000014.LOG
 Last committed transaction             = 2017-10-31-02.03.51.000000 UTC

DB20000I  The ROLLFORWARD command completed successfully.
[[email protected] ~]$ db2 "rollforward db yynewlog complete"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = not pending
 Next log file to be read               =
 Log files processed                    = S0000013.LOG - S0000014.LOG
 Last committed transaction             = 2017-10-31-02.03.51.000000 UTC

DB20000I  The ROLLFORWARD command completed successfully.

  將活動日誌拷貝過來之後再rollforward就可以成功了。

7.2 我只想恢復id =7 的資料,不想要id=8的資料怎麼辦?重點怎麼確定時間戳?

相關推薦

DB2備份+日誌恢復資料庫

任何資料庫都是這樣的,我們會做資料庫的全量備份,增量備份,並記錄日誌。如果資料庫出現意外宕機,則可以用全備+增量備份+日誌來進行資料庫恢復。 本文主要展示如何使用備份+日誌來恢復DB2的資料庫,為了更清晰一點兒,使用兩臺機器151和152;在152機

一步一步告訴你DB2關於指定時間點恢復方法

(1)備份資料庫 [[email protected] ~ 22]$db2 backup db sample online include logs Backup successful. The timestamp for this backup image

DB2關於指定時間點恢復方法

其中,End Time: 20051223120902為備份的結束時間,同時它也是前滾時的最小恢復時間點(PIT),也就是說,如果按照指定時間點恢復,最小要恢復到這個時間點,如果用提前於這個時間的時間來前滾,將會返回資訊提示:[[email protected] ~ 31]$db2 ”rollfor

關於oracle實例恢復和回的理解

關於oracle實例恢復的前滾和回滾的理解關於oracle實例恢復的一些理解,一直都有誤區,今天通過查看相關資料和與同學探討,發覺了自己的錯誤,探討結果如下:實例恢復:當數據庫非正常關閉的時候(斷電或者shu abort等等非一致性關閉),當你從新啟動數據庫的時候,數據庫相關進程自動進行實例恢復,無須人工幹

用AOP記錄操作日誌寫進資料庫

先用AOP註解 1 package com.vlandc.oss.apigate.log.aspect; 2 3 import java.util.Map; 4 import java.util.Optional; 5 6 import javax.servlet.http.H

如何在Ubuntu 14.04上備份恢復及遷移MongoDB資料庫

MongoDB可謂目前人氣最高的NoSQL資料庫引擎之一。其憑藉著出色的可擴充套件性、可靠性與易用性徵服了無數使用者。今天,我們將共同探討如何備份、恢復及遷移MogoDB資料庫。 對資料庫進行匯入與匯出意味著需要以人類可讀的格式處理資料,且確保其與其它軟體

D_db2重定向恢復+日誌恢復誤刪除的資料

UPDATE COMMAND OPTIONS USING S ON Z ON HIS_NODE0000.out V ON; SET CLIENT ATTACH_DBPARTITIONNUM  0; SET CLIENT CONNECT_DBPARTITIONNUM 0; RESTORE DATABASE my

DB2資料庫歸檔模式下線上備份恢復

以下方法使用線上備份方式並且恢復時無須手動複製日誌檔案。 前提:          修改資料庫引數,使之開啟歸檔日誌,支援線上備份。 備份命令:            db2 backup db dbname online to 【路徑名】compress includ

MySQL資料庫之全量+增量+二進位制日誌備份恢復

一、簡介資料的備份與恢復 1、為什麼備份? 災難恢復:人為錯誤、硬體故障(冗餘)、軟體故障(bug)、自然災害、黑客攻擊、誤操作、…; 測試; 2、備份時應該注意些什麼? 能容忍最多丟失多少資料; 恢復資料需要在多長時間內完成; 需要恢復哪些資料;

db2資料庫備份恢復!!! (以window平臺為例)

       1. 建立好新的資料庫後,首先做一個離線的全備。. E:/Program Files/IBM/SQLLIB/BIN>;db2 connect to sample    #聯接到資料庫。   

DB2資料庫備份恢復

資料庫備份(離線): C:\Documents and Settings\quanlun>db2 backup db sample to 'E:\bk\test' 備份成功。此備份映像的時間戳記是:20101207095857 C:\Documents and Set

96.創建普通用戶授權,常用SQL語句,MySQL數據庫備份恢復

創建普通用戶並授權常用SQL語句 MySQL數據庫備份與恢復 一、創建普通用戶並授權 1、創建用戶並授權 [root@sdwaqw ~]# mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ;

db2數據庫備份恢復

ase into div tar 目錄 img OS clas alt 備份 先停掉Tomcat,然後在機器A上執行以下語句: db2stop force db2start db2 force applications all db2 backup database p

MySQL 資料庫增量備份恢復資料命令實戰

1. 備份單個數據庫練習 mysqldump 命令多種引數的使用 1.1 調整 MySQL 客戶端及服務端字符集為建庫建表時預設的 latin1,避免備份時的亂碼問題 [[email protected] ~]# vi /etc/my.cnf [[email protected] ~

詳解如何通過Mysql的二進位制日誌恢復資料庫資料

經常有網站管理員因為各種原因和操作,導致網站資料誤刪,而且又沒有做網站備份,結果不知所措,甚至給網站運營和盈利帶來負面影響。所以本文我們將和大家一起分享學習下如何通過Mysql的二機制日誌(binlog)來恢復資料。 系統環境: 作業系統:CentOS 6.5 X64  (虛擬機器

SqlServer資料庫備份恢復措施

一、備份資料庫 1、開啟SQL企業管理器,在控制檯根目錄中依次點開Microsoft SQL Server2、SQL Server組-->雙擊開啟你的伺服器-->雙擊開啟資料庫目錄3、選擇你的資料庫名稱(如論壇資料庫Forum)-->然後點上面選單中的工具-->選擇備份資料庫

mysql 操作資料庫備份恢復

一、直接把建立資料庫的語句放到sql 檔案中: php 寫法: <?php    $mysql_port = get_mysql_port();   $cmd = US_MYSQL_BIN."/mysql.exe --port=".

PostgreSQL利用全備與WAL日誌恢復資料庫

文章目錄 基礎備份——全備 使用pg_basebackup 引數 WAL日誌的的備份 測試流程 將被資料檔案全備 繼續操作主庫 配置

influxd資料庫備份恢復

先上influxdb的配置檔案: 其中主要更改了預設的元資料和資料庫資料的儲存位置,更改了8088埠的繫結地址 ### Welcome to the InfluxDB configuration file. # The values in this file override the

Oracle資料庫備份恢復 - 增量備份

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!