现在的位置: 首页 > 综合 > 正文

如何恢复DB2中误删除表的数据

2018年04月26日 ⁄ 综合 ⁄ 共 5355字 ⁄ 字号 评论关闭

如何恢复DB2中误删除表的数据

1. 首先数据库要可以前滚恢复(数据库配置参数logretain或userexit打开)。
db2 connect to dbname
db2 update db cfg using logretain on
db2 backup db dbname (当打开归档日志后,该数据库会处于rollforward pending状态,所以要做一次全备份)

2. 对要实施Drop Table Recovery的表空间(限regular tablespace),执行:
db2 "alter tablespace 表空间名称 dropped table recovery on "

3. 用 list history dropped table all for 数据库名 得到删除表的tableid(例如 0000000000006d0000020003)和表结构的生成语句(DDL),记录tableid和该语句以便恢复。之后,用drop命令删除的表 中的数据可以在前滚恢复时导出。
db2 list history dropped table all for dbname

4. 恢复数据库后,如果想恢复已删除的表,在前滚时加recover dropped table tableid to 目标目录 。在该目录下被删除的表中的数据导出。利用上面提到表结构生成语句生成被删除了的表,然后用import命令将数据导入表中。
db2 restore db dbname tablespace(tablespacename) without rolling forward without prompting
db2 "rollforward db dbname to end of logs and stop tablespace(tablespacename) recover dropped table table id to path "
cd path
利用 db2 list history dropped table all for dbname 查出的表结构生成语句生成被删除了的表
db2 import from data of del insert into tablename

附一个具体的恢复步骤(以sample库为例):
D:sample>db2 alter tablespace userspace1 dropped table recovery on
DB20000I SQL 命令成功完成。

D:sample>db2 commit
DB20000I SQL 命令成功完成。

D:sample>db2 list history dropped table all for sample

列示 sample 的历史文件

匹配的文件条目数 = 1

Op 对象 时间戳记+序列 类型 设备 最早日志 当前日志 备份标识
- — ----------------- -- — ---------- ---------- -------------
D T 20061204003949 000000000000b70000
020002
----------------------------------------------------------------------------
"HEAVEN "."ORG" 驻留在 1 表空间中:

00001 USERSPACE1
----------------------------------------------------------------------------
注释:DROP TABLE
开始时间:20061204003949
结束时间:20061204003949
状态:A
----------------------------------------------------------------------------
EID:2

DDL: CREATE TABLE "HEAVEN "."ORG" ( "DEPTNUMB" SMALLINT NOT NULL , "DEPTNAME"
VARCHAR(14) , "MANAGER" SMALLINT , "DIVISION" VARCHAR(10) , "LOCATION" VARCHAR(1
3) ) IN "USERSPACE1" ;
----------------------------------------------------------------------------
D:sample>db2 restore db sample tablespace(userspace1) without rolling forward w
ithout prompting
DB20000I RESTORE DATABASE 命令成功完成。
D:sample>db2 "rollforward db sample to end of logs and stop tablespace(userspac
e1) recover dropped table 000000000000b70000020002 to C: "

前滚状态

输入数据库别名 = sample
节点数已返回状态 = 1

节点号 = 0
前滚状态 = 未暂挂
下一个要读取的日志文件 =
已处理的日志文件 = -
上次落实的事务 = 1970-01-01-00.00.00.000000

DB20000I ROLLFORWARD 命令成功完成。

D:sample>db2 connect to sample

数据库连接信息

数据库服务器 = DB2/NT 8.2.5
SQL 授权标识 = HEAVEN
本地数据库别名 = SAMPLE

D:sample>db2 select * from sales
SQL0204N "HEAVEN.SALES" 是未定义的名称。 SQLSTATE=42704

D:sample>db2 list history dropped table all for sample

列示 sample 的历史文件

匹配的文件条目数 = 2

Op 对象 时间戳记+序列 类型 设备 最早日志 当前日志 备份标识
- — ----------------- -- — ---------- ---------- -------------
D T 20061204003949 000000000000b700
020002
----------------------------------------------------------------------------
"HEAVEN "."ORG" 驻留在 1 表空间中:

00001 USERSPACE1
----------------------------------------------------------------------------
注释:DROP TABLE
开始时间:20061204003949
结束时间:20061204003949
状态:A
----------------------------------------------------------------------------
EID:2

