DB2 备份 恢复 详细测试
本文测试环境:Windows XP、IBM DB2 Express V9.5、 DB2 Quest Center V9.5 在这里举个应用场景以说明数据库恢复备份的思路: 那么假设我们周一晚上做了一个数据库的备份,周二中午 12 点数据库存储介质出现了故障。如何能够实现恢复呢 ? 那么首先是把数据库
本文测试环境:Windows XP、IBM DB2 Express V9.5、 DB2 Quest Center V9.5
在这里举个应用场景以说明数据库恢复备份的思路:
那么假设我们周一晚上做了一个数据库的备份,周二中午12
点数据库存储介质出现了故障。如何能够实现恢复呢
?
那么首先是把数据库恢复到我们备份的那个时刻(周一晚上),但是备份之后和周二
12
点之间对数据库已经做的交易(事物)怎么办呢?这就需要用到数据库日志,因为一旦交易提交,我们对数据库做的
SQL
(
insert
、
update
、
delete
等)都会记录到数据库日志中。所以我们就用数据库日志(前提是数据库日志没有受到损坏)把备份之后和数据库崩溃之前的所有
SQL
操作重做(
redo
)一遍,。这就是数据库备份恢复的原理。
1、DB2数据备份和恢复实验准备工作
Step1:创建测试数据库TestDB
1
CREATE
DATABASE
TestDB
2
ON
'
E:
'
3
USING CODESET GBK TERRITORY CN
4
WITH
'
DEMO TestDB
'
;
结果:
*** SCRIPT START: Connection: LENOVO-MILO-DB2-TOOLSDB (db2admin) Jul-09-2009 14:34:49 ***
CREATE DATABASE TestDB
ON 'E:'
USING CODESET GBK TERRITORY CN
WITH 'DEMO TestDB';
completed successfully.
DB20000I CREATE DATABASE命令成功完成。
Statement processed successfully in 32.42 secs.
*** SCRIPT END : Connection: NONE Jul-09-2009 14:35:21 ***
Step2:创建数据库管理表区间Data_SP(要先设置好路径和文件)
此处的路径为:E:/DB2_Train/TestDB/UserData
文件名设置为:TestDB.UserData
1
CREATE
REGULAR TABLESPACE Data_SP
2
3
PAGESIZE 4K
4
5
MANAGED
BY
DATABASE
6
7
USING(
FILE
'
E:/DB2_Train/TestDB/UserData/TestDB.UserData
'
2560
)
8
9
BUFFERPOOL IBMDEFAULTBP;
这里设置了初始化为4K * 2560 = 10MB的表空间
结果:TableSpace里面多了一个名为Data_SP表空间
Step3:创建测试表TestTable
1
CREATE
TABLE
TestTable
2
(
3
ID
INTEGER
NOT
NULL
GENERATED ALWAYS
AS
IDENTITY
(START
WITH
0
, INCREMENT
BY
1
, NO CACHE),
4
Message
VARCHAR
(
100
),
5
PRIMARY
KEY
(ID)
6
)
IN
Data_SP;
结果:
:
Step4:在TestTable下面插入测试数据
1
INSERT
INTO
TestTable(Message)
2
VALUES
(
'
测试表建立成功
'
)
--说明:建立测试表并插入数据是为了稍后验证数据库恢复的时候用的--
结果:
或者可以使用
1
db2stop force
2
db2start
3
db2 connect
to
TestDB
4
db2
select
*
from
TestTable
Step5:DB2数据库脱机备份和恢复实验
说明:我将脱机备份的文件放在了以下路径中:E:/DB2_Train/Offline
1、完全脱机备份数据库
1
db2stop force
//
停止及启动DB2实例
2
db2start
3
4
db2 connect
to
TestDB
5
//
指定当前活动的数据库为TestDB
6
7
db2
backup
db TestDB
to
"E:/DB2_Train/Offline"
8
//
完全备份数据库,(脱机,备份时间戳为20090709152004)
9
10
2、模拟灾难现场,强制删除TestDB数据库
1
db2
drop
db TestDB
3、根据数据库完全备份恢复数据库
1
//
利用离线完全备份恢复数据库
2
db2
restore
db TestDB
from
"E:/DB2_Train/Offline" taken at
20090709152004
4、查询表TestTable里面内容,这一块要重点关注,因为它涉及三种备份方式的区别
1
db2stop force
2
db2start
3
4
db2 connect
to
TestDB
5
6
db2
select
*
from
TestTable
Step6、DB2数据库增量备份和恢复实验
1、查看数据库配置文件
1
//
查询数据库配置文件里的参数TRACKMOD的状态,默认为OFF
2
db2 get db cfg
for
TestDB
2、修改数据库配置参数TRACKMOD,使之数据库进行增量备份
1
//
设置参数TRACKMOD为YES,使之数据库可以进行增量备份
2
db2
update
db cfg
for
TestDB USING TRACKMOD YES
3、更改参数后,必须需要完全离线全备份数据库
1
//
修改参数后需要全备份数据库,脱机,时间戳为20090709163256
2
db2
backup
db TestDB
to
"E:/DB2_Train/Online"
3、查看TestTable表的内容
4、插入测试数据
这里我要说明一下为什么要在表这里插入测试据?
状态 | TestTable表里MESSAGE的值 | 备注 |
全备份 | 测试表建立成功 | |
插入数据 |
测试表建立成功 开始增量数据库备份测试 |
|
增量备份 |
测试表建立成功 开始增量数据库备份测试 |
|
还原全备份 |
测试表建立成功 |
|
还原增量备份 |
测试表建立成功 开始增量数据库备份测试 |
1
--
插入数据 开始增量数据库备份测试--
2
INSERT
INTO
TestTable(Message)
3
VALUES
(
'
开始增量数据库备份测试
'
)
5、开始增量备份(脱机备份)
1
//
增量备份数据库,脱机,时间戳为20090709170956
2
db2
backup
db TestDB incremental
to
"E:/DB2_Train/Online"
6、模拟灾难,强制删除数据库
1
//
强制删除数据库,模拟灾难现场
2
db2
drop
db TestDB
7、恢复数据库
首先还原至完全离线备份状态,然后还原至增量离线备份状态
7.1、还原至完全离线备份状态
1
//
还原离线完全备份数据库,脱机,时间戳为20090709163256
2
db2
restore
db TestDB
from
"E:/DB2_Train/Online" taken at
20090709163256
这里要注意还原完全备份后TestTable表里Message的值,也是为什么要加入测试数据的原因
7.2、还原至增量离线备份状态
1
//
--
注意:下述语句中,有一个 automatic ,它表示无论有多少个增量备份,系统将全自动检索恢复数据库的顺序并自动恢复数据库。如果没有 automatic ,则需要多次手动恢复数据库,很麻烦而且容易出错
2
db2
restore
db TestDB incremental automatic
from
"E:/DB2_Train/Online" taken at
20090709170956
3
//
然后提示想继续吗y
OR
n,然后点击y进行恢复
4
Step7、DB2联机备份和恢复实验
联
机备份数据库可以使数据库在备份的同时仍然保持在可用状态。要让数据库支持联机备份,必须更改数据库的日志归档方式。在脱机备份模式下,数据库采用循环日
志方式记录数据库日志,在联机备份模式下,数据库则采用归档日志的方式备份数据库日志。另外,对于联机备份的数据库来说,
活动日志和归档日志就很重要了,一定要经常备份、保存,像银行多数使用的是归档日志的方式,因为银行的数据关乎到民生的生活,哪怕是突然间断电了,数据库崩溃了,但是每一笔的银行交易都需要时时刻刻把交易记录保存下来,所以这里的联机备份模式就很适合这类的应用场景
1、实验开始之前,最好先重新启动数据库实例,并且连接到需要做联机备份的数据库,此例数据库为TestDB
1
db2stop force
2
db2start
3
db2 connect
to
TestDB
2、开始插入测试数据,此处为“开始联机备份恢复的测试”
1
--
开始联机备份恢复测试--
2
INSERT
INTO
TestTable(Message)
3
VALUES
(
'
开始联机备份恢复测试
'
)
3、修改数据库配置文件参数,以启动联机备份模式
1
//
查询数据库配置文件
2
db2 get db cfg
for
TestDB
主要测试的参数为
参数名 | 修改前 | 修改后 |
TRACKMOD | ON | ON |
MIRRORLOGPATH | NULL | E:/DB2_Train/Logs |
LOGRETAIN | OFF | ON |
USEREXIT | OFF | ON |
1
//
参数TRACKMOD设置为ON
2
//
参
数MIRRORLOGPATH设置为E:/DB2_Train/Logs,还有一点需要补充的是,当删除数据库的时候,它会根据数据库配置文件来自动搜寻
相关的文件,因此这个文件夹下面的数据库日志文件同样也会被删除,所以需要将它复制一份放到别的路径下,此例放在了E:/DB2_Train
/Logs_Backup,一定要注意另外保存归档好这些数据库日志文件
3
//
参数LOGRETAIN设置为ON
4
//
参数USEREXIT设置为ON
5
db2
update
db cfg
for
TestDB USING TRACKMOD
ON
MIRRORLOGPATH E:/DB2_Train/Logs LOGRETAIN
ON
USEREXIT
ON
6
7
//
更新配置文件参数好,最好重新启动一下数据库,以使参数设置成功
8
db2stop force
9
db2start
10
db2 connect
to
TestDB
11
12
//
每当更改为联机备份模式的时候,系统会要求你强制离线完全备份一次数据库
4、执行离线完全备份(脱机,时间戳为20090710113202)
1
//
执行联机模式备份之前必须执行离线完全备份一次,否则数据库处于备份暂挂的不可用状态
2
db2
backup
db TestDB
to
"E:/DB2_Train/Online"
5、插入测试数据
1
--
开始联机备份恢复测试--开始增量备份--
2
INSERT
INTO
TestTable(Message)
3
VALUES
(
'
开始联机备份恢复测试--开始增量备份
'
)
6、执行联机在线增量备份,模拟应用在线(联机增量备份,时间戳为20090710115930)
1
//
这里不需要重新断开应用程序连接,就可在线增量备份
2
db2
backup
db TestDB online incremental
to
"E:/DB2_Train/Online"
7、插入测试数据
这里模拟发生了一个应用场景,假设我早上11点钟做了离线完全备份,12点钟做了在线增量备份,但是1点钟突然间发生了不可预见的灾难性事故,数据库崩溃了,在12点钟备份后到1点钟这个时间段我做了一个插入数据“开始联机备份恢复测试-数据库日志”
1
--
开始联机备份恢复测试-数据库日志--
2
INSERT
INTO
TestTable(Message)
3
VALUES
(
'
开始联机备份恢复测试-数据库日志
'
)
这里列出所有备份所对应的TestTable表里Message字段的值
名称 |
ID |
TestTable表里Message字段的值 |
离线完全备份 |
0 测试表建立成功 |
|
在线增量备份 |
0 测试表建立成功 |
|
数据库日志 |
0 测试表建立成功 |
8、保存归档好数据库日志文件
系统默认的数据库日志文件存放位置,此路径由参数MIRRORLOGPATH设置,这里为E:/DB2_Train/Logs
1
//
一定要注意将数据库日志文件复制一份放置到另一个路径上,此处为E:/DB2_Train/Logs_Backup,并且把它归档好,归档的方法下面所述
说
明:Windows操作系统下的数据库备份文件是嵌套在一系列文件夹之下的特殊结构。上例中,D:/DB2_Train
是指备份目录,TESTDB.0是指数据库名称为 TESTDB,DB2 是指实例名称,NODE0000 是指节点名称,CATN0000
是指编目名称,20070801是指备份发生的年月日,形如YYYYMMDD,181241是指备份发生的时间,精确到秒,也就是指 18 点 12
分 41秒,形如HHMMSS,最后的 001 则是备份文件的一个序列号。
9、本例数据库日志归档整理后的文件组织结构
1
//
日志文件最好和数据库备份文件对应起来,这样子到时候比较好查询
10、模拟灾难现场,强制删除数据库
1
//
删除数据库之前,最好先重启一下DB2实例,以断开应用程序和数据库的连接
2
db2stop force
3
db2start
4
//
强制删除数据库
5
db2
drop
db TestDB
11、根据离线完全备份恢复数据库
1
//
离线完全备份恢复数据库
2
db2
restore
db TestDB
from
"E:/DB2_Train/Online" taken at
20090710113202
12、根据在线增量备份恢复数据库
1
//
在线增量备份恢复数据库,记得加上automatic,系统会自动寻找相关的增量备份文件
2
db2
restore
db TestDB incremental automatic
from
"E:/DB2_Train/Online" taken at
20090710115930
3
//
然后根据系统提示,输入y确认恢复
13、前滚恢复数据库,并指定归档日志位置
1
//
前滚恢复数据库,最大程度的保留了数据
2
db2 ROLLFORWARD
DATABASE
TestDB
TO
END
OF
LOGS
AND
COMPLETE OVERFLOW
LOG
PATH("E:/DB2_Train/Logs_Backup/TestDB.
0
/DB2/NODE0000/CATN0000/
20090710
/
115930.001
")
14、查看数据完整性
1
//
查询TestTable表的内容,检查数据的完整性
2
db2 "
select
*
from
TestTable"
15、总结及展望
这
个教程花了我两天的时间来整理和撰写,但是还是感觉到对文章组织结构、逻辑推理方面还远远不够,这篇教程的用意是为了让自己更加深刻的理解DB2数据库的
各种备份和恢复类型,也作为一个知识字典,下次忘记语句了,还可以过来查找,希望自己更加的努力学习DB2数据库,它的魅力实在太大了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Open WeChat, select Settings in Me, select General and then select Storage Space, select Management in Storage Space, select the conversation in which you want to restore files and select the exclamation mark icon. Tutorial Applicable Model: iPhone13 System: iOS15.3 Version: WeChat 8.0.24 Analysis 1 First open WeChat and click the Settings option on the My page. 2 Then find and click General Options on the settings page. 3Then click Storage Space on the general page. 4 Next, click Manage on the storage space page. 5Finally, select the conversation in which you want to recover files and click the exclamation mark icon on the right. Supplement: WeChat files generally expire in a few days. If the file received by WeChat has not been clicked, the WeChat system will clear it after 72 hours. If the WeChat file has been viewed,

Windows Recovery Environment (WinRE) is an environment used to repair Windows operating system errors. After entering WinRE, you can perform system restore, factory reset, uninstall updates, etc. If you are unable to boot into WinRE, this article will guide you through fixes to resolve the issue. Unable to boot into the Windows Recovery Environment If you cannot boot into the Windows Recovery Environment, use the fixes provided below: Check the status of the Windows Recovery Environment Use other methods to enter the Windows Recovery Environment Did you accidentally delete the Windows Recovery Partition? Perform an in-place upgrade or clean installation of Windows below, we have explained all these fixes in detail. 1] Check Wi

On Douyin, a short video platform full of creativity and vitality, we can not only enjoy a variety of exciting content, but also have in-depth communications with like-minded friends. Among them, chat sparks are an important indicator of the intensity of interaction between the two parties, and they often inadvertently ignite the emotional bonds between us and our friends. However, sometimes due to some reasons, the chat spark may be disconnected. So what should we do if we want to restore the chat spark? This tutorial guide will bring you a detailed introduction to the content strategy, hoping to help everyone. How to restore the spark of Douyin chat? 1. Open the Douyin message page and select a friend to chat. 2. Send messages and chat to each other. 3. If you send messages continuously for 3 days, you can get the spark logo. On a 3-day basis, send pictures or videos to each other

How to restore Xiaomi Cloud Photo Album to local? You can restore Xiaomi Cloud Photo Album to local in Xiaomi Cloud Photo Album APP, but most friends don’t know how to restore Xiaomi Cloud Photo Album to local. The next step is to restore Xiaomi Cloud Photo Album to local. Local method graphic tutorials, interested users come and take a look! How to restore Xiaomi cloud photo album to local 1. First open the settings function in Xiaomi phone and select [Personal Avatar] on the main interface; 2. Then enter the Xiaomi account interface and click the [Cloud Service] function; 3. Then jump to Xiaomi For the function of cloud service, select [Cloud Backup]; 4. Finally, in the interface as shown below, click [Cloud Album] to restore the album to local.

