If you want to clone a database in the same server or in other compatible* server, the fastest way is using the backup &
$ db2 backup db ATOP_STG to /db/ db2d ata/ inst devp /bac kups compress Backup successful. The timestamp for this backup image is : 20090201091813
db2 " restore db atop_stg
from /db/ db2d ata/ inst devp /bac kups
taken at 20090131
into atop_tst
redirect
generate script new_db.txt"
Restore of ATOP_STG's backup image ( db2 restore db atop_stg ) The backup image is located at /db/ db2d ata/ inst devp /bac kups ( from /db/ db2d ata/ inst devp /bac kups ) The backup's timestamp used was 20090131. ( taken at 20090131 ) And the backup image should be restore in ATOP_TST. ( into atop_tst ) For restoring this ATOP_STG database's file in another location, other than the original, we requested a REDIRECT. ( redirect ) And for easing our process, DB2 will create a script called new_db.txt, so we can edit and run later on. ( generate script new_db.txt )
RESTORE DATABASE ATOP_STG
-- USER < username>
-- USING '< password> '
FROM '/db /db2 data /ins tdev p/ba ckup s'
TAKEN AT 20090131070258
-- ON '/db /db2 data /ins tdev p'
-- DBPATH ON '< target-directory> '
INTO ATOP_TST
-- NEWLOGPATH '/db /db2 data /ins tdev p/in stde vp/N ODE0 000/ SQL0 0002 /SQL OGDI R/'
-- WITH < num-buff> BUFFERS
-- BUFFER < buffer-size>
-- REPLACE HISTORY FILE
-- REPLACE EXISTING
REDIRECT
-- PARALLELISM < n>
-- COMPRLIB '< lib-name> '
-- COMPROPTS '< options-string> '
WITHOUT ROLLING FORWARD
-- WITHOUT PROMPTING
;
...
SET TABLESPACE CONTAINERS FOR 3
USING ( PATH
'/db /db2 data /ins tdev p/in stde vp/N ODE0 000/ ATOP _STG /TS/ SYST OOLS PACE '
);
...
RESTORE DATABASE ATOP_STG CONTINUE ;
RESTORE DATABASE ATOP_STG
-- Indicates where the backup image file is located
FROM '/db /db2 data /ins tdev p/ba ckup s'
-– Indicates the backup image's timestamp
TAKEN AT 20090131070258
-– Indicates the file system where the database will be created
ON '/db /db2 data /ins tdev p'
-– Indicates the database name to be restored as
INTO ATOP_TST
-– Indicates the log files location to be used for the new database
NEWLOGPATH '/db /db2 data /ins tdev p/in stde vp/N ODE0 000/ SQL0 0001 /SQL OGDI R/'
-– Indicates the existing database should be replaced by the restore
REPLACE EXISTING
-– Indicates the tablespaces will be created in a different location
REDIRECT;
..
SET TABLESPACE CONTAINERS FOR 3
USING ( PATH
-– Specify a new tablespace location
'/db /db2 data /ins tdev p/in stde vp/N ODE0 000/ ATOP _TST /TS/ SYST OOLS PACE ' );
SQL5099N The value "/db /db2 data /ins tdev p/in stde vp/N ODE0 000/ SQL0 0001 /SQL OGDI R/" indicated by the database configuration parameter "NEWLOGPATH" is not valid, reason code "4". SQLSTATE=08004
-- Abort the restore: db2 restore database ATOP_STG abort
-- Go to the specified directory and remove the files: cd /db/ db2d ata/ inst devp /ins tdev p/NO DE00 00/S QL00 001/ SQLO GDIR / rm *
-- Go back and restart the restore cd /db/ db2d ata/ inst devp /bac kups db2 –tvf new_db.txt
SQL1035N The database is currently in use.SQLSTATE=57019
First find out what users are using it the target database: The output for the command above will be like this one: Now you need for the force the user off: Be aware that you may use db2 force application all , however all connections in the instance will be forced off, not only for that database Resubmit your script:
db2 list application for db ATOP_TST
Auth Id Application Appl. Application Id DB # of Name Handle Name Agents -------- ----------- ------ --------------- -------- ----- INSTDEVP Winsql.exe 5497 9.8.0.135.19972 ATOP_TST 1
db2 "force application (5497)
db2 –tvf new_db.txt
--
Good one Sundeep
ReplyDelete