DDL: CREATE TABLE "HEAVEN "."ORG" ( "DEPTNUMB" SMALLINT NOT NULL , "DEPTNAME
VARCHAR(14) , "MANAGER" SMALLINT , "DIVISION" VARCHAR(10) , "LOCATION" VARCHAR
3) ) IN "USERSPACE1" ;
----------------------------------------------------------------------------

Op 对象 时间戳记+序列 类型 设备 最早日志 当前日志 备份标识
- — ----------------- -- — ---------- ---------- -------------
D T 20061204004618 000000000000d500
02000b
----------------------------------------------------------------------------
"HEAVEN "."SALES" 驻留在 1 表空间中:

00001 USERSPACE1
----------------------------------------------------------------------------
注释:DROP TABLE
开始时间:20061204004618
结束时间:20061204004618
状态:A
----------------------------------------------------------------------------
EID:6

DDL: CREATE TABLE "HEAVEN "."SALES" ( "SALES_DATE" DATE , "SALES_PERSON" VAR
AR(15) , "REGION" VARCHAR(15) , "SALES" INTEGER ) IN "USERSPACE1" ;
----------------------------------------------------------------------------

D:sample>c:

C:>db2 "CREATE TABLE "HEAVEN "."SALES" ( "SALES_DATE" DATE , "SALES_PERSON" VARAR(15) , "REGION" VARCHAR(15) , "SALES" INTEGER ) IN "USERSPACE1""
CREATE TABLE "HEAVEN "."SALES" ( "SALES_DATE" DATE , "SALES_PERSON" VARCHAR(1
, "REGION" VARCHAR(15) , "SALES" INTEGER ) IN "USERSPACE1"
DB20000I SQL 命令成功完成。

C:>cd NODE0000

C:NODE0000>db2 import from data of del insert into sales
SQL3109N 实用程序开始装入文件 "data" 中的数据。

SQL3110N 实用程序已完成处理。从输入文件读取了 "41" 行。

SQL3221W ...开始 COMMIT WORK。输入记录计数 = "41"。

SQL3222W ...COMMIT 任何数据库更改成功。

SQL3149N 从输入文件中处理了 "41" 行。将 "41" 行成功插入到表中。"0" 行被拒绝。

读取行数 = 41
跳过行数 = 0
插入行数 = 41
更新行数 = 0
拒绝行数 = 0
落实行数 = 41

C:NODE0000>db2 select * from sales

SALES_DATE SALES_PERSON REGION SALES
--------- ------------- ------------- ----------
1995-12-31 LUCCHESSI Ontario-South 1
1995-12-31 LEE Ontario-South 3
1995-12-31 LEE Quebec 1
1995-12-31 LEE Manitoba 2
1995-12-31 GOUNOT Quebec 1
1996-03-29 LUCCHESSI Ontario-South 3
1996-03-29 LUCCHESSI Quebec 1
1996-03-29 LEE Ontario-South 2
1996-03-29 LEE Ontario-North 2
1996-03-29 LEE Quebec 3
1996-03-29 LEE Manitoba 5
1996-03-29 GOUNOT Ontario-South 3
1996-03-29 GOUNOT Quebec 1
1996-03-29 GOUNOT Manitoba 7
1996-03-30 LUCCHESSI Ontario-South 1
1996-03-30 LUCCHESSI Quebec 2
1996-03-30 LUCCHESSI Manitoba 1
1996-03-30 LEE Ontario-South 7
1996-03-30 LEE Ontario-North 3
1996-03-30 LEE Quebec 7
1996-03-30 LEE Manitoba 4
1996-03-30 GOUNOT Ontario-South 2
1996-03-30 GOUNOT Quebec 18
1996-03-30 GOUNOT Manitoba 1
1996-03-31 LUCCHESSI Manitoba 1
1996-03-31 LEE Ontario-South 14
1996-03-31 LEE Ontario-North 3
1996-03-31 LEE Quebec 7
1996-03-31 LEE Manitoba 3
1996-03-31 GOUNOT Ontario-South 2
1996-03-31 GOUNOT Quebec 1
1996-04-01 LUCCHESSI Ontario-South 3
1996-04-01 LUCCHESSI Manitoba 1
1996-04-01 LEE Ontario-South 8
1996-04-01 LEE Ontario-North -
1996-04-01 LEE Quebec 8
1996-04-01 LEE Manitoba 9
1996-04-01 GOUNOT Ontario-South 3
1996-04-01 GOUNOT Ontario-North 1
1996-04-01 GOUNOT Quebec 3
1996-04-01 GOUNOT Manitoba 7

41 条记录已选择。

抱歉!评论已关闭.