0% found this document useful (0 votes)
34 views

05-DB2 Database and Tablespace Relocation

Uploaded by

syed farhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

05-DB2 Database and Tablespace Relocation

Uploaded by

syed farhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Db2 database and tablespace relocation

Db2 database and


tablespace relocation

Db2 11.1 Advanced Database Administration

© Copyright IBM Corporation 2018


Course materials may not be reproduced in whole or in part without the written permission of IBM.
Unit 5 Db2 database and tablespace relocation

© Copyright IBM Corp. 1997, 2018 5-2


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Unit objectives
• Explain the facility of the Db2 RESTORE command to recover table
spaces to different containers
• Use the SET TABLESPACE CONTAINERS command to define new
containers during a redirected restore
• Utilize the SET STOGROUP PATHS command to change the storage
paths for automatic storage tablespaces in storage groups
• Plan the use of redirected restore as part of a disaster recovery
• Describe two methods that can be used to convert a DMS table space to
utilize Automatic Storage
• Use the GENERATE SCRIPT option of RESTORE to set up a command
script for a redirected restore operation
• Copy schemas from one database to another using the TRANSPORT
option of the RESTORE utility
• Use db2relocatedb when moving or copying Db2 databases with non-Db2
utilities
Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Unit objectives

© Copyright IBM Corp. 1997, 2018 5-3


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Backup image contents

Db2 Backup

Backup Media Header


--------------------------------------------------------------------------------------
Database Control Files:
• Backup start timestamp
• List of table spaces in backup
• Recovery History File (db2rhist.asc)
• List of all table spaces in database at time of backup
• Automatic storage paths
• Database (partition) Configuration (DB CFG)
• Buffer pool configuration file
---------------------------------------------------------------------------------------
Data in requested table spaces
---------------------------------------------------------------------------------------
Log File Header

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Backup image contents


There are some key files in a backup image which you should know about when
recovering from a disaster.
In order to recover a database to a new site or recover from a situation where access to
the database and its control files has been lost, Db2 stores information in the backup
image in addition to the data being copied.
Each backup image starts with a media header followed by the database control files
which includes:
• The backup start time stamp
• A list of the table spaces contained in the backup
• A copy of the recovery history file, db2rhist.asc, which lists key utility and
database alterations prior to the start of this backup utility
• A list of all table spaces in the database at the time the backup was taken
• The automatic storage paths used by the database
• The database configuration file, which has all the options for this database or
partition of a partitioned database
• The buffer pool configuration file, sqllbp.x

© Copyright IBM Corp. 1997, 2018 5-4


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Next is the data from the table spaces that were backed up.
At the end of the backup image is another control file, the Log File Header, or log
control files. It is used to place this backup in the sequence of database log files, and it
is used to set the starting point in the logs where a roll forward process must begin.

© Copyright IBM Corp. 1997, 2018 5-5


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Container information in a backup image


db2ckbkp -t TP1.0.inst481.DBPART000.20160117192419.001
.................................
TP1HIST
tbspInImage: T

ID: 4
.................................
Container CB
Type: 6
TotalPages: 5000
UsablePages: 4992
# of OS rsvd bytes: 512
Page 0 offset: 131072
Tag offset: 512
Extent offset: 0
Name: /database/inst481/tp1hist
.................................
TP1ACCTD
tbspInImage: F

ID: 5
.................................
Container CB
Type: 6
TotalPages: 30000
UsablePages: 29888
# of OS rsvd bytes: 512
Page 0 offset: 262144
Tag offset: 512
Extent offset: 0
Name: /database/inst481/tp1acctd
.................................

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Container information in a backup image


If you look at the options for a RESTORE command you will find the ability to specify
selected table space names, but there are no options to define table space containers.
The restore process reads the table space container definitions that are stored in the
backup image and automatically restores data to the same disk file or device.
You can use the backup check utility, db2ckbkp, to list the detailed table space
container information saved in the backup. This could be used at a disaster recovery
site to plan the recovery when information about the original database disk
configuration is not available.
The option '-t' displays table space details, including container information, for the table
spaces in the image.

© Copyright IBM Corp. 1997, 2018 5-6


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Db2 RESTORE: Container damaged

backup database tp1 to... restore database tp1 from...


Restore Fails!
Db2 Backup

IBMSTOGROUP IBMSTOGROUP
SYSCATSPACE
SYSCATSPACE
TEMPSPACE1
TEMPSPACE1
DB2
Db2 Database Db2 Database
TP1HIST TP1HIST

X
/DBfs1/cont1 /DBfs2/cont2
/DBfs2/cont2 /DBfs1/cont1
file file
file file

APSTOGRP1 APSTOGRP1
Table space
Table space TSAP1
TSAP1
Containers
Containers TSAP2
TSAP2

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Db2 RESTORE: Container damaged


During a database backup operation, a record is kept of all the table space containers
and automatic storage paths associated with the table spaces that are being backed
up.
During a restore operation, all containers listed in the backup image are checked to
determine if they exist and if they are accessible. If one or more of these containers or
storage paths are inaccessible because of media failure (or for any other reason), the
restore operation will fail. A successful restore operation in this case requires redirection
to different containers or new storage paths. Db2 supports adding, changing, or
removing table space containers for DMS managed table spaces and changes to
automatic storage paths.
Db2 can create directories and files during the restore operation if the file system or
device is accessible. If a UNIX file system is not mounted, or a disk cannot be accessed
by the system, then Db2 cannot complete the restore.
The slide shows a condition where one container for a DMS managed tablespace,
TP1HIST is offline, which causes a standard RESTORE to fail

© Copyright IBM Corp. 1997, 2018 5-7


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Redirected restore: Overview

1) restore database tp1 from...


Db2 Backup .... redirect
2) set tablespace containers
for 4 using
IBMSTOGROUP

SYSCATSPACE
(file '/database/inst491/testhist2' 5000)
Db2 Database
TS id = 0
TEMPSPACE1
3) restore database tp1
TS id =1
continue
USERSPACE1 TP1HIST TS id =4

TS id =2

APSTOGRP1
DAMAGED

TSAP1 ts ID = 5 /database/inst491/testhist /database/inst491/testhist2


file file
TSAP2 ts ID = 6
New Table space Container

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Redirected restore: Overview


Db2 supports adding, changing, or removing table space containers and changing
automatic storage paths during RESTORE processing.
You can redefine table space containers or automatic storage paths by invoking the
RESTORE DATABASE command and specifying the REDIRECT parameter.
The restore process is divided into three separate steps or phases:
1. The RESTORE command is issued to select the database or table spaces to
restore from the specified backup image, with the REDIRECT option included.
This causes Db2 to delay the restoring of the data from the backup until the
third phase.
2. In phase two, one or more SET TABLESPACE CONTAINERS commands can
be issued to redefine the containers to be used for a table space. During a
Database level RESTORE, SET STOGROUP PATHS statements could be
used to redefine automatic storage paths.
3. In phase three, the RESTORE command is issued with the CONTINUE option
to direct Db2 to restore the database or table spaces to the database with the
modified container definitions.
In the example, this would allow the restore to complete successfully even though one
of the original disks, /database/inst491/testhist could not be accessed.

© Copyright IBM Corp. 1997, 2018 5-8


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Step 1: Start the redirected restore

db2 "restore db tp1 tablespace(tp1hist) online


from $HOME/backups taken at 20160117155148 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.

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Step 1: Start the redirected restore


The first phase is initialization. The RESTORE command is issued to restore a
database or selected table spaces from the backup image specified in the FROM
clause. Db2 will read the header information from the backup image and check to see if
the table space containers that were present at the time of the backup are available.
The REDIRECT option tells Db2 to delay restoring any data extents to allow table
space containers to be altered.

© Copyright IBM Corp. 1997, 2018 5-9


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Step 2a: Check tablespace ID and size


db2 list tablespaces
Tablespaces for Current Database

Tablespace ID = 0
Name = SYSCATSPACE
Type = Database managed space
Contents = All permanent data. Regular table space.
State = 0x0000
Detailed explanation:
Normal
. . . . . .
Tablespace ID = 4
Name = TP1HIST
Type = Database managed space
Contents = All permanent data. Large table space.
State = 0x2003100
Detailed explanation:
Restore pending
Storage must be defined
Restore in progress
Storage may be defined

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Step 2a: Check tablespace ID and size


The LIST TABLESPACES and LIST TABLESPACE CONTAINERS commands can be
used to check table space information during RESTORE processing. The SET
TABLESPACE CONTAINERS command uses the table space ID rather than a table
space name, so the table space ID needs to be checked for any table spaces that need
to have container changes.
When table spaces are being restored online the MON_GET_TABLESPACE and
MON_GET_CONTAINER functions could be used in SQL statements to retrieve table
space statistics using another command line session.

© Copyright IBM Corp. 1997, 2018 5-10


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Step 2b: Check container information

db2 list tablespace containers for 4 show detail

Tablespace Containers for Tablespace 4

