DB2的rollforward前滚恢复

1.首先开启归档日志模式
db2 update db cfg for testdb using logarchmeth1 disk:/db2cfg

获取db cfg相关日志配置信息:
db2 get db cfg for testdb|grep -i log:

 Path to log files                                       = /db2cfg/test/NODE0000/SQL00001/SQLOGDIR/
 Overflow log path                     (OVERFLOWLOGPATH) = 
 Mirror log path                         (MIRRORLOGPATH) = 
 First active log file                                   = S0000004.LOG
 Block log on disk full                (BLK_LOG_DSK_FUL) = NO
 Block non logged operations            (BLOCKNONLOGGED) = NO
 Percent max primary log space by transaction  (MAX_LOG) = 0
 Num. of active log files for 1 active UOW(NUM_LOG_SPAN) = 0
 Percent log file reclaimed before soft chckpt (SOFTMAX) = 520
 Log retain for recovery enabled             (LOGRETAIN) = RECOVERY
 User exit for logging enabled                (USEREXIT) = OFF
 HADR log write synchronization mode     (HADR_SYNCMODE) = NEARSYNC
 First log archive method                 (LOGARCHMETH1) = DISK:/db2cfg/
2.测试环境较为简单testdb中只有一张表
[test@demo sqllib]$ db2 "select * from tb1"

ID          NAME      
----------- ----------
          1 yo        
          2 yo2       
          3 yo3       
          6 yo6       

  4 record(s) selected.

发起在线备份:
[test@demo sqllib]$ db2 backup db testdb online include logs
Backup successful. The timestamp for this backup image is : 20181231144308

备份成功后可以查看:
db2 list history backup all for testdb

  Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  B  D  20181231144308001   N    D  S0000004.LOG S0000004.LOG  
 ----------------------------------------------------------------------------
  Contains 3 tablespace(s):
  00001 SYSCATSPACE                                                           
  00002 USERSPACE1                                                            
  00003 SYSTOOLSPACE                                                          
 ----------------------------------------------------------------------------
    Comment: DB2 BACKUP TESTDB ONLINE                                         
 Start Time: 20181231144308
   End Time: 20181231144310
     Status: A
 ----------------------------------------------------------------------------
  EID: 14 Location: /home/test/

Earliest Log是S0000004.LOG,表明S0000004.LOG之前的日志都同步至磁盘文件,数据库已备份,
该备份前滚恢复的时候都不需要S0000004.LOG之前的日志。
3.在线全备份成功后,继续往表插入数据:
[test@demo sqllib]$ db2 "insert into tb1 values(13,'yo13'),(16,'yo16')"
DB20000I  The SQL command completed successfully.
[test@demo sqllib]$ db2 "select * from tb1"

ID          NAME      
----------- ----------
          1 yo        
          2 yo2       
          3 yo3       
          6 yo6       
         13 yo13      
         16 yo16      

  6 record(s) selected.


备份数据库的活动和归档日志(以测试前滚至最新状态)
[test@demo ~]$ cp -R /db2cfg/test/NODE0000/SQL00001/SQLOGDIR .
[test@demo ~]$ ls
sqllib  SQLOGDIR

drop掉数据库
[test@demo ~]$ db2 drop db testdb
DB20000I  The DROP DATABASE command completed successfully.
4.恢复数据库
[test@demo db2cfg]$ db2 restore db testdb
SQL2542N  No match for a database image file was found based on the source 
database alias "TESTDB" and timestamp "" provided.
    ---指定恢复介质路径再试一遍:
[test@demo db2cfg]$ db2 restore db testdb from /home/test/sqllib
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.

连接失败,数据库处于ROLL-FORWARD PENDING状态:
[test@demo ~]$ db2 connect to testdb
SQL1117N  A connection to or activation of database "TESTDB" cannot be made 
because of ROLL-FORWARD PENDING.  SQLSTATE=57019
5.rollforward数据库到最新状态,如果没有最新的活动日志和归档日志,可以rollforward
  到online备份结束状态
  db2 rollforward db testdb to end of backup and stop
  但这样就会丢失备份后数据库的状态。


[test@demo SQLOGDIR]$  db2 rollforward db testdb to end of logs and complete overflow log path \(/home/test/SQLOGDIR\)
                                 Rollforward Status
 Input database alias                   = testdb
 Number of nodes have returned status   = 1
 Node number                            = 0
 Rollforward status                     = not pending
 Next log file to be read               =
 Log files processed                    = S0000004.LOG - S0000006.LOG--(前滚需要的日志)
 Last committed transaction             = 2018-12-31-06.48.08.000000 UTC
DB20000I  The ROLLFORWARD command completed successfully.
 前滚获取日志的顺序:
 1.活动日志目录,若找到则使用该目录下日志,如果没找到则检查2
 2.overflow log path日志文件
 3.最后检查logarchmeth1指定的归档日志位置
 对于2与3获取的日志将要先放入db2活动日志目录,前滚完成后,db2将从活动目录删除这些日志文件


前滚结束后,connect to testdb可以查询备份后新插入的2条数据:
[test@demo SQLOGDIR]$ db2 "select * from tb1"

ID          NAME      
----------- ----------
          1 yo        
          2 yo2       
          3 yo3       
          6 yo6       
         13 yo13      
         16 yo16      

  6 record(s) selected.
6.查看恢复后数据库的数据库目录和表空间容器位置:
  因为恢复的时候没指定TO/DBPATH ON/ON等目录位置:
  [test@demo SQLOGDIR]$ db2 get dbm cfg|grep -i dftdb
  Default database path                       (DFTDBPATH) = /home/test
  所以恢复后本地数据库目录在DFTDBPATH /home/test下面了

[test@demo SQLOGDIR]$ db2 list db directory
 System Database Directory
 Number of entries in the directory = 1
Database 1 entry:
 Database alias                       = TESTDB
 Database name                        = TESTDB
 Local database directory             = /home/test

[test@demo SQLOGDIR]$ db2 list db directory on /home/test
 Local Database Directory on /home/test
 Number of entries in the directory = 1
Database 1 entry:
 Database alias                       = TESTDB
 Database name                        = TESTDB
 Database directory                   = SQL00001
 Database release level               = d.00


 但是数据库表空间文件存储位置等依旧在/db2data下,
 (drop掉前,testdb的路径是on /db2data  dbpath on /db2cfg), 可查看数据库表空间的路径:
 db2pd -db testdb -tablespaces
 db2 list tablespace containers for 3 show detail


其实数据库恢复可以指定数据库目录和自动存储路径(即使用自动存储表空间路径),比如异机恢复
到新目录,提供3个参数TO,DBPATH on和ON三个选项,
1.
如果目标库不存在,则restore的TO选项和DBPATH ON选项指定目标库的数据库目录,ON用来指定
自动存储位置,若只指定ON,那么本地数据库目录将会使用ON的第一个目录,
若3个参数都没指定,使用DFTDBPATH作为数据库目录,保留原有的自动存储路径。
2.
如果目标库存在,则忽略TO选项和DBPATH ON选项,若指定ON则改变自动存储路径,否则保留原有
自动存储路径。

对于异机恢复,上面参数并不能修改非自动存储路劲,于使用非自动存储路径的表空间容器,DB还
提供了redirect恢复方式来修改恢复目标位置.
db2 restore db  redirect
db2 "set tablespace container for id using (new path)"
db2 restore db  continue

 

你可能感兴趣的:(DB2)