05-DB2 Database and Tablespace Relocation
05-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
Db2 Backup
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.
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
.................................
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
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
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
Container ID = 0
Name = /database/inst491/testhist
Type = File
Total pages = 5000
Useable pages = 4928
Accessible = Yes
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
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.
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.
SYSCATSPACE
/DBfs1/cont1 /DBfs2/cont2 TEMPSPACE1
file file
/DBfs2/cont1
APSTOGRP1
file
APSTOGRP1 TSAP1
TSAP1
TSAP2
TSAP2
• 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.
DMS01 DMS01
DMS01
X X
/DBfs1/cont1 /DBfs1/cont1
/DBfs1/cont1
file file
file
/DBfs1/cont2 /DBfs1/cont2
file file
X
S0000021.log S0000022.log S0000023.log S0000024.log
/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--'
• 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
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.
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
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
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 ……
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
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.
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.
• 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.
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.
/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
/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
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
Demonstration 1
Db2 tablespace relocation and transportation
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.
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.
SQL3107W At least one warning message was encountered during LOAD processing.
SQL3107W At least one warning message was encountered during LOAD processing.
Create a database level offline backup before you begin to make changes to the
tablespace containers.
-- 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
-- *****************************************************************************
-- *****************************************************************************
-- *****************************************************************************
-- ** 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.
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.
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.
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
3 record(s) selected.
• db2 terminate
• db2 deactivate db tp1
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.
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