Container ID = 0
Name = /database/inst491/testhist
Type = File
Total pages = 5000
Useable pages = 4928
Accessible = Yes

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Step 2b: Check container information


The LIST TABLESPACE CONTAINERS command can be used to list the table space
size and container definitions. The command references the tablespace ID rather than
a tablespace name. The report includes the total number of pages defined for a table
space.

© Copyright IBM Corp. 1997, 2018 5-11


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Step 3: Set container definition

db2 set tablespace containers for 4 using ((file ‘/database/inst491/testhist2’ 5000 ) )

DB20000I The SET TABLESPACE CONTAINERS command completed successfully.

Note: For DMS managed tablespace containers the SET TABLESPACE


CONTAINERS command will allocate and format the new containers which
can take considerable time if the containers are large.

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Step 3: Set container definition


In the slide, the following command is shown:
set tablespace containers
for 4 using ((file '/database/inst491/testhist2' 5000 )
This command will define a new container for the table space with a table space ID of 4,
TP1HIST. This is a DMS table space, so file containers and raw device containers
could be specified.
Db2 would allow either more or fewer containers to be defined as long as there is
enough space allocated to complete the restore. For DMS table spaces, the amount
specified must be at least equal to the high water mark, the highest extent that contains
data to be restore for the table space. If the REORG utility is used to reorganize a table
in a DMS table space without using a temporary table space, this could set the high
water mark to a size twice the size of the table.

© Copyright IBM Corp. 1997, 2018 5-12


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Step 4: Complete restore processing

db2 restore database tp1 continue

DB20000I The RESTORE DATABASE command completed successfully.

db2 rollforward db tp1 to end of logs tablespace online


Rollforward Status

Input database alias = tp1


Number of members have returned status = 1

Member ID = 0
Rollforward status = not pending
Next log file to be read =
Log files processed = -
Last committed transaction = 2016-05-15-13.57.30.000000 UTC

DB20000I The ROLLFORWARD command completed successfully.

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Step 4: Complete restore processing


To complete the redirected restore processing, use the RESTORE command with the
CONTINUE option. This will cause Db2 to restore the data extents from the backup
image using the modified containers.
For a recoverable database, the roll forward process is required to recover the
database or table space to a point following the original backup. In the example, the TO
END OF LOGS option is used to apply the latest logged updates to the restored table
space.
The table space would now be available for access using the modified containers.

© Copyright IBM Corp. 1997, 2018 5-13


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Changing Automatic Storage paths


• Automatic Storage Table spaces can not be individually moved using
the REDIRECT option of RESTORE
• Automatic Storage table spaces can be reassigned new containers
during a database level RESTORE
• With Db2 multiple storage groups can be defined, the group
IBMSTOGROUP is created by default during CREATE DATABASE
 The storage paths assigned to a storage group can be changed during a
database redirected restore using a SET STOGROUP PATHS statement:

db2 set stogroup paths for IBMSTOGROUP


on '/dbauto/path1’, '/dbauto/path2'

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Changing Automatic Storage paths


Automatic Storage managed table spaces cannot be individually moved using the
REDIRECT option of a RESTORE command.
Table spaces using AUTOMATIC Storage will reuse the containers defined at the time
of the backup image when a RESTORE is performed at the TABLESPACE level.
If a DATABASE level restore is performed, the automatic storage paths can be
redefined, which will cause new container assignments for the table spaces.
The Db2 feature of storage groups for automatic storage managed table spaces, allows
you to define multiple storage groups. The RESTORE utility with the REDIRECT can be
used with a database level restore and the SET STOGROUP PATHS statement can
assign new storage paths to a specific storage group, which would cause the table
spaces in that storage group to acquire new containers. No SET TABLESPACE
CONTAINERS commands are allowed or required for Automatic Storage managed
table spaces.
Any storage groups that have their paths redefined during a restore operation will not
have any changes to their storage paths or media attributes replayed during a
subsequent roll forward operation.

© Copyright IBM Corp. 1997, 2018 5-14


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Generating a redirected restore script (1 of 3)


db2 restore db tp1 tablespace(TP1HIST) from /home/inst1/backups
redirect GENERATE SCRIPT rdirect.txt
RESTORE DATABASE TP1
-- USER <username>
-- USING '<password>' The Generated Script includes
TABLESPACE (
"TP1HIST" a RESTORE DATABASE command
)
-- ONLINE
FROM '/home/inst491/backups/lab7'
TAKEN AT 20160718095239
-- ON '/dbauto/path1'
-- DBPATH ON '<target-directory>'
INTO TP1
-- NEWLOGPATH '/dblogs/inst491/tp1/NODE0000/LOGSTREAM0000/'
-- 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
;

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Generating a redirected restore script


To perform a redirected restore using a script:
Use the Restore utility to generate a redirected restore script. The Restore utility can be
invoked through the command line processor (CLP) or the db2Restore application
programming interface (API).
The following is an example of the RESTORE DATABASE command with the
REDIRECT option and the GENERATE SCRIPT option:
db2 restore db test from /home/jseifert/backups taken at 20140304090733
redirect generate script test_node0000.clp
This creates a redirected restore script on the client called test_node0000.clp.
Open the redirected restore script in a text editor to make any modifications that are
required.
You can modify:
• Restore options
• Automatic storage paths
• Container layout and paths
Run the modified redirected restore script. For example:
db2 -tvf test_node0000.clp

© Copyright IBM Corp. 1997, 2018 5-15


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Generating a redirected restore script (2 of 3)


*****************************************************************************
-- ** storage group definition
-- ** Default storage group ID = 0
-- ** Number of storage groups = 2
-- *****************************************************************************
-- *****************************************************************************
-- ** Storage group name = IBMSTOGROUP
-- ** Storage group ID = 0
-- ** Data tag = None
-- *****************************************************************************
-- SET STOGROUP PATHS FOR IBMSTOGROUP
-- ON '/dbauto/path1'
-- ;
-- *****************************************************************************
-- ** Storage group name = TP1SG1
-- ** Storage group ID = 1
-- ** Data tag = None
-- *****************************************************************************
-- SET STOGROUP PATHS FOR TP1SG1
-- ON '/dbauto/path2' The Generated Script includes
-- ; SET STOGROUP PATHS
Statements to redefine storage groups

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

The example output includes SET STOGROUP PATHS statements for any automatic
storage groups in the database at the time of the backup.
These statements are marked as comments. The SET STOGROUP PATHS could only
be utilized if you perform a database level redirected restore.

© Copyright IBM Corp. 1997, 2018 5-16


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Generating a redirected restore script (3 of 3)


*****************************************************************************
-- ** Tablespace name = TP1HIST
-- ** Tablespace ID = 4
-- ** Tablespace Type = Database managed space
-- ** Tablespace Content Type = All permanent data. Large table space.
-- ** Tablespace Page size (bytes) = 16384
-- ** Tablespace Extent size (pages) = 8
-- ** Using automatic storage = No
-- ** Storage group ID = -1
-- ** Source storage group ID = -1
-- ** Data tag = None
-- ** Auto-resize enabled = Yes
-- ** Total number of pages = 5000
-- ** Number of usable pages = 4992
The Generated Script includes
-- ** High water mark (pages) = 64
SET TABLESPACE CONTAINERS
-- *****************************************************************************
SET TABLESPACE CONTAINERS FOR 4 Statements to redefine containers
-- IGNORE ROLLFORWARD CONTAINER OPERATIONS
USING (
FILE '/database/inst491/tp1hist' 5000
);
-- *****************************************************************************
-- ** start redirected restore
-- *****************************************************************************
RESTORE DATABASE TP1 CONTINUE;

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

The example output shows some of the information provided in comments for a table
space, including, the total number of pages, the high water mark and whether the table
space is managed by automatic storage.
Automatic Storage table space containers cannot be changed during a redirected
restore using the SET TABLESPACE CONTAINERS commands.

© Copyright IBM Corp. 1997, 2018 5-17


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Db2 RESTORE: Restore to disaster site


restore database tp1 from...
backup database tp1 to...
Db2 Backup REDIRECT
set tablespace containers..
IBMSTOGROUP set stogroup paths…
SYSCATSPACE
TEMPSPACE1 Db2 Database
Db2 Database
TP1HIST
IBMSTOGROUP TP1HIST

SYSCATSPACE
/DBfs1/cont1 /DBfs2/cont2 TEMPSPACE1
file file
/DBfs2/cont1
APSTOGRP1
file
APSTOGRP1 TSAP1
TSAP1
TSAP2
TSAP2

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Db2 RESTORE: Restore to disaster site


A redirected restore might be required when a Db2 backup image is restored to a
different system where there could be a different disk configuration. For example, if the
production system has 50 smaller capacity disks assigned and the disaster site has 20
larger capacity disks, the container definitions might need to be altered to fit the new
disk configuration.
Automatic storage groups can easily be redefined to allocate the tablespaces into
different storage paths.
In the slide, the tablespace TP1HIST used two smaller containers at the production
location and is planned to use one large container at the disaster site.
The redirected restore could also be used to restore a production database backup to a
test database on the same server to avoid the duplication of directory and file names on
the single server. The container names generated for automatic storage tablespaces
include the database name and would automatically avoid duplicate container names
when restored to a different database name or Db2 instance.
If you need to migrate a database to a new disk subsystem, you could use a redirected
restore to move all or a portion of the database.

© Copyright IBM Corp. 1997, 2018 5-18


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Converting a DMS table space to use Automatic Storage


1. ALTER TABLESPACE can be used to convert a DMS table space to
an Automatic Storage table space without an outage:
 ALTER TABLESPACE … MANAGED BY AUTOMATIC STORAGE [ USING
STOGROUP sg_name ]
 All new growth comes from the automatic storage paths
 Old (file or raw) containers can be removed using ALTER TABLESPACE
REBALANCE
ALTER TABLESPACE TSP12 MANAGED BY AUTOMATIC STORAGE
USING STOGROUP sg_medium
ALTER TABLESPACE TSP12 REBALANCE
2. A redirected RESTORE can be used to convert DMS table spaces to
AUTOMATIC STORAGE table spaces:
 The USING AUTOMATIC STORAGE option of the SET TABLESPACE
CONTAINERS will convert a DMS-managed table space to automatic
storage management using the default storage group
SET TABLESPACE CONTAINERS FOR 6 USING AUTOMATIC STORAGE
Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Converting a DMS table space to use Automatic Storage


A Database Managed (DMS) table space can be converted to use Automatic Storage
management using two methods.
In order to minimize the loss of availability for a table space during the conversion, the
ALTER TABLESPACE command can be used to convert a database-managed table
space with user assigned containers to an Automatic Storage-managed table space.
This requires two steps.
• First, the ALTER TABLESPACE statement with the MANAGED BY AUTOMATIC
STORAGE option is used to change the table space definition to become an
Automatic Storage table space. The USING STOGROUP clause can be used to
assign the table space to a specific storage group. This will assign some new
containers to the table space from the Automatic Storage paths defined for the
storage group, but will retain the original file or raw device containers. The new
Automatic Storage containers will be added as a new stripe set so no rebalancing
will occur and any new space added by autoresize will take place in the
Automatic Storage paths. At this point, Db2 considers this an Automatic Storage
table space and will not permit any alteration that directly names containers like
the ALTER TABLESPACE ADD or DROP.

© Copyright IBM Corp. 1997, 2018 5-19


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

• Next, the ALTER TABLESPACE STATEMENT with the REBALANCE option can
be used to rearrange the extents in a way that releases the original non-
Automatic Storage containers. For example:
ALTER TABLESPCE TSP12 REBALANCE
DMS-managed table spaces can be converted to Automatic Storage management
using a a database or table space redirected restore. The SET TABLESPACE
CONTAINERS statement specifies USING AUTOMATIC STORAGE rather than listing
user assigned containers. The table space will be assigned to the default storage group
for the database.
SET TABLESPACE CONTAINERS FOR 6 USING AUTOMATIC STORAGE
There is no supported method to convert an Automatic Storage managed table space
to DMS management.

© Copyright IBM Corp. 1997, 2018 5-20


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

ALTER TABLESPACE Add: After backup (1 of 2)

BACKUP Table Space ALTER TABLESPACE DMS01 DMS01 ALL Containers


DMS01 Backup ADD ( file '/DBfs1/cont2' 10000) Inaccessible

Time 1 Time 2 Time 3

S0000021.log S0000022.log S0000023.log S0000024.log

Db2 Database Db2 Database Db2 Database

DMS01 DMS01
DMS01
X X
/DBfs1/cont1 /DBfs1/cont1
/DBfs1/cont1
file file
file

/DBfs1/cont2 /DBfs1/cont2
file file

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

ALTER TABLESPACE Add: After backup


The information stored in the backup image includes the container definitions at the
time of the backup. For DMS table spaces, an ALTER TABLESPACE can be used to
add additional container definitions to an existing table space. That ALTER
TABLESPACE is logged by Db2. If the table space is restored from the previous
backup image and a roll forward is performed that includes the log record for the
ALTER TABLESPACE, Db2 will redo the container creation as part of the roll forward.
In the example shown:
1. At Time 1, a backup of the table space DMS01 is created containing a single
container definition.
2. At Time 2, an ALTER TABLESPACE is executed to add a new container to the
table space, on the same disk as the first container.
3. At Time 3, a hardware problem occurs and the disk with both containers in now
inaccessible.
4. It is determined that a redirected restore will be used to recover from this
problem.

© Copyright IBM Corp. 1997, 2018 5-21


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

ALTER TABLESPACE Add: After backup (2 of 2)

ALTER TABLESPACE DMS01


Table Space
ADD ( file 'd:\dms01\cont2' 10000)
DMS01 Backup

X
S0000021.log S0000022.log S0000023.log S0000024.log

Db2 Database 1) restore database tp1 from... Db2 Database


.... redirect

DMS01 2) set tablespace containers


DMS01
for 3 using (file '/DBfs2/cont1' 20000)
X X IGNORE ROLLFORWARD CONTAINER OPERATIONS

/DBfs1/cont1
3) restore database tp1 continue
file /DBfs2/cont1
4) rollforward database tp1
/DBfs1/cont2 file
to end of logs
file tablespace(dms01) online
Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Since a table space container was added after the last backup image was created, the
roll forward process would normally redo the container definition. The loss of access to
the disk with both containers would prevent the roll forward from completing
successfully.
The SET CONTAINERS command has an option that will direct Db2 to either REPLAY
or IGNORE the container changes during the roll forward process. This option would
apply to the single table space defined in the command, new containers could still be
added to other table spaces during the roll forward.
In the example, the IGNORE ROLLFORWARD CONTAINER OPERATIONS option is
used to define a single container to be used for the restore and roll forward processing
and to bypass the container addition that was logged. This allows the redirected restore
process to complete recovery of the table space including all of the current log files.
The command syntax is:
>>-SET TABLESPACE CONTAINERS FOR--tablespace-id------->

>-----+----------------------------------------+------>
| .-REPLAY--. |
'--+-IGNORE--+---ROLLFORWARD CONTAINER OPERATIONS--'

© Copyright IBM Corp. 1997, 2018 5-22


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Copying schemas of objects from one database to another


using db2move

• One or more schemas can be copied from one database to another


using the db2move COPY option:
 Table data is copied using LOAD utility processing
 Source and target databases can use different product platforms
− For example, from Db2 on Windows to Db2 on AIX
 Schemas can be renamed
 Options to create schema objects, create and load or just load data
 Alternate table spaces could be assigned
− Example - To duplicate schema schema1 from source database dbsrc to the target
database dbtgt, rename the schema to newschema1 on the target, and map
source table space ts1 to ts2 on the target, issue:

db2move dbsrc COPY -sn schema1 -co TARGET_DB dbtgt


USER myuser1 USING mypass1
SCHEMA_MAP ((schema1,newschema1))
TABLESPACE_MAP ((ts1,ts2), SYS_ANY))
Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Copying schemas of objects from one database to another using db2move


The db2move command can be used to copy one or more schemas of database
objects between databases using the COPY option. The source and target database
names are supplied in the command. The two databases might be on different servers
using different Db2 platforms. The data is copied using a LOAD utility based on a cursor
rather than using external files.
Using this method the schemas could be renamed using the SCHEMA_MAP keyword.
The table space assignments could be adjusted using the TABLESPACE_MAP
keyword.
This utility attempts to copy all allowable schema objects with the exception of the
following types:
• Table hierarchy
• Staging tables (not supported by the load utility in multiple partition database
environments)
• Jars (Java routine archives)
• Nicknames
• Packages

© Copyright IBM Corp. 1997, 2018 5-23


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

• View hierarchies
• Object privileges (All new objects are created with default authorizations)
• Statistics (New objects do not contain statistics information)
• Index extensions (user-defined structured type related)
• User-defined structured types and their transform functions

© Copyright IBM Corp. 1997, 2018 5-24


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Copying schemas of objects from one database to another


using RESTORE with TRANSPORT

• One or more schemas can be copied from one database to another


using the TRANSPORT option of RESTORE:
 The tables are copied from a backup image into one or more table spaces
 The objects (tables, indexes, views) and table spaces can not duplicate any
existing object in the target database
 Table spaces are restored using a RESTORE utility including the
TRANSPORT option
 A list of table spaces must be named using the TABLESPACE option
 The SCHEMA option is required to list the schema names to be transported
to the target database
 The target database must use a compatible platform for the source backup
image
− Db2 for Linux could not be restored on Db2 for Windows or AIX

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Copying schemas of objects from one database to another using RESTORE with
TRANSPORT
Table spaces and SQL schemas can be restored as a set from one database to
another using transportable sets.
By using the RESTORE command with the TRANSPORT option, you can restore data
in a set of table spaces from a backup image into another existing database. You can
re-create database objects in the SQL schemas that reference the data in the restored
table spaces. The restored table spaces and SQL schemas can function as part of the
new database.
The objects being transported cannot duplicate any existing database objects in the
target database.
The list of table spaces must be specified in a TABLESPACE option when using the
TRANSPORT option. You must also list the schemas using the SCHEMA option.
Transporting a database schema involves taking a backup image of a database and
restoring the database schema to a different, existing database. When you transport a
database schema, the database objects in the transported schema are re-created to
reference the new database, and the data is restored to the new database.

