Showing posts with label db2 table recovery. Show all posts
Showing posts with label db2 table recovery. Show all posts

Thursday, August 21, 2014

db2 luw - table recovery

     This is regarding how to recover the dropped table from tablespace backup. The following are the steps to recover it.
  • Restore the tablespace
  • Rollforward the tablespace to end of logs with recover table option. This will create an export file of type DEL.
  • With the export we can recreate the table again.
Please find the example

###### backup already taken

### test case droping table
bcuaix@db_host1:/db2home/bcuaix> db2 connect to test

   Database Connection Information

 Database server        = DB2/AIX64 10.1.3
 SQL authorization ID   = BCUAIX
 Local database alias   = TEST

bcuaix@db_host1:/db2home/bcuaix> db2 "select count(*) from bkp_test.emp"

1
-----------
    2097152

  1 record(s) selected.

bcuaix@db_host1:/db2home/bcuaix> db2 "drop table bkp_test.emp"
DB20000I  The SQL command completed successfully.
bcuaix@db_host1:/db2home/bcuaix> db2 "select count(*) from bkp_test.emp"
SQL0204N  "BKP_TEST.EMP" is an undefined name.  SQLSTATE=42704

## table got dropped in the previous step. Starting to restoring the table

bcuaix@db_host1:/db2home/bcuaix> db2 force application all
DB20000I  The FORCE APPLICATION command completed successfully.
DB21024I  This command is asynchronous and may not be effective immediately.

bcuaix@db_host1:/db2home/bcuaix> db2 terminate
DB20000I  The TERMINATE command completed successfully.

bcuaix@db_host1:/db2home/bcuaix> db2_all "<<+1<||  db2 \"RESTORE DATABASE TEST  tablespace (TS_PART)  from  '/bkpfs/bcuaix/NODE0001/TEST/backup' TAKEN AT 20140821171919  replace existing \""

rah: primary monitoring process for db2 is 20709848



db_host1: DB20000I  The RESTORE DATABASE command completed successfully.
db_host1: db2 "RESTORE DATABASE ... completed ok
bcuaix@db_host1:/db2home/bcuaix> db2_all "<<+2<||  db2 \"RESTORE DATABASE TEST tablespace (TS_PART) from  '/bkpfs/bcuaix/NODE0002/TEST/backup' TAKEN AT  20140821171919 replace existing \""




db_host1: DB20000I  The RESTORE DATABASE command completed successfully.
db_host1: db2 "RESTORE DATABASE ... completed ok
bcuaix@db_host1:/db2home/bcuaix> db2_all "<<+3<||  db2 \"RESTORE DATABASE  TEST tablespace (TS_PART) from  '/bkpfs/bcuaix/NODE0003/TEST/backup' TAKEN AT  20140821171919 replace existing \""

rah: primary monitoring process for db2 is 17498370



db_host1: DB20000I  The RESTORE DATABASE command completed successfully.
db_host1: db2 "RESTORE DATABASE ... completed ok
You have mail in /usr/spool/mail/bcuaix
bcuaix@db_host1:/db2home/bcuaix> db2_all "<<+4<|| db2 \"RESTORE DATABASE TEST tablespace (TS_PART) from  '/bkpfs/bcuaix/NODE0004/TEST/backup'   TAKEN AT  20140821171919  replace existing \""

rah: primary monitoring process for db2 is 15466886



db_host1: DB20000I  The RESTORE DATABASE command completed successfully.
db_host1: db2 "RESTORE DATABASE ... completed ok
bcuaix@db_host1:/db2home/bcuaix>


bcuaix@db_host1:/db2home/bcuaix> db2 list history dropped table all for TEST

                    List History File for TEST