What do you think of furmark? 1. Set the "Run Mode" and "Display Mode" in the main interface, and also adjust the "Test Mode" and click the "Start" button. 2. After waiting for a while, you will see the test results, including various parameters of the graphics card. How is furmark qualified? 1. Use a furmark baking machine and check the results for about half an hour. It basically hovers around 85 degrees, with a peak value of 87 degrees and room temperature of 19 degrees. Large chassis, 5 chassis fan ports, two on the front, two on the top, and one on the rear, but only one fan is installed. All accessories are not overclocked. 2. Under normal circumstances, the normal temperature of the graphics card should be between "30-85℃". 3. Even in summer when the ambient temperature is too high, the normal temperature is "50-85℃

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

Title: How to restore the hosts file after deletion Summary: The hosts file is a very important file in the operating system and is used to map domain names to IP addresses. If you accidentally delete the hosts file, you may be unable to access certain websites or have other network problems. This article will introduce how to recover accidentally deleted hosts file in Windows and Mac operating systems. Text: 1. Restore hosts file in Windows operating system. Hosts file in Windows operating system

Emmo Diary is a software specially designed for recording your mood. It provides you with a private diary space, allowing you to record important or trivial things every day. Through unique emotion recognition technology, Emmo Diary can also help you better understand and deal with your emotions. But sometimes I find that my diary has been deleted by mistake and I don’t know how to restore it. So this tutorial guide will bring you a detailed recovery guide, hoping to help everyone in need. How can emmo retrieve his previous diary? 1. Click the [Settings] icon in the lower left corner of the emmo selection screen to enter; 2. Select the [Data Backup and Restore] icon on the screen and enter the operation.