© Copyright IBM Corp. 1997, 2018 5-25


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Objects that can be transported using the TRANSPORT


option of RESTORE
• Tables, created global temporary tables, materialized query tables
• Views (standard and statistical views)
• The following types of generated columns:
 Expression
 Identity
 Row change timestamp
 Row change token
• User-defined functions, user-defined types, and generated functions
• The following types of constraints:
 Check
 Foreign Key
 Unique and primary
 Functional dependency
• Indexes
• Triggers
• Sequences
• Functions and procedures except for external routine executables
• Object authorizations, privileges, security, access control, and audit configuration
• Table statistics, profiles, and hints
• Packages
Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Objects that can be transported using the TRANSPORT option of RESTORE


When you transport data from a backup image to a different database, the physical and
logical objects contained in the table spaces being restored are re-created in the target
database, and the table space definitions and containers are added to the target
database.
The following logical objects are re-created:
• Tables, created global temporary tables, materialized query tables, and normal
and statistical views
• The following types of generated columns:
• Expression
• Identity
• Row change timestamp
• Row change token
• User-defined functions, user-defined types, and generated functions

© Copyright IBM Corp. 1997, 2018 5-26


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

• The following types of constraints:


• Check
• Foreign Key
• Unique and primary
• Functional dependency
• Indexes
• Triggers
• Sequences
• Functions and procedures except for external routine executables
• Object authorizations, privileges, security, access control, and audit configuration
• Table statistics, profiles, and hints
• Packages
The following components of a schema are not created on the target database:
• Aliases
• Created global variables
• Jobs
• Index extensions
• Hierarchical tables, typed tables, and typed views
• Nicknames
• Structured types
• Methods
• Servers
• Wrappers
• Functional mappings and templates
• OLE DB external functions
• Sourced procedures
• External routine executable files
• System catalogs
• Range-partitioned tables

© Copyright IBM Corp. 1997, 2018 5-27


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Transportable sets

Following combinations of table spaces and schemas are valid transportable sets:
• Tablespace1 with schema1 and schema2
• Tablespace2 and tablespace3 with schema3
• Tablespace4, tablespace5, and tablespace6, with schema4 and schema5
• Combination of valid transportable sets also constitutes a valid transportable set:
 tablespace1, tablespace2, and tablespace3, with schema1, schema2, schema3

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Transportable sets
A database schema must be transported in its entirety. If a table space contains both
the schema you want to transport, as well as another schema, you must transport all
data objects from both schemas. These sets of schemas that have no references to
other database schemas are called transportable sets. The data in the table spaces
and logical objects in the schemas in a transportable set reference only table spaces
and schemas in the transportable set. For example, tables have table dependencies
only on other tables in the transportable set.
The example shows the mapping of table spaces to database objects in different
schemas.
The following combinations of table spaces and schemas are valid transportable sets:
• tablespace1 with schema1 and schema2
• tablespace2 and tablespace3 with schema3
• tablespace4, tablespace5, and tablespace6, with schema4 and schema5

© Copyright IBM Corp. 1997, 2018 5-28


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

A combination of valid transportable sets also constitutes a valid transportable set:


• tablespace1, tablespace2, and tablespace3, with schema1, schema2, and
schema3
• The set tablespace4 and tablespace5 with schema4 is not a valid transportable
set because there are references between tablespace5 and schema5 and
between schema5 and tablespace6. The set requires tablespace6 with schema5
to be a valid transportable set.

© Copyright IBM Corp. 1997, 2018 5-29


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Db2 RESTORE: TRANSPORT


db2 backup
database PROD
Tablespace(syscatspace, Database SYSCATSPACE
Db2 Backup
TSap1,TSap2) TEST
TSAP1
online to...
TSAP2

TST01 TST02 Test database


SYSCATSPACE Database
PROD
TSAP1 db2 restore db PROD
(schema TABLESPACES (TSAP1,TSAP2)

AP1) SCHEMA(AP1,AP2)
TRANSPORT into TEST
TSAP2 TSAP3 TSAP4
FROM ... REDIRECT
(schema
AP2) db2 set tablespace containers
for 4 using ……
Production Site db2 set tablespace containers
for 5 using ……

db2 restore db PROD continue

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Db2 RESTORE: TRANSPORT


In this example, two database schemas are copied from the database named PROD to
the target database named TEST. The two schemas, AP1 and AP2 are stored in two
table spaces, TSAP1 and TSAP2 in the source database. An online table space backup
of the PROD database could include the two table spaces TSAP1 and TSAP2 as well
as the catalog tablespace SYSCATSPACE. A database backup could also be used.
The RESTORE command would refer to the source and target databases and list the
table spaces and schemas to be transported into the target database. The REDIRECT
option of the RESTORE command could be used to assign new table space containers
for use by the target database.

© Copyright IBM Corp. 1997, 2018 5-30


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Processing performed by RESTORE TRANSPORT


• Backup image used for RESTORE with TRANSPORT option
 Can be either database or table space level
 Can be online or offline
• The transport operation consists of the following phases:
 Staging database creation:
− Temporary Database is used to extract schema definitions from source catalog
information
− A STAGE IN option may be used to set the staging database name
 Physical table space container restoration
 Roll forward processing:
− Used to redo or undo changes performed when using an online backup
− Log files included in backup image are used
− Similar to roll forward TO END OF BACKUP
 Schema validation
 Transfer of ownership of the table space containers
 Schema re-creation in target database
 Dropping the staging database (if the STAGE IN option is not specified)

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Processing performed by RESTORE TRANSPORT


When transporting database schemas, a temporary database is created and named as
a part of the transport operation. The transport staging database is used to extract
logical objects from the backup image so that they can be re-created on the target
database. If logs are included in the backup image, they will also be used to bring the
staging database to a point of transactional consistency. The ownership of the
transported table spaces is then transferred to the target database.
The transport operation consists of the following phases:
1. Staging database creation.
2. Physical table space container restoration.
3. Rollforward processing - the log files included in an online backup image are
used to replay and undo any uncommitted changes produced during the backup
processing. This would be similar to performing a roll forward to the
END OF BACKUP.
4. Schema validation.
5. Transfer of ownership of the table space containers.
6. Schema re-creation in target database.
7. Dropping the staging database (if the STAGE IN option is not specified).

© Copyright IBM Corp. 1997, 2018 5-31


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

db2relocatedb command
After copying the files,
use db2relocatedb to: Instance inst1
1. Change database name Automatic Storage
2. Change database path Path(s) DB Directory

3. Change Db2 instance


4. Change Automatic Storage Path(s) SYSCATSPACE Db2
Database Active Logs
5. Change Log path
TEMPSPACE1
6. Change Table space container(s) PROD Archive Logs
7. Change additional log related paths
db2relocatedb -f filename Table space Table space
DMS02
DMS01

DB_NAME=oldName,newName
DB_PATH=oldPath,newPath
INSTANCE=oldInst,newInst
NODENUM=nodeNumber
LOG_DIR=oldDirPath,newDirPath
CONT_PATH=oldContPath1,newContPath1
CONT_PATH=oldContPath2,newContPath2
STORAGE_PATH=oldStorPath1,newStorPath1
Db2 database and tablespace relocation © Copyright IBM Corporation 2018

db2relocatedb command
Each Db2 database has a set of control files that identify the name of the database, the
instance name, the names of all table space containers, the automatic storage paths
and the various logging related paths. The Db2 RESTORE utility makes changes to
some of these files automatically. The db2relocatedb command can be used to update
this information when part of a Db2 database is relocated outside Db2's control.
This command renames a database, or relocates a database or part of a database (for
example, the container and the log directory) as specified in the configuration file
provided by the user. This tool makes the necessary changes to the Db2 instance and
database support files.
The target database must be offline before running the db2relocatedb command to
modify the control files and metadata of the target database.
The changes that the db2relocatedb command makes to files and control structures of
a database are not logged and are therefore not recoverable. A good practice is to
make a full backup after running the command against a database, especially if the
database is recoverable with log files being retained.

© Copyright IBM Corp. 1997, 2018 5-32


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Authorization
None.
Command Syntax
db2relocatedb -f configFilename
Command Parameters
-f configFilename
Specifies the name of the file containing configuration information necessary for
relocating the database. This can be a relative or absolute filename. The format of the
configuration file is:
DB_NAME=oldName,newName
DB_PATH=oldPath,newPath
INSTANCE=oldInst,newInst
NODENUM=nodeNumber
LOG_DIR=oldDirPath,newDirPath
CONT_PATH=oldContPath1,newContPath1
CONT_PATH=oldContPath2,newContPath2

STORAGE_PATH=oldStoragePath1,newStoragePath1
STORAGE_PATH=oldStoragePath2,newStoragePath2

FAILARCHIVE_PATH=newDirPath
LOGARCHMETH1=newDirPath
LOGARCHMETH2=newDirPath
MIRRORLOG_PATH=newDirPath
OVERFLOWLOG_PATH=newDirPath
Where:
• DB_NAME Specifies the name of the database being relocated. If the database
name is being changed, both the old name and the new name must be specified.
This is a required field.
• DB_PATH Specifies the path of the database being relocated. This is the path
where the database was originally created. If the database path is changing, both
the old path and new path must be specified. This is a required field.
• INSTANCE Specifies the instance where the database exists. If the database is
being moved to a new instance, both the old instance and new instance must be
specified. This is a required field.
• NODENUM Specifies the node number for the database node being changed.
The default is 0.

© Copyright IBM Corp. 1997, 2018 5-33


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

• LOG_DIR Specifies a change in the location of the log path. If the log path is
being changed, then both the old path and new path must be specified. This
specification is optional if the log path resides under the database path, in which
case the path is updated automatically.
• CONT_PATH Specifies a change in the location of table space containers. Both
the old and new container path must be specified. Multiple CONT_PATH lines
can be provided if there are multiple container path changes to be made. This
specification is optional if the container paths reside under the database path, in
which case the paths are updated automatically. If you are making changes to
more than one container where the same old path is being replaced by a
common new path, a single CONT_PATH entry can be used. In such a case, an
asterisk (*) could be used both in the old and new paths as a wildcard.
• STORAGE_PATH Specifies a change in the location of one of the storage paths
for the database. Both the old storage path and the new storage path must be
specified. Multiple STORAGE_PATH lines can be given if there are several
storage path changes to be made. You can specify this parameter to modify any
storage path in all storage groups. However, you cannot specify this parameter to
modify the storage paths for an individual storage group.
• FAILARCHIVE_PATH Specifies a new location to archive log files if the
database manager fails to archive the log files to either the primary or the
secondary archive locations. You should only specify this field if the database
being relocated has the failarchpath configuration parameter set.
• LOGARCHMETH1 Specifies a new primary archive location. You should only
specify this field if the database being relocated has the logarchmeth1
configuration parameter set.
• LOGARCHMETH2 Specifies a new secondary archive location. You should only
specify this field if the database being relocated has the logarchmeth2
configuration parameter set.
• MIRRORLOG_PATH Specifies a new location for the mirror log path. The string
must point to a path name, and it must be a fully qualified path name, not a
relative path name. You should only specify this field if the database being
relocated has the mirrorlogpath configuration parameter set.
• OVERFLOWLOG_PATH Specifies a new location to find log files required for a
rollforward operation, to store active log files retrieved from the archive, and to
find and store log files required by the db2ReadLog API. You should only specify
this field if the database being relocated has the overflowlogpath configuration
parameter set.

© Copyright IBM Corp. 1997, 2018 5-34


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Usage Notes:
If the instance that a database belongs to is changing, the following must be done
before running this command to ensure that changes to the instance and database
support files are made:
• If a database is being moved to another instance, create the new instance. The
new instance must be at the same release level as the instance where the
database currently resides.
• If the new instance has a different owner than the current instance, grant access
to the new instance owner.
• Copy the files and devices belonging to the databases being copied onto the
system where the new instance resides. The path names must be changed as
necessary. However, if there are already databases in the directory where the
database files are moved to, you can mistakenly overwrite the existing sqldbdir
file, thereby removing the references to the existing databases. In this scenario,
the db2relocatedb utility cannot be used. Instead of db2relocatedb, an alternative
is a redirected restore operation.
• Change the permission of the files/devices that were copied so that they are
owned by the instance owner.
When moving a database from a database path where more than one database
resides, the sqldbdir directory within that database path must be copied and not
moved. This directory is still needed in the old location for Db2 to locate the
databases that are not moving. After copying the sqldbdir directory to the new
location, a LIST DB DIRECTORY ON newPath command lists databases that were
not moved. These references cannot be removed and new databases with those
names cannot be created on this same path. However, databases can be created
with those names on a different path.
The db2relocatedb command cannot be used to move existing user created
containers for a table space that was converted to use automatic storage using the
ALTER TABLESPACE MANAGED BY AUTOMATIC STORAGE statement.
If the instance is changing, the command must be run by the new instance owner.

© Copyright IBM Corp. 1997, 2018 5-35


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Changing log-related options using db2relocatedb


• You can specify additional keywords in the db2relocatedb command
configuration file that make it easier to relocate a database when the
log-related paths used are different:
 FAILARCHIVE_PATH=newDirPath
 LOGARCHMETH1=newDirPath
 LOGARCHMETH2=newDirPath
 MIRRORLOG_PATH=newDirPath
 OVERFLOWLOG_PATH=newDirPath
• You should only specify these fields if the database being relocated
has the matching configuration parameter set
• Note, the old path name is not specified for these

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Changing log-related options using db2relocatedb


You can specify additional keywords in the db2relocatedb command configuration file
that make it easier to relocate a database when the paths used are different.
The db2relocatedb configuration file can contain new values for the mirrorlogpath,
failarchivepath, logarchmeth1, logarchmeth2, and overflowlogpath database
configuration parameters.
When you run the db2relocatedb command, the database configuration parameters of
the relocated database are updated with the values specified in the configuration file. If
you do not specify any of the new keywords, the relocated database maintains the
original parameters values.

© Copyright IBM Corp. 1997, 2018 5-36


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Using db2relocatedb after copying containers


DB_NAME=prod
db2relocatedb -f newts DB_PATH=/db

Instance inst1 INSTANCE=inst1


CONT_PATH= /dbvol2/DB1/dms01, /dbvol3/DB1/dms1a
DB Directory
DB CFG
Log Control File /db/inst1/NODE0000/SQL00001
Recovery History
Table space files

/dbauto/path1/inst1/NODE0000/PROD/T0000000/C0000000.CAT
SYSCATSPACE

Db2 Database
/dbauto/path1/inst1/NODE0000/PROD/T0000001/C0000000.TMP
PROD TEMPSPACE1

/dbvol2/DB1/dms01
Table space Table space /dbvol3/DB1/dms1a
DMS01 DMS01

Active Logs /dblogs/DB1/logs

Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Using db2relocatedb after copying containers


The db2relocatedb command can be used to update a database configuration when
table space containers are moved using operating system file or disk volume copies.
For example:
The database PROD exists in the instance inst1 on the path /db.
The location of the table space container need to be changed as follows:
The DMS container /dbvol2/DB1/dms01 needs to be moved to
/dbvol3/DB1/dms1a
After the files has been moved to the new locations, the following configuration file
can be used with the db2relocatedb command to make changes to the database
files so that they recognize the new locations:
DB_NAME=prod
DB_PATH=/db
INSTANCE=inst1
CONT_PATH=/dbvol2/DB1/dms01,/dbvol3/DB1/dms1a

© Copyright IBM Corp. 1997, 2018 5-37


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Copying a Db2 database to a new instance

Instance inst1 Database


/dbprd/inst1/NODE0000/SQL00001
/dbprd/DB1/dms01
Directory
IBMSTOGROUP
SYSCATSPACE APSTOGRP1
Db2 Database Table space
TEMPSPACE1 TSAP1 DMS01
PROD TSAP2
/dbprd/sysgrp
/dbprd/apgrp1
Active Logs /dblogs/DB1/logs

/dbtest/TEST/logs APSTOGRP1
Active Logs
IBMSTOGROUP TSAP1
SYSCATSPACE TSAP2
Db2 Database TEMPSPACE1
/dbtest/apgrp1 Table space
TEST
/dbtest/sysgrp DMS01

Database /tsdb/TEST/dms01
Directory
Instance inst2 /tsdb/inst2/NODE0000/SQL00001
Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Copying a Db2 database to a new instance


The db2relocatedb command can be used as part of copying a database from one
instance to another using non-Db2 utilities, like operating system file copy
commands.
For example:
• The database PROD exists in the instance inst1 and was created on the path
/dbprd. The database has two automatic storage groups defined,
IBMSTOPGROUP which uses the path /dbprd/sysgrp, APSTOGROUP which
uses the path /dbprd/apgrp1. A DMS managed table space uses the container
/dbprd/DB1/dms01.
• The database PROD is to be moved to a new system with a new database name
of TEST. The instance on the new system will be inst2 and the location of the
database path will be /tsdb.
• The TEST database needs two automatic storage groups defined,
IBMSTOPGROUP which will use the path /dbtest/sysgrp, APSTOGROUP will
uses the path /dbtest/apgrp1. The DMS managed table space DMS01 will use
the container /tsdb/TEST/dms01.
When moving the database, care must be taken to locate all of the database related
files in the paths planned.

© Copyright IBM Corp. 1997, 2018 5-38


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Run db2relocatedb in new instance after copy

Instance inst1 Instance inst2


DB Directory DB Directory

SYSCATSPACE SYSCATSPACE

