日期:2014-05-16  浏览次数:20463 次

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 da