一生产的DB2数据库需要将其数据全部备份,然后还原导入到一测试库中,中间遇到一些问题,最后还是圆满解决了,现将步骤记录下来方便学习和分析。

系统环境:AIX5.3

数据库: DB2 V9.1

两台服务器:  生产服务器 192.168.11.178

                          测试服务器 10.10.11.81

一、备份

分别备份一下生产服务器和测试服务器的数据库(两个服务器上数据库一样)

$  db2 backup db LAW online to /basefsnew/db2bak0111 include logs             #生产

备份成功。此备份映像的时间戳记是:20130111180236

$  db2 backup db LAW online to /basefs/db2bak0111 include logs      #测试

备份成功。此备份映像的时间戳记是:20130111190381

二、将生产库的备份传送到测试服务器

大家可以利用SCP命令或其他文件传输软件来传送备份,这里提醒一下传过来的备份需要改属主和属组还有权限,否则将导致还原时出错

三、具体还原过程和问题解决

$ db2 force applications all   (先停止所有应用连接)
DB20000I  The FORCE APPLICATION command completed successfully.
DB21024I  This command is asynchronous and may not be effective immediately.

在测试服务器上开始还原
$ db2 restore db LAW from /home taken at 20130111180236
SQL2539W  Warning!  Restoring to an existing database that is the same as the backup p_w_picpath database.  The database files will be deleted.
Do you want to continue ? (y/n) y
DB20000I  The RESTORE DATABASE command completed successfully.
还原成功

但随后连接数据库时报错

$ db2 connect to LAW
SQL1117N  A connection to or activation of database "LAW" cannot be made
because of ROLL-FORWARD PENDING.  SQLSTATE=57019

这个提示是说需要前滚期间的日志才能激活数据库

下面执行此句进行前滚

$ db2 rollforward db LAW to end of logs and complete
SQL4970N  Roll-forward recovery on database "LAW" cannot reach the specified
stop point (end-of-log or point-in-time) because of missing log file(s) on
node(s) "0".

提示缺失日志,不能到达结束点

注:比较顺利一次成功时,是这个状态

$db2 rollforward db LAW to end of logs and complete

Rollforward Status

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

Node number = 0
Rollforward status = not pending
Next log file to be read =
Log files processed = -
Last committed transaction = 2013-1-11-10.59.23.000000

DB20000I The ROLLFORWARD command completed successfully.
 

下面通过这个命令看一下数据库的状态和提示
$ db2 rollforward db LAW query status

                                 Rollforward Status

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

 Node number                            = 0
 Rollforward status                     = DB  working
 Next log file to be read               = S0003169.LOG
 Log files processed                    =  -
 Last committed transaction             = 2012-08-24-12.02.32.000000
通过这个命令,我们发现提示下一个日志为S0003169.LOG,
需要找到这个文件所在的路径,将生产服务器上从备份时间点后的日志全部同步或拷贝到测试服务器。
找到生产服务器上DB2日志,目录为/home/db2logs/db2inst2/LAW/NODE0000/C0000004
将此目录下从做备份的那个时间点后的日志全部拷贝放到测试服务器的相应目录(可以放到库备份的那个目录)下,同时注意修改日志文件的属主、属组和权限,防止权限不一致问题。

# chown db2inst1:db2iadm1 S0003170.LOG
# chown db2inst1:db2iadm1 S0003171.LOG
# chown db2inst1:db2iadm1 S0003172.LOG
# chown db2inst1:db2iadm1 S0003173.LOG
# chown db2inst1:db2iadm1 S0003174.LOG
# chown db2inst1:db2iadm1 S0003175.LOG
# chown db2inst1:db2iadm1 S0003176.LOG
#chmod 744  S0003170.LOG
#chmod 744  S0003171.LOG
#chmod 744  S0003172.LOG
#chmod 744  S0003173.LOG
#chmod 744  S0003174.LOG
#chmod 744  S0003175.LOG
#chmod 744  S0003176.LOG  

用如下命令来前滚日志并停止日志的溢出,注意"("/home")"格式,指的是日志所在的路径

$ db2 rollforward db LAW to end of logs and stop overflow log path "("/home")"

                                 Rollforward Status

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

 Node number                            = 0
 Rollforward status                     = not pending
 Next log file to be read               =
 Log files processed                    = S0003169.LOG - S0003176.LOG
 Last committed transaction             = 2013-01-14-21.04.00.000000

DB20000I  The ROLLFORWARD command completed successfully.

最后连接数据库,发现已成功了,经检查数据一致。
$ db2 connect to LAW

   Database Connection Information

 Database server        = DB2/6000 9.1
 SQL authorization ID   = DB2INST1
 Local database alias   = LAW

经开发和测试人员验证,一切正常。