TEMPSPACE1 TEMPSPACE1
Db2 Db2
Database Active Logs
Active Logs Database
PROD TEST
TSAP1 STOGROUP STOGROUP TSAP1
TSAP2 APSTOGRP1 APSTOGRP1 TSAP2

Table space
Table space
DMS01
DMS01

DB_NAME=PROD,TEST
db2relocatedb -f newdb DB_PATH=/dbprd,/tsdb
INSTANCE=inst1,inst2
LOG_DIR=/dblogs/PROD/logs,/dbtest/TEST/logs
CONT_PATH=/dbprd/DB1/dms01,/tsdb/TEST/dms01
STORAGE_PATH=/dbprd/sysgrp,/dbtest/sysgrp
STORAGE_PATH=/dbprd/apgrp1,/dbtest/apgrp1
Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Run db2relocatedb in new instance after copy


After the physical directories and files have been copied to their new locations, the
following configuration file can be used with db2relocatedb to make changes to the
database files so that they recognize the new locations:
DB_NAME=PROD,TEST
DB_PATH=/dbprd,/tsdb
INSTANCE=inst1,inst2
LOG_DIR=/dblogs/PROD/logs,/dbtest/TEST/logs
CONT_PATH=/dbprd/DB1/dms01,/tsdb/TEST/dms01
STORAGE_PATH=/dbprd/sysgrp,/dbtest/sysgrp
STORAGE_PATH=/dbprd/apgrp1,/dbtest/apgrp1

© Copyright IBM Corp. 1997, 2018 5-39


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Demonstration 1
Db2 tablespace relocation and transportation

• Plan and execute a redirected restore using Db2 commands


• Use the SET TABLESPACE CONTAINERS command to define new
containers for a table space
• Covert DMS managed tablespaces to use Automatic Storage
management with a redirected restore.
• Use the LIST TABLESPACE command to check table space and
container status
• Utilize the db2relocatedb command to change the name of a container
for a table space
• Execute a redirected restore to transport a schema of database objects
and the set of tablespaces used by those objects into a different
database
Db2 database and tablespace relocation © Copyright IBM Corporation 2017

Demonstration 1: Db2 tablespace relocation and transportation

© Copyright IBM Corp. 1997, 2018 5-40


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Demonstration 1:
Db2 tablespace relocation and transportation

Purpose:
This demonstration will allow you how to perform several redirected restores.
One DMS managed table space will be restored to a different container.
Several DMS managed tablespaces will be converted to use automatic storage
management. You will utilize the TRANSPORT option of RESTORE to copy a
schema of objects in a set of table spaces to a second database. The
db2relocatedb command will be used to rename a container.

Task 1. Use db2relocatedb to change the name of a


tablespace container.
Important Note: This demonstration uses the database named TP1 loaded with test
tables in the Db2 instance inst464. You need to utilize the Linux system login to the
instance owner id: inst464.
1. Logon to the Linux system using the user id inst464, with a password of
ibm2blue.
You will create a set of test objects in three new tablespaces. Use the command files
testspace.ddl to create the new tablespaces. The tablespace TESTSMALL is defined
using automatic storage in the storage group TP1SG1. The other two tablespaces,
TESTHIST1 and TESTHIST2 are DMS managed tablespaces. If the current container
is renamed using the UNIX mv command, the db2relocatedb command can be used
to update the table space container definition for the TP1 database to reflect the change
made outside of Db2's control. This must be done with the database stopped.
You will use the Linux Terminal session to enter Db2 commands.
2. Right-click the empty Linux desktop and select Open in Terminal.

© Copyright IBM Corp. 1997, 2018 5-41


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

3. Enter the following commands in the Linux terminal session:


• cd $HOME/bin
• db2 connect to tp1
• db2 -tvf testspace.ddl
The output from this command will look similar to the following:
CREATE TABLESPACE TESTSMALL USING STOGROUP TP1SG1 INITIALSIZE 4M EXTENTSIZE 4
DB20000I The SQL command completed successfully.

CREATE TABLESPACE TESTHIST1 MANAGED BY DATABASE USING (FILE


'/database/inst464/testhist1' 5000) EXTENTSIZE 64 PREFETCHSIZE 64 autoresize yes
DB20000I The SQL command completed successfully.

CREATE TABLESPACE TESTHIST2 MANAGED BY DATABASE USING (FILE


'/database/inst464/testhist2' 5000) EXTENTSIZE 64 PREFETCHSIZE 64 autoresize yes
DB20000I The SQL command completed successfully.

CONNECT RESET
DB20000I The SQL command completed successfully.

TERMINATE
DB20000I The TERMINATE command completed successfully.
Next you will create a set of objects using the schema TEST in the three
tablespaces and load data into the tables. The file testtables.ddl contains the
necessary SQL.
4. Enter the following commands in the Linux terminal session:
• db2 connect to tp1
• db2 -tvf testtables.ddl
The output from this command will look similar to the following:
CREATE TABLE TEST.BRANCH (BRANCH_ID SMALLINT NOT NULL primary key,
BRANCH_NAME CHAR(20) NOT NULL, BALANCE DECIMAL(15,2) NOT
NULL, AREA_CODE CHAR(4) NOT NULL, ADDRESS CHAR(30)
NOT NULL, TEMP CHAR(40) NOT NULL) IN TESTSMALL
DB20000I The SQL command completed successfully.

CREATE TABLE TEST.TELLER (TELLER_ID SMALLINT NOT NULL primary key,


TELLER_NAME CHAR(20) NOT NULL, BRANCH_ID SMALLINT NOT
NULL, BALANCE DECIMAL(15,2) NOT NULL, TELLER_CODE CHAR(2)
NOT NULL, ADDRESS CHAR(30) NOT NULL, TEMP CHAR(40)
NOT NULL) IN TESTSMALL
DB20000I The SQL command completed successfully.

CREATE TABLE TEST.HISTORY1 (ACCT_ID INTEGER NOT NULL, TELLER_ID


SMALLINT NOT NULL, BRANCH_ID SMALLINT NOT NULL, BALANCE
DECIMAL(15,2) NOT NULL, DELTA DECIMAL(9,2) NOT NULL, PID

© Copyright IBM Corp. 1997, 2018 5-42


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

INTEGER NOT NULL, TSTMP TIMESTAMP NOT NULL WITH DEFAULT,


ACCTNAME CHAR(20) NOT NULL, TEMP CHAR(6) NOT
NULL) IN TESTHIST1
DB20000I The SQL command completed successfully.

CREATE INDEX TEST.H1INDEX ON TEST.HISTORY1 (ACCT_ID)


DB20000I The SQL command completed successfully.

CREATE TABLE TEST.HISTORY2 (ACCT_ID INTEGER NOT NULL, TELLER_ID


SMALLINT NOT NULL, BRANCH_ID SMALLINT NOT NULL, BALANCE
DECIMAL(15,2) NOT NULL, DELTA DECIMAL(9,2) NOT NULL, PID
INTEGER NOT NULL, TSTMP TIMESTAMP NOT NULL WITH DEFAULT,
ACCTNAME CHAR(20) NOT NULL, TEMP CHAR(6) NOT
NULL) IN TESTHIST2
DB20000I The SQL command completed successfully.

CREATE INDEX TEST.H2INDEX ON TEST.HISTORY2 (ACCT_ID)


DB20000I The SQL command completed successfully.
The file loadtest.ddl will be used to load data into the test tables.
5. Enter the following commands in the Linux terminal session:
• db2 connect to tp1
• db2 -tvf loadtest.ddl
The output from this command will look similar to the following:
load from hist200k.del of del messages ldhtest1.msg replace into test.history1
statistics yes copy no

Number of rows read = 200000


Number of rows skipped = 0
Number of rows loaded = 200000
Number of rows rejected = 0
Number of rows deleted = 0
Number of rows committed = 200000

SQL3107W At least one warning message was encountered during LOAD processing.

load from hist200k.del of del messages ldhtest1.msg replace into test.history2


statistics yes copy no

Number of rows read = 200000


Number of rows skipped = 0
Number of rows loaded = 200000
Number of rows rejected = 0
Number of rows deleted = 0
Number of rows committed = 200000

SQL3107W At least one warning message was encountered during LOAD processing.

© Copyright IBM Corp. 1997, 2018 5-43


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

INSERT INTO TEST.BRANCH SELECT * FROM inst464.BRANCH


DB20000I The SQL command completed successfully.

INSERT INTO TEST.TELLER SELECT * FROM inst464.TELLER


DB20000I The SQL command completed successfully.
Use the SQL query in the file testcontainers.sql to list the table space
containers for the three new tablespaces.
6. Enter the following commands in the Linux terminal session:
• db2 connect to tp1
• db2 -tvf testcontainers.sql
The output from this command will look similar to the following:
select tbsp_id as TS_ID,
substr(tbsp_name,1,20) as TS_name ,
container_type , substr(container_name,1,80) as container_name
, total_pages
from table(mon_get_container(null,-2)) as cont
where tbsp_name like 'TEST%'
order by tbsp_id, container_id

TS_ID TS_NAME CONTAINER_TYPE CONTAINER_NAME


TOTAL_PAGES
--------- ----------- -------------------- ---------------- ----------------------
------------------------ ------------
8 TESTSMALL FILE_EXTENT_TAG
/dbauto/path2/inst464/NODE0000/TP1/T0000008/C0000000.LRG 1024
9 TESTHIST1 FILE_EXTENT_TAG /database/inst464/testhist1
5000
10 TESTHIST2 FILE_EXTENT_TAG /database/inst464/testhist2
5000

Create a database level offline backup before you begin to make changes to the
tablespace containers.

© Copyright IBM Corp. 1997, 2018 5-44


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

7. Enter the following commands in the Linux terminal session:


• db2 terminate
• db2 backup db tp1 to $HOME/backups/u5 compress
The db2relocatedb command can be used to update the table space container
definition for the TP1 database to reflect the change made outside of Db2's
control. This must be done with the database stopped. You will rename the
container for the DMS managed tablespace TESTHIST2, then use
db2relocatedb to reflect the name change in the database control files.
You can use the db2relocatedb generate option to create a database
configuration file for a Db2 database.
8. Enter the following commands in the Linux terminal session:
• db2relocatedb -g tp1config -d tp1
• cat tp1config
The generated configuration looks similar to the following:
DB_NAME=tp1,tp1
INSTANCE=inst464,inst464
DB_PATH=/database,/database
NODENUM=0
STORAGE_PATH=/dbauto/path1,/dbauto/path1
STORAGE_PATH=/dbauto/path2,/dbauto/path2
CONT_PATH=/database/inst464/NODE0000/SQL00001/tp1hist,/database/inst464/NODE0000/S
QL00001/tp1hist
CONT_PATH=/database/inst464/NODE0000/SQL00001/tp1acctd,/database/inst464/NODE0000/
SQL00001/tp1acctd
CONT_PATH=/database/inst464/testhist1,/database/inst464/testhist1
CONT_PATH=/database/inst464/testhist2,/database/inst464/testhist2
LOG_DIR=/dblogs/inst464/tp1/,/dblogs/inst464/tp1/
LOGARCHMETH1=DISK:/home/inst464/archive/,DISK:/home/inst464/archive/
9. Enter the following commands in the Linux terminal session:
• db2stop force
• cd /database/inst464
• mv testhist2 testhist2_new
• cd $HOME/bin
The file relocatedms.ddl which will be used to alter the TESTHIST2 container
name in the Db2 table space definition files.

© Copyright IBM Corp. 1997, 2018 5-45


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

10. Enter the following commands in the Linux terminal session:


• cat relocatedms.ddl
The generated configuration will similar to the following:
DB_NAME=TP1
DB_PATH=/database
INSTANCE=inst464
CONT_PATH=/database/inst464/testhist2,/database/inst464/testhist2_new
11. Enter the following command in the Linux terminal session:
• db2relocatedb -f relocatedms.ddl
The output from this command will look similar to the following:
Files and control structures were changed successfully.
DBT1000I The tool completed successfully.
Restart the instance and connect to the TP1 database to check the new
container definition. Use the SQL query in the file testcontainers.sql to list the
table space containers.
12. Enter the following commands in the Linux terminal session:
• db2start
• db2 connect to tp1
• db2 -tf testcontainers.sql
The output from this SQL will look similar to the following:
TS_ID TS_NAME CONTAINER_TYPE CONTAINER_NAME
TOTAL_PAGES
--------- ----------- -------------------- ---------------- ----------------------
------------------------ ------------
8 TESTSMALL FILE_EXTENT_TAG
/dbauto/path2/inst464/NODE0000/TP1/T0000008/C0000000.LRG 1024
9 TESTHIST1 FILE_EXTENT_TAG /database/inst464/testhist1
5000
10 TESTHIST2 FILE_EXTENT_TAG /database/inst464/testhist2_new
5000
To verify access to the manually moved tablespace container, run the
RUNSTATS command for the Db2 table TEST.HISTORY2
13. Enter the following commands in the Linux terminal session:
• db2 connect to tp1
• db2 runstats on table test.history2 and indexes all
• db2 terminate

© Copyright IBM Corp. 1997, 2018 5-46


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Task 2. Change the container for a DMS table space and


convert a DMS table space using a redirected restore.
You can use the full database backup to perform a redirected restore of the two DMS
managed tablespaces TESTHIST1 and TESTHIST2. The tablespace TESTHIST1 will
be converted to use Automatic Storage management. The tablespace TESTHIST2 will
be assigned a new container name. These changes can be performed using online
tablespace restores. First activate the database TP1.
1. In the terminal session, issue the following command:
• db2 activate db tp1
In order to change the container used for the TESTHIST2 table space using a
redirected restore, you need to get information on the current container for that
table space. Use the GENERATE SCRIPT option of the RESTORE command
to create a script for the redirected restore of the TESTHIST1 and TESTHIST2
table spaces.
2. Enter the following commands in the Linux terminal session:
• db2 " restore db tp1 tablespace(testhist1,testhist2) online from
$HOME/backups/u5 redirect generate script redirect.cmd "
• cat redirect.cmd
The output will include a complete sequence of commands that can be used to
perform a redirected restore. The script will look similar to the following:
-- *****************************************************************************
-- ** automatically created redirect restore script
-- *****************************************************************************
UPDATE COMMAND OPTIONS USING S ON Z ON TP1_NODE0000.out V ON;
SET CLIENT ATTACH_MEMBER 0;
SET CLIENT CONNECT_MEMBER 0;
-- *****************************************************************************
-- ** automatically created redirect restore script
-- *****************************************************************************
RESTORE DATABASE TP1
-- USER <username>
-- USING '<password>'
TABLESPACE (
"TESTHIST1"
, "TESTHIST2"
)
ONLINE
FROM '/home/inst464/backups/u5'
TAKEN AT 20161101133236
-- ON '/dbauto/path1'

© Copyright IBM Corp. 1997, 2018 5-47


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

-- DBPATH ON '<target-directory>'
INTO TP1
-- NEWLOGPATH '/dblogs/inst464/tp1/NODE0000/LOGSTREAM0000/'
-- 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
;
-- *****************************************************************************
-- ** storage group definition
-- ** Default storage group ID = 0
-- ** Number of storage groups = 2
-- *****************************************************************************
-- *****************************************************************************
-- ** Storage group name = IBMSTOGROUP
-- ** Storage group ID = 0
-- ** Data tag = None
-- *****************************************************************************
-- SET STOGROUP PATHS FOR IBMSTOGROUP
-- ON '/dbauto/path1'
-- ;
-- *****************************************************************************
-- ** Storage group name = TP1SG1
-- ** Storage group ID = 1
-- ** Data tag = None
-- *****************************************************************************
-- SET STOGROUP PATHS FOR TP1SG1
-- ON '/dbauto/path2'
-- ;
-- *****************************************************************************
-- ** table space definition
-- *****************************************************************************
-- *****************************************************************************

© Copyright IBM Corp. 1997, 2018 5-48


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

-- ** Tablespace name = TESTHIST1


-- ** Tablespace ID = 9
-- ** Tablespace Type = Database managed space
-- ** Tablespace Content Type = All permanent data. Large table
space.
-- ** Tablespace Page size (bytes) = 4096
-- ** Tablespace Extent size (pages) = 64
-- ** Using automatic storage = No
-- ** Storage group ID = -1
-- ** Source storage group ID = -1
-- ** Data tag = None
-- ** Auto-resize enabled = Yes
-- ** Total number of pages = 5000
-- ** Number of usable pages = 4928
-- ** High water mark (pages) = 4672
-- *****************************************************************************
SET TABLESPACE CONTAINERS FOR 9
-- IGNORE ROLLFORWARD CONTAINER OPERATIONS
USING (
FILE '/database/inst464/testhist1' 5000
);
-- *****************************************************************************
-- ** Tablespace name = TESTHIST2
-- ** Tablespace ID = 10
-- ** Tablespace Type = Database managed space
-- ** Tablespace Content Type = All permanent data. Large table
space.
-- ** Tablespace Page size (bytes) = 4096
-- ** Tablespace Extent size (pages) = 64
-- ** Using automatic storage = No
-- ** Storage group ID = -1
-- ** Source storage group ID = -1
-- ** Data tag = None
-- ** Auto-resize enabled = Yes
-- ** Total number of pages = 5000
-- ** Number of usable pages = 4928
-- ** High water mark (pages) = 4672
-- *****************************************************************************
SET TABLESPACE CONTAINERS FOR 10
-- IGNORE ROLLFORWARD CONTAINER OPERATIONS
USING (
FILE '/database/inst464/testhist2' 5000
);

© Copyright IBM Corp. 1997, 2018 5-49


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

