`
guoyanxi
  • 浏览: 276917 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

db2 恢复误删表

    博客分类:
  • db2
阅读更多
oracle如果误删了表,可以很方便的flash back
最重要的是不会影响业务
但是如果你在DB2中招,就目前来说,你就不那么走运了

--首先设置归档模式
[db2inst2@localhost ~]$ db2 update db cfg using logretain on
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification 
were not changed dynamically. For these configuration parameters, all 
applications must disconnect from this database before the changes become 
effective.
[db2inst2@localhost ~]$ db2 get db cfg | grep -i logre
 Log retain for recovery enabled             (LOGRETAIN) = RECOVERY
 First log archive method                 (LOGARCHMETH1) = LOGRETAIN


--然后理所当然的full backup一次
[db2inst2@localhost ~]$ db2 connect to sample
SQL1116N  A connection to or activation of database "SAMPLE" cannot be made 
because of BACKUP PENDING.  SQLSTATE=57019
[db2inst2@localhost ~]$ db2 backup db sample

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

--然后确认表空间是DROP_RECOVERY的
select TBSPACE, DROP_RECOVERY from SYSCAT.TABLESPACES

TBSPACE                                                                                                                          DROP_RECOVERY
-------------------------------------------------------------------------------------------------------------------------------- -------------
SYSCATSPACE                                                                                                                      N            
TEMPSPACE1                                                                                                                       N            
USERSPACE1                                                                                                                       Y            
SYSTOOLSPACE                                                                                                                     Y            

--否则可以使用以下语句打开:
ALTER TABLESPACE <tablespace-name> DROPPED TABLE RECOVERY ON

--建立测试表
--T3表是准备被模拟误drop,并进行恢复的
--T4表是模拟其他不相关的表,看恢复操作是否有影响
[db2inst2@localhost ~]$ db2 "create table T3 (C1 INT)"
DB20000I  The SQL command completed successfully.
[db2inst2@localhost ~]$ db2 "create table T4 (C1 INT)"
DB20000I  The SQL command completed successfully.
[db2inst2@localhost ~]$ db2 "insert into t3 values (333)"
DB20000I  The SQL command completed successfully.
[db2inst2@localhost ~]$ db2 "select * from t3"

C1         
-----------
        333

--开始模拟误删除
[db2inst2@localhost ~]$ db2 drop table t3
DB20000I  The SQL command completed successfully.
[db2inst2@localhost ~]$ db2 "select * from t3"
SQL0204N  "DB2INST2.T3" is an undefined name.  SQLSTATE=42704

--这时候模拟在发现前,t4表的业务继续
[db2inst2@localhost ~]$ db2 "insert into t4 values (444)"
DB20000I  The SQL command completed successfully.
[db2inst2@localhost ~]$ db2 "select * from t4"

C1         
-----------
        444

  1 record(s) selected.


--使用list history看到刚刚删除的表
[db2inst2@localhost ~]$ db2 list history dropped table all for sample

                    List History File for sample

Number of matching file entries = 1

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20110424193315                                        000000000000521200020014 
 ----------------------------------------------------------------------------
  "DB2INST2"."T3" resides in 1 tablespace(s):

  00001 USERSPACE1                                                            
 ----------------------------------------------------------------------------
    Comment: DROP TABLE                                                       
 Start Time: 20110424193315
   End Time: 20110424193315
     Status: A
 ----------------------------------------------------------------------------
  EID: 33

 DDL: CREATE TABLE "DB2INST2"."T3" ( "C1" INTEGER )  IN "USERSPACE1" ;   
 ----------------------------------------------------------------------------

--记住backup id,稍后有用   000000000000521200020014

--开始恢复
--先restore
[db2inst2@localhost ~]$ db2 restore db sample
SQL2539W  Warning!  Restoring to an existing database that is the same as the 
backup image database.  The database files will be deleted.
Do you want to continue ? (y/n) y
DB20000I  The RESTORE DATABASE command completed successfully.

--开始rollforward,关键的一步
[db2inst2@localhost ~]$ db2 rollforward db sample to end of logs and stop recover dropped table 000000000000521200020014 to /home/db2inst2

                                 Rollforward Status

 Input database alias                   = sample
 Number of nodes have returned status   = 1

 Node number                            = 0
 Rollforward status                     = not pending
 Next log file to be read               =
 Log files processed                    = S0000000.LOG - S0000003.LOG
 Last committed transaction             = 2011-04-24-11.34.03.000000 UTC

DB20000I  The ROLLFORWARD command completed successfully.

--这时候看到指定目录下多了一个以节点为名字的目录,里面有名为data的一个文件
[db2inst2@localhost ~]$ ll NODE0000/
total 4
-rw-r----- 1 db2inst2 db2iadm1 4 Apr 24 19:40 data
[db2inst2@localhost ~]$ pwd
/home/db2inst2

--开始重建表t3
--先建表结构,使用list history里面的DDL
[db2inst2@localhost ~]$ db2 connect to sample

   Database Connection Information

 Database server        = DB2/LINUX 9.7.2
 SQL authorization ID   = DB2INST2
 Local database alias   = SAMPLE

[db2inst2@localhost ~]$ db2 "CREATE TABLE "DB2INST2"."T3" ( "C1" INTEGER )  IN "USERSPACE1""
DB20000I  The SQL command completed successfully.
[db2inst2@localhost ~]$ db2
[db2inst2@localhost ~]$ db2 "select * from t3"

C1         
-----------

  0 record(s) selected.

--看到现在是没有数据的

--然后使用刚才rollforward出来的data import
[db2inst2@localhost ~]$ db2 import from /home/db2inst2/NODE0000/data of del insert into t3
SQL3109N  The utility is beginning to load data from file 
"/home/db2inst2/NODE0000/data".

SQL3110N  The utility has completed processing.  "1" rows were read from the 
input file.

SQL3221W  ...Begin COMMIT WORK. Input Record Count = "1".

SQL3222W  ...COMMIT of any database changes was successful.

SQL3149N  "1" rows were processed from the input file.  "1" rows were 
successfully inserted into the table.  "0" rows were rejected.


Number of rows read         = 1
Number of rows skipped      = 0
Number of rows inserted     = 1
Number of rows updated      = 0
Number of rows rejected     = 0
Number of rows committed    = 1

[db2inst2@localhost ~]$ db2 "select * from t3"

C1         
-----------
        333

  1 record(s) selected.

--看到数据回来了

--因为是end of logs,所以T4的数据应该也是最新的
[db2inst2@localhost ~]$ db2 "select * from t4"

C1         
-----------
        444

  1 record(s) selected.

--完成

分享到:
评论

相关推荐

    DB2恢复删除表学习

    ### DB2恢复删除表学习 #### 一、DB2中删除表恢复机制概述 在IBM DB2数据库管理系统中,为了应对误删表的情况,DB2提供了表空间级别的表恢复功能。这种功能允许用户在一定条件下恢复已被删除的表及其数据。默认...

    使用IBM DB2 Recovery Expert工具高效完成数据库恢复之对象恢复篇.docx

    【IBM DB2 Recovery Expert工具详解——对象恢复篇】 IBM DB2 Recovery Expert是一...无论是误删的表还是其他数据库对象,都能通过该工具迅速恢复,极大地降低了因人为错误导致的损失,提高了数据库的稳定性和安全性。

    深入了解IBM+DB2数据库的备份与恢复

    2. **创建数据库管理表空间**:`db2 "create regular tablespace data_sp1 pagesize 4k managed by database using('/db2test/usrdata2.dbf' 5120) BUFFERPOOL IBMDEFAULTBP";` 以上步骤仅为实验准备的一部分,完整...

    DB2数据库基础

    - **已删除表的恢复:** 在某些情况下可以恢复误删的表。 - **如何更改本地系统名称:** 修改系统配置文件或使用DB2命令。 - **在AIX上建立裸设备数据库管理(DMS)表空间:** 需要在AIX系统上进行特殊配置。 - **如何...

    db2常用命令

    - `DROP TABLE`:删除表,如`DROP TABLE IF EXISTS &lt;table_name&gt;`(IF EXISTS可防止误删)。 - `JOIN`:连接两个或更多表,以便在查询中合并数据。 7. 索引操作: - `db2 create index &lt;index_name&gt; on ...

    Windows服务器的在线保护和快速恢复

    文件恢复可以快速恢复误删文件,裸盘恢复则用于大量数据丢失后的全面恢复,而瞬时恢复能实现瞬间恢复到特定时间点,减少业务中断。 在系统严重损坏时,DiskGuard提供了Recovery CD引导恢复、SAN-boot远程启动和网卡...

    db2维护手册

    - **以脱机方式重组表**: 介绍如何使用DB2的脱机方式重组表功能,提高表性能。 - **索引重组**: 解释索引重组的方法和重要性,确保索引的有效性和性能。 - **收集和更新统计信息的准则**: 提供关于如何有效收集和...

    Win8.1怎么去掉文件夹.docx

    - **备份**:在进行任何注册表修改之前,请先备份当前注册表或创建系统还原点,以防万一出现问题时可以轻松恢复。 - **谨慎操作**:请确保只删除指定的键值,避免误删其他关键注册表项,以免造成不必要的麻烦。 ###...

    完全删除ORACLE的方法

    1. **备份注册表**:在进行任何修改之前,请务必备份注册表,以防万一出现意外情况时能够恢复。 2. **停止Oracle服务**: - 打开“服务”管理工具(可以通过运行`services.msc`命令打开),找到所有与Oracle相关的...

Global site tag (gtag.js) - Google Analytics