Number of matching file entries = 12


 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140725144218                                        000000000000085200040004
 ----------------------------------------------------------------------------
  "BKP_TEST"."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140725144218
   End Time: 20140725144218
     Status: A
 ----------------------------------------------------------------------------
  EID: 46

 DDL: CREATE TABLE "BKP_TEST"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140725150305                                        0000000000000ce000040004
 ----------------------------------------------------------------------------
  "BKP_TEST"."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140725150305
   End Time: 20140725150305
     Status: A
 ----------------------------------------------------------------------------
  EID: 75

 DDL: CREATE TABLE "BKP_TEST"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140727185131                                        00000000000019c900040004
 ----------------------------------------------------------------------------
  "BKP_TEST"."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140727185131
   End Time: 20140727185131
     Status: A
 ----------------------------------------------------------------------------
  EID: 108

 DDL: CREATE TABLE "BKP_TEST"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140727204210                                        0000000000001d8600040005
 ----------------------------------------------------------------------------
  "BKP_TEST"."DEP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140727204210
   End Time: 20140727204210
     Status: A
 ----------------------------------------------------------------------------
  EID: 115

 DDL: CREATE TABLE "BKP_TEST"."DEP" ( "ID" INTEGER , "DEPNAME" VARCHAR(20) , "LOC" VARCHAR(20) )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140727205754                                        0000000000001f2100040004
 ----------------------------------------------------------------------------
  "BKP_SCHEMA"."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140727205754
   End Time: 20140727205754
     Status: A
 ----------------------------------------------------------------------------
  EID: 126

 DDL: CREATE TABLE "BKP_SCHEMA"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140728094337                                        000000000000273900040004
 ----------------------------------------------------------------------------
  "BKP_TEST"."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140728094337
   End Time: 20140728094337
     Status: A
 ----------------------------------------------------------------------------
  EID: 151

 DDL: CREATE TABLE "BKP_TEST"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140728101905                                        0000000000002a6400040004
 ----------------------------------------------------------------------------
  "BKP_TEST"."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140728101905
   End Time: 20140728101905
     Status: A
 ----------------------------------------------------------------------------
  EID: 188

 DDL: CREATE TABLE "BKP_TEST"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140728104041                                        0000000000002c4100040004
 ----------------------------------------------------------------------------
  "BCUAIX  "."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140728104041
   End Time: 20140728104041
     Status: A
 ----------------------------------------------------------------------------
  EID: 198

 DDL: CREATE TABLE "BCUAIX  "."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140728110747                                        000100000000086f00040004
 ----------------------------------------------------------------------------
  "BKP_TEST"."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140728110747
   End Time: 20140728110747
     Status: A
 ----------------------------------------------------------------------------
  EID: 215

 DDL: CREATE TABLE "BKP_TEST"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140729145625                                        000000000000369c00040004
 ----------------------------------------------------------------------------
  "BKP_TEST"."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140729145625
   End Time: 20140729145625
     Status: A
 ----------------------------------------------------------------------------
  EID: 250

 DDL: CREATE TABLE "BKP_TEST"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140821170527                                        000000000000f2d600040004
 ----------------------------------------------------------------------------
  "BCUAIX  "."EMP" resides in 1 tablespace(s):

 00001 TS_SMALL
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140821170527
   End Time: 20140821170527
     Status: A
 ----------------------------------------------------------------------------
  EID: 369

 DDL: CREATE TABLE "BCUAIX  "."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_SMALL" ;
 ----------------------------------------------------------------------------

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  D  T  20140821172029                                        000000000000f6e200040004
 ----------------------------------------------------------------------------
  "BKP_TEST"."EMP" resides in 1 tablespace(s):

 00001 TS_PART
 ----------------------------------------------------------------------------
    Comment: DROP TABLE
 Start Time: 20140821172029
   End Time: 20140821172029
     Status: A
 ----------------------------------------------------------------------------
  EID: 382

 DDL: CREATE TABLE "BKP_TEST"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_PART" ;
 ----------------------------------------------------------------------------
bcuaix@db_host1:/db2home/bcuaix>


bcuaix@db_host1:/db2home/bcuaix> db2 "rollforward database TEST to end of logs and complete tablespace (TS_PART) RECOVER DROPPED TABLE 000000000000f6e200040004 to /bkpfs/bcuaix/NODE0001/TEST/ "

                                 Rollforward Status

 Input database alias                   = TEST
 Number of members have returned status = 4

 Member ID    Rollforward                 Next log             Log files processed        Last committed transaction
              status                      to be read
 -----------  --------------------------  -------------------  -------------------------  --------------------------
           1  not pending                                                  -              2014-08-21-17.20.29.000000 Local
           2  not pending                                                  -              2014-08-21-17.20.29.000000 Local
           3  not pending                                                  -              2014-08-21-17.20.29.000000 Local
           4  not pending                                                  -              2014-08-21-17.20.29.000000 Local

