Adsense

Wednesday, April 17, 2013

How to move db2 database across different platforms ?

Moving / Cloning the data base residing in one machine to different machine with Same Platform (Window to Windows or Linux to Linux) can be done by redirected restore operation on a full database backup image OR using backup and restore commands given below. 

1) db2 backup db dbname 
- For Source Database to be run on source machine 
db2 restore db databasename taken at timestamp into sourcedb replace existing 
- For target Database , to be run on destination machine. 

Now the problem comes when both machine are having different platforms (may be Windows to Linux or Linux to Windows, ..) , Because you can't usually back up a database on one operating system, and restore it on another operating system . For this DB2 UDB has two tools db2move & db2lookup . db2move : It is used to move the database tables across different plat forms. . db2lookup : It is used to transfer other database objects, such as constraints, triggers, indexes, sequences, table spaces, buffer pools, among others.  Using this tool, you can generate the data definition language (DDL) for such objects in the source database, and apply it to recreate those objects in the target database. 

Example for moving database from Windows machine to Linux Machine

For Example , we have to move Employee database from Windows Machine to Linux Machine 
Steps to do 
1) Create a folder EmpDB. Export the Employee database by running the command db2move

D:\EmpDB> db2move employee export
which outputs as follows

***** DB2MOVE *****

Action: EXPORT

Start time: Sat Jun 18 22:32:03 2011


Connecting to database EMPLOYEE ... successful! Server: DB2 Common Server V8.2.
0

EXPORT: 2 rows from table "TECHLAB "."EMPLOYEE"
EXPORT: 32 rows from table "SYSTOOLS"."ALTOBJ_INFO"
EXPORT: 0 rows from table "SYSTOOLS"."DB2LOOK_INFO"
EXPORT: 0 rows from table "TECHLAB "."T20110618_222010"
EXPORT: 0 rows from table "TECHLAB "."T20110618_222010_EXCEPTION"
EXPORT: 4 rows from table "TECHLAB "."DESIG"
EXPORT: 3 rows from table "TECHLAB "."QUAL"

Disconnecting from database ... successful!

End time: Sat Jun 18 22:32:05 2011

2) Generate DDL of the tables including primary key , etc..

D:\EmpDB> db2look -d employee -e -o emp.sql

which outputs 
-- USER is:
-- Creating DDL for table(s)
-- Output is sent to file: emp.sql


Now You can open the file db2move.lst file which contains a list of table names, their corresponding PC/IXF file names, and message file names. You can change this file like Schema name or remove any line having unnecessary table names. Save the fle.
Now copy all the files to linux machine . You can copy files from windows to Linux machine using WinScp utility .

After copying the files , You have to do run the following commands

1) Create new databas by issuing the following command
$ db2 create db employee

2) Run the Sql commands by running the following command
$ db2 -tvf emp.sql

3) Run the db2move command sothat all data will be imported in corresponding tables in the database. If the table does not exist , it will be automatically created.
$ db2move employee import

2 comments: