Copy and Rename an Oracle Database – (without export/import)
by Stefan M. Dohn, Sr. Database Administrator
You may find it necessary to duplicate (clone) an Oracle database. One method is to use import/export. This method can work fine, but what if your database is too big? Another method is to make a copy of the current database and rename it. This month I will present an article that explains the steps necessary to copy and rename a database. This article will assume that the original database is called PROD and you want to create a TEST duplicate database.
1.) Copy production database files and init.ora
The first step is to locate and copy all database files to their new location. You can use the view V$DATAFILE in the PROD database to locate these files. Before running the query from V$DATAFILE, ensure that you are connected to the PROD database by selecting from V$DATABASE:
SQL> select name from v$database;
SQL> select name from v$datafile;
After recording these files, shutdown the PROD database and perform an operating system copy of all database files to another location and/or machine. In my example, I will copy all datafiles to a new location as shown in the following table:
|Old Location||New Location|
After copying all files to their new location, startup the PROD database.
From the production database, get a copy of the initPROD.ora file and copy it to initTEST.ora. In the initTEST.ora file, change the value of "db_name" from PROD to TEST. Keep in mind that you may also need to change:
If the TEST database is going to be on a different machine, copy the initTEST.ora file to that machine in the proper directory.
2.) Create the script that will re-create the controlfile
Using SVRMGR on the PROD database, create a script that will be able to re-create the controlfile for the database.
PROD on testdb: svrmgrl
SVRMGR> connect internal
SVRMGR> alter database backup controlfile to trace;
The above statement will put a text copy of the controlfile in the USER_DUMP_DEST directory. You will need to search for the newest trace file in this directory. In UNIX you can use the "ls -lt" command. Once you find the correct trace file, rename it to cr_control.sql and edit it as follows:
- Remove everything up to the "START NOMOUNT" statement and everything after the semicolon at the end of the "CREATE CONTROLFILE" statement.
- Edit the line starting with "CREATE CONTROLFILE" and replace the word "REUSE" with the word "SET" right before the keyword DATABASE.
- On the same line, modify the database name changing it from PROD to TEST.
- On the same line, change the keyword NORESETLOGS to RESETLOGS.
Your script should now read:
|Edited file cr_control.sql|
CREATE CONTROLFILE SET DATABASE "TEST" RESETLOGS NOARCHIVELOG
GROUP 1 (
) SIZE 200K,
GROUP 2 (
) SIZE 200K,
GROUP 3 (
) SIZE 200K
If the TEST database is on a different machine move this file to that machine.
3.) Create the new controlfile for TEST
Make sure that your Oracle environment variable "ORACLE_SID" is set to TEST. (i.e. export ORACLE_SID=TEST).
Now use SVRMGR and the CREATE CONTROLFILE script (cr_control.sql) to create your controlfile for TEST:
TEST on testdb: svrmgrl
SVRMGR> connect internal
Connected to an idle instance.
ORACLE instance started.
Total System Global Area 32798752 bytes
Fixed Size 39816 bytes
Variable Size 22600856 bytes
Database Buffers 9994240 bytes
Redo Buffers 163840 bytes
NOTE: Stay logged into SVRMGR and proceed to the next step.
4.) Open the TEST database
Before opening the TEST database, you will need to perform incomplete recovery. After recovery you can open the database using the RESETLOGS option as show below:
SVRMGR> alter database recover database until cancel using backup controlfile;
SVRMGR> alter database recover cancel;
SVRMGR> alter database open resetlogs;
You can verify that the database was renamed to TEST by querying from V$DATABASE:
SVRMGR> select name from v$database;
1 row selected.