DB20000I  The ROLLFORWARD command completed successfully.
You have mail in /usr/spool/mail/bcuaix

### tablespace is recovered to the database current time. 
### checking the export directory for the export file

bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST> ls -lrt
total 128
drwxr-xr-x    2 bcuaix   bcuigrp        8192 Aug 05 09:52 logbackup
drwxr-xr-x    2 bcuaix   bcuigrp        8192 Aug 20 11:25 fullbkp
drwxr-xr-x    2 bcuaix   bcuigrp        8192 Aug 21 17:19 backup
drwxr-x---    2 bcuaix   bcuigrp        8192 Aug 21 17:42 NODE0004
drwxr-x---    2 bcuaix   bcuigrp        8192 Aug 21 17:42 NODE0003
drwxr-x---    2 bcuaix   bcuigrp        8192 Aug 21 17:42 NODE0002
drwxr-x---    2 bcuaix   bcuigrp        8192 Aug 21 17:42 NODE0001
drwxr-x---    2 bcuaix   bcuigrp        8192 Aug 21 17:42 NODE0000
## it created all the node directory so the corresponding data will be exported

bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST> ls -lrt NODE0000
total 0
bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST> ls -lrt NODE0001
total 0
-rw-r-----    1 bcuaix   bcuigrp           0 Aug 21 17:42 data
bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST> ls -lrt NODE0002
total 0
-rw-r-----    1 bcuaix   bcuigrp           0 Aug 21 17:42 data
bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST> ls -lrt NODE0003
total 0
-rw-r-----    1 bcuaix   bcuigrp           0 Aug 21 17:42 data
bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST> ls -lrt NODE0004
total 65536
-rw-r-----    1 bcuaix   bcuigrp    33554432 Aug 21 17:42 data

## after reviewing the data it seems the data only exist in node0004 directory and remaining files are empty. This is due to the table which we recovering is very small and it doesnt have distribution key to get distributed. 

### the export file type will be del
### as the export file type is DEL, please create the empty table"
bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST/NODE0004> db2 "CREATE TABLE "BKP_TEST"."EMP" ( "ID" INTEGER , "NAME" VARCHAR(20) , "SAL" INTEGER )  DISTRIBUTE BY HASH("ID")    IN "TS_PART"
> "
DB20000I  The SQL command completed successfully.

bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST/NODE0004> db2 "describe table BKP_TEST.emp"

                                Data type                     Column
Column name                     schema    Data type name      Length     Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID                              SYSIBM    INTEGER                      4     0 Yes
NAME                            SYSIBM    VARCHAR                     20     0 Yes
SAL                             SYSIBM    INTEGER                      4     0 Yes


bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST/NODE0004> db2 "select count(*) from bkp_test.emp"

1
-----------
          0

  1 record(s) selected.

  ## at this stage we have the structure 
  ##importing
  
  
  
##  monitoring the import
## the current isolation is set to UR (un commited read) so you can able to see the current inserted rows 
  
bcuaix@db_host1:/db2home/bcuaix>  db2 "SET CURRENT ISOLATION UR"
DB20000I  The SQL command completed successfully.
bcuaix@db_host1:/db2home/bcuaix> db2 "select count(*) from bkp_test.emp"

1
-----------
    2097152

  1 record(s) selected.

bcuaix@db_host1:/db2home/bcuaix> db2 "select count(*) from bkp_test.emp"

1
-----------
    2097152

  1 record(s) selected.

bcuaix@db_host1:/db2home/bcuaix> db2 "select count(*) from bkp_test.emp"

1
-----------
    2097152

  1 record(s) selected.

###### import completed sucessfully and we got the rows

bcuaix@db_host1:/bkpfs/bcuaix/NODE0001/TEST/NODE0004> db2 "import from /bkpfs/bcuaix/NODE0001/TEST/NODE0004/data of del insert into bkp_test.emp"
SQL3109N  The utility is beginning to load data from file
"/bkpfs/bcuaix/NODE0001/TEST/NODE0004/data".

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

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

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

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


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

You have mail in /usr/spool/mail/bcuaix