开发team需要克隆一个数据库,只更改库名,但是库里面的信息如表空间,表等对象不做变化。经指导可以使用RESTORE REDIRECT进行数据库克隆。
重定向恢复操作核心步骤有三步,包括两步数据库恢复过程,一个表空间容器重定义:
1)带 REDIRECT 选项发出 RESTORE DATABASE 命令。
2)使用 SET TABLESPACE CONTAINERS 命令来定义被恢复的数据库的表空间容器(克隆库的存放位置有关)。
3)再次发出 RESTORE DATABASE 命令,这次指定 CONTINUE 选项。
需要注意的是整个重定向恢复操作要在同一个session中,否则恢复操作会失败。
具体步骤如下:
1. 备份数据库
db2 backup db testdb online
2. 使用RESTORE重定向进行克隆
db2 restore db testdb taken at 20151202102547 dbpath on /disk2/testdbz/ into testdbz redirect
SQL1277W A redirected restore operation is being performed. During a table
space restore, only table spaces being restored can have their paths
reconfigured. During a database restore, storage group storage paths and DMS
table space containers can be reconfigured.
DB20000I The RESTORE DATABASE command completed successfully.
3. 在另一个session中查看testdb表空间容器信息
查看原库表空间容器:
0x00007F252DEF9120 0 0 File 131072 131040 - 0 /disk1/testdb/syscatspace_c1
0x00007F252DEF9780 1 0 Path 1 1 - 0 /disk1/testdb/tempspace1
0x00007F252DEDAC60 2 0 File 262144 262112 - 0 /disk1/testdb/userspace_c1
0x00007F252DED69E0 3 0 File 2621440 2621408 - 0 /disk1/testdb/cas_data8k_c1
0x00007F252DED2B80 4 0 File 4096 4092 - 0 /disk1/testdb/db2inst1/NODE0000/SQL00001/SYSTOOLSPACE
0x00007F252DED17A0 5 0 File 2621440 2621408 - 0 /disk1/testdb/cas_idx8k_c1
4. 重定向克隆库容器:
db2 "set tablespace containers for 0 using ( file '/disk2/testdbz/syscatspace_c1' 131072)"
db2 "set tablespace containers for 1 using ( path '/disk2/testdbz/tempspace1')"
db2 "set tablespace containers for 2 using ( file '/disk2/testdbz/userspace_c1' 262144)"
db2 "set tablespace containers for 3 using ( file '/disk2/testdbz/cas_data8k_c1' 2621440)"
db2 "set tablespace containers for 4 using ( file '/disk2/testdbz/db2inst1/NODE0000/SQL00001/SYSTOOLSPACE' 4096)"
db2 "set tablespace containers for 5 using ( file '/disk2/testdbz/cas_idx8k_c1' 2621440)"
5. 继续restore:
db2 restore db testdb continue
6. 前滚:
db2 "rollforward db testdbz to end of logs and complete"
此处可能会报错:log missing
SQL1273N An operation reading the logs on database "testdbZ" cannot continue
because of a missing log file "S0000000.LOG" on database partition "0" and log
stream "0".
从原库中将missing的log拷贝到克隆库的日志存放目录即可。
ls
S0000000.LOG S0000002.LOG S0000004.LOG S0000006.LOG S0000008.LOG S0000010.LOG S0000012.LOG
S0000001.LOG S0000003.LOG S0000005.LOG S0000007.LOG S0000009.LOG S0000011.LOG S0000013.LOG
cp S0000000.LOG /disk2/testdbz/db2inst1/NODE0000/SQL00001/LOGSTREAM0000/
7. 继续前滚
db2 "rollforward db testdbz to end of logs and complete"
Rollforward Status
Input database alias = testdbz
Number of members have returned status = 1
Member ID = 0
Rollforward status = not pending
Next log file to be read =
Log files processed = S0000000.LOG - S0000000.LOG
Last committed transaction = 2015-12-02-02.25.49.000000 UTC
DB20000I The ROLLFORWARD command completed successfully.
至此testdb的克隆库testdbz就建好了。