-- *****************************************************************************
-- ** start redirected restore
-- *****************************************************************************
RESTORE DATABASE TP1 CONTINUE;
-- *****************************************************************************
-- ** end of file
-- *****************************************************************************
During the redirected restore, the container for the TESTHIST2 table space will
be changed to a file named /database/inst464/testhist2_2. The size of the new
container will be the same as the original container. The TESTHIST1 table
space will be converted to use automatic storage.
First, restore the TESTHIST1 and TESTHIST2 table spaces using the
REDIRECT option to allow definition of new containers. Run the Db2
RESTORE command to restore the table space from backup in the
$HOME/backups/u5 directory
3. Enter the following command in the Linux terminal session:
• db2 " restore db tp1 tablespace(testhist1,testhist2) online from
$HOME/backups/u5 redirect "
The command should complete with a message:
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.
Check the table space status for TESTHIST1 and TESTHIST2 using the LIST
TABLESPACES command. Note the tablespace ID numbers.

© Copyright IBM Corp. 1997, 2018 5-50


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

4. Enter the following command in the Linux terminal session:


• db2 LIST TABLESPACES | more
The output will include information similar to the following:

Tablespace ID = 9
Name = TESTHIST1
Type = Database managed space
Contents = All permanent data. Large table space.
State = 0x2002100
Detailed explanation:
Restore pending
Restore in progress
Storage may be defined

Tablespace ID = 10
Name = TESTHIST2
Type = Database managed space
Contents = All permanent data. Large table space.
State = 0x2003100
Detailed explanation:
Restore pending
Storage must be defined
Restore in progress
Storage may be defined
Convert the tablespace TESTHIST1 to utilize automatic storage management
using the SET TABLESPACE CONTAINER command.
5. Enter the following command in the Linux terminal session:
• db2 " SET TABLESPACE CONTAINERS FOR 9 USING automatic
storage "
Note that there is no option in SET TABLESPACE CONTAINERS to assign a
storage group, so this tablespace will utilize the default storage group,
IBMSTOGROUP.
List the new container information for the TESTHIST1 table space using a LIST
TABLESPACE CONTAINERS command.

© Copyright IBM Corp. 1997, 2018 5-51


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

6. Enter the following command in the Linux terminal session:


• db2 LIST TABLESPACE CONTAINERS FOR 9 SHOW DETAIL
The output will include information similar to the following:

Tablespace Containers for Tablespace 9

Container ID = 0
Name =
/dbauto/path1/inst464/NODE0000/TP1/T0000009/C0000000.LRG
Type = File
Total pages = 5056
Useable pages = 4992
Accessible = Yes
Define a new container for the DMS managed, TESTHIST2 table space using
the SET TABLESPACE CONTAINER command.
7. Enter the following command in the Linux terminal session:
• db2 " set tablespace containers for 10 using (file
'/database/inst464/testhist2_2' 5000) "
List the new container information for the TESTHIST2 table space using a LIST
TABLESPACE CONTAINERS command.
8. Enter the following command in the Linux terminal session:
• db2 LIST TABLESPACE CONTAINERS FOR 10 SHOW DETAIL
The output will include information similar to the following:
Tablespace Containers for Tablespace 10

Container ID = 0
Name = /database/inst464/testhist2_2
Type = File
Total pages = 5000
Useable pages = 4928
Accessible = Yes
Complete the restore process by issuing the RESTORE command with the
CONTINUE option and roll forward the table space to the end of the logs.

© Copyright IBM Corp. 1997, 2018 5-52


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

9. Enter the following commands in the Linux terminal session:


• db2 restore database TP1 continue
• db2 rollforward database tp1 to end of logs tablespace online
The output will include information similar to the following:
Rollforward Status

Input database alias = tp1


Number of members have returned status = 1

Member ID = 0
Rollforward status = not pending
Next log file to be read =
Log files processed = -
Last committed transaction = 2014-05-15-13.57.30.000000 UTC

DB20000I The ROLLFORWARD command completed successfully.


Use the SQL in the file testcontainers.sql to show the current tablespace
containers assigned to the two tablespaces, TESTHIST1 and TESTHIST2.
10. Enter the following commands in the Linux terminal session:
• db2 connect to tp1
• db2 -tf testcontainers.sql
The output will include information similar to the following:
TS_ID TS_NAME CONTAINER_TYPE CONTAINER_NAME
TOTAL_PAGES
--------- ----------- -------------------- ---------------- ----------------------
------------------ ----------
8 TESTSMALL FILE_EXTENT_TAG
/dbauto/path2/inst464/NODE0000/TP1/T0000008/C0000000.LRG 1024
9 TESTHIST1 FILE_EXTENT_TAG
/dbauto/path1/inst464/NODE0000/TP1/T0000009/C0000000.LRG 5056
10 TESTHIST2 FILE_EXTENT_TAG /database/inst464/testhist2_2
5000

3 record(s) selected.

• db2 terminate
• db2 deactivate db tp1

© Copyright IBM Corp. 1997, 2018 5-53


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Task 3. Use the TRANSPORT option of RESTORE to copy the


tablespaces used for a schema of objects into a test
database.
The objects created for the schema TEST reside in three distinct tablespaces,
TESTSMALL, TESTHIST1 and TESTHIST2. Create a new offline database backup of
the TP1 database. You can use the TRANSPORT option of RESTORE to copy those
tablespaces and the objects into a different database. You will create a database
named TEST to be the target database for the TRANSPORT.
1. In the terminal session, issue the following commands:
• db2 terminate
• rm $HOME/backups/u5/*
• db2 backup db tp1 to $HOME/backups/u5 compress
• db2 create database test on /database (this may take a few minutes)
Use the TRANSPORT option of RESTORES to copy the three tablespaces,
TESTSMALL, TESTHIST1 and TESTHIST2 from the database backup image
into the database named TEST. You will use a redirected RESTORE to be able
to change the container assignments for the DMS managed tablespaces.
2. Enter the following command in the Linux terminal session:
• db2 " restore db tp1 tablespace(testsmall,testhist1,testhist2)
schema(test) from /home/inst464/backups/u5 transport into test
redirect "
The output will include information similar to the following:
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.
You will convert both DMS managed tablespaces TESTHIST1 and TESTHIST2
to automatic storage management.
3. Enter the following commands in the Linux terminal session:
• db2 set tablespace containers for 9 using automatic storage
• db2 set tablespace containers for 10 using automatic storage
Complete the RESTORE processing now that all of the tablespaces have
containers defined.

© Copyright IBM Corp. 1997, 2018 5-54


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

4. Enter the following command in the Linux terminal session:


• db2 restore db tp1 continue
No ROLLFORWARD command is required. If an online backup image was
utilized, Db2 would use the included logs to make the restored tablespaces
consistent.
Connect to the TEST database and use the SQL statement in the file
testcontainers.sql to check the container assignments for the transported
copies of the tablespaces.
5. Enter the following commands in the Linux terminal session:
• db2 connect to test
• db2 -tvf testcontainers.sql
The output will include information similar to the following:
TS_ID TS_NAME CONTAINER_TYPE CONTAINER_NAME
TOTAL_PAGES
--------- ----------- -------------------- ---------------- ----------------------
------------------ ----------
3 TESTSMALL FILE_EXTENT_TAG
/database/inst464/NODE0000/TEST/T0000003/C0000000.LRG 1024
4 TESTHIST1 FILE_EXTENT_TAG
/database/inst464/NODE0000/TEST/T0000004/C0000000.LRG 5056
5 TESTHIST2 FILE_EXTENT_TAG
/database/inst464/NODE0000/TEST/T0000005/C0000000.LRG 5056

3 record(s) selected.
The tablespaces are all defined using automatic storage management. The
tablespace ID numbers are different in the target database than those in the
source database.
Use the command file unit5_end.cmd to drop the tablespaces used for the
TEST objects in the TP1 database and to drop the TEST database.
6. Enter the following commands in the Linux terminal session:
• db2 terminate
• db2 deactivate db tp1
• unit5_end.cmd
7. Close the terminal and then logout of the Linux operating system
Results:
You used db2relocatedb to rename a table space container. You made table
space container changes, including converting DMS table spaces to use
Automatic Storage management during a redirected restore. You utilized the
TRANSPORT option of RESTORE to copy several table spaces and the
objects for a schema to a different database.

© Copyright IBM Corp. 1997, 2018 5-55


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit 5 Db2 database and tablespace relocation

Unit summary
• Explain the facility of the Db2 RESTORE command to recover table
spaces to different containers
• Use the SET TABLESPACE CONTAINERS command to define new
containers during a redirected restore
• Utilize the SET STOGROUP PATHS command to change the storage
paths for automatic storage tablespaces in storage groups
• Plan the use of redirected restore as part of a disaster recovery
• Describe two methods that can be used to convert a DMS table space to
utilize Automatic Storage
• Use the GENERATE SCRIPT option of RESTORE to set up a command
script for a redirected restore operation
• Copy schemas from one database to another using the TRANSPORT
option of the RESTORE utility
• Use db2relocatedb when moving or copying Db2 databases with non-Db2
utilities
Db2 database and tablespace relocation © Copyright IBM Corporation 2018

Unit summary

© Copyright IBM Corp. 1997, 2018 5-56


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

You might also like