Cloning a Database
Cloning a
database means create an identical copy of a database, either on the same machine as the original, or
on a different one. Additionally, it means changing the database name, and the
Instance name. But those are both optional steps if the clone is being created
on a different machine. Even when it's all happening on the one machine, the
only requirement is to change the Instance name -changing the database name
remains entirely optional.
What
follows is therefore a set of instructions that takes things one-step at a time:
first, the clone. Second the renaming of the Instance. Third (if you really want
to) is the renaming of the Database. You can
do it all in
one go. But, if different machines are involved, there's strictly no requirement
for steps 2 and 3 -so I've kept them separate. It goes without saying (I hope)
that if you are cloning onto a new machine, the Oracle executables and
environment variables (like ORACLE_BASE and ORACLE_HOME) need to bein place
before you even start.
• In
the source database, issue the following command:
ALTER
DATABASE
BACKUP
CONTROLFILE
TO TRACE;
• Perform
a clean shutdown of the source database
• Copy
the source Data Files and Redo Logs to the clone location
• Locate
the Control File trace script made earlier (in your UDUMP directory):
o Clear
out the junk at the top, so that the first line reads STARTUP NOMOUNT.
o Edit
this statement to read:
CREATE
CONTROLFILE SET DATABASE "new_db_name" RESETLOGS ARCHIVELOG
o Add
to the STARTUP
NOMOUNT line a
reference to where the new init.ora is to be located (i.e., the line should read
STARTUP
NOMOUNT PFILE=/SOMEWHERE/INIT<SID>.ORA)
o Also
edit all the file locations mentioned in the trace file so that they point to
appropriate clone locations. For example, when it says LOGFILE GROUP 1
'D:\ODATA\BLAH\LOG1A.RDO’, change that to read
LOGFILE GROUP 1
‘C:\SOMEWHERE_NEW\LOG1A.RDO'.
Do that for all file location references.
• Copy
the init.ora from the source database, and edit it so that it is appropriate for
the Clone. Make sure you edit:
o CONTROL_FILES=
(point to where you want the clone's Control Files
created)
o LOG_ARCHIVE_DEST
(and variants) (the
source archives must not be overwritten by the clone's)
o USER_DUMP_DEST
& BACKGROUND_DUMP_DEST
(source trace files
must not be overwritten by the clone)
• Start
Server Manager or SQL Plus for the clone.
• Run
the Trace File script (ie, type @NAME_OF_SCRIPT).
That will then connect you as per the connect line you added to the script,
startup in the nomount stage, and
issue the 'create
controlfile' commands. At the end of the exercise, your database should be left
in the fully open state, with all the required controlfiles in place.
At
this point, you make sure everything is working perfectly. Once you know it is,
it is time to consider re-naming the Instance and the Database
• Shutdown
the new clone database
• Exit
completely out of SQL Plus or Server Manager (you can't set an environment
variable if you simply shell out of those applications).
• Use
the appropriate O/S command to set a new ORACLE_SID. On Unix, that means type EXPORT
ORACLE_SID=XXX,
and on NT, that means type SET
ORACLE_SID=XXX
• Run
SQL Plus or Server Manager once more, connect as a Privileged User, and issue
the
STARTUP PFILE=/WHEREVER/<NAME OF INIT.ORA> command.
Note
that on NT you'd have to use ORADIM (or, for versions before 8i, ORADIM80 or
ORADIM73) to create a Service for the new Instance name before you can start it
up (you still need to change the ORACLE_SID as well). On Unix, the change of
ORACLE_SID is sufficient in itself.
Also
note that I'm assuming here that you're using Operating System authentication
for the Privileged User. If you've got a Password File instead, you'll need to
copy the Password File from the primary database into the standard default
location, and change its name to be ORAPW<SID>, otherwise you'll never get
authenticated properly.
• While
connected to the Clone database, issue the Alter Database Backup Controlfile to
Trace command once more.
• Perform
a clean Shutdown of the clone. (Shutdown Immediate will do, but don't try a
Shutdown Abort).
• Delete
all clone Control Files
• Locate
the Control File trace script created earlier and make the following amendments
to it:
o strip
out all the rubbish at the top so that the first line reads STARTUP NOMOUNT.
o Add
in a line right at the top which connects as a Privileged UserCONNECT /
AS SYSDBA).
o Add
to the "startup nomount" line a reference to where the new init.ora is
to be located (i.e., the line should read STARTUP NOMOUNT PFILE=/SOMEWHERE/INIT<SID>.ORA).
o Change
the CREATE
CONTROLFILE
REUSE
'X' NORESETLOGS.....
line to read
CREATE
CONTROLFILE
SET 'Y' RESETLOGS.....
In other words, X
is the old
name, and Y is the new
one. And yes, you're going to have to do a Resetlogs after this procedure.
o Finally,
change the line ALTER
DATABASE OPEN to ALTER
DATABASE
OPEN
RESETLOGS.
• Edit
the clone init.ora and change the db_name parameter to match the new database
name
• Start
SQL Plus or Server Manager and run the Control File trace script (by typing @NAME_OF_SCRIPT). As before, the database should eventually be left in
the fully open state. Do a SELECT * FROM V$DATABASE
to check that the
database name really has changed.
• If
you care about your clone, you should now perform a clean shutdown and backup,
because the resetlogs that was issued will have rendered all prior backups and
archives of the primary system completely useless as a way of recovering the
clone.
Note
that where these instructions refer to 'starting SQL Plus or Server Manager',
they are assuming at least an Oracle 8i database. In all versions prior to that,
Server Manager was the only tool able to perform startups and shutdowns.
Another Method
1.
If your database is not in ARCHIVE LOG mode, take an off-line database backup.
If in ARCHIVELOG MODE, you can either take an on-line or off-line database
backup.
2. On your production database, connect to "SYS AS SYSDBA" (internal)
and do a:
SQL> alter database backup controlfile to trace;
3. Edit the controlfile trace (in $ORACLE_BASE/admin/$ORACLE_SID>/udump) and
remove the lines until you get to the 'CREATE CONTROLFILE ..' statement. Edit
this stement to read:
CREATE CONTROLFILE SET DATABASE "new_db_name"
RESETLOGS ARCHIVELOG
4. Move this script to the new machine. Edit this script to update the
directories for the datafiles and redo log files. Also comment out the lines for
"RECOVER ..." and "ALTER DATABASE OPEN ..."
5. Restore/ or copy the backup and the archived logs (if any) to the new
machine.
6. Copy your INIT.ORA file to the new machine. Ensure your controlfile and
archive destinations in ini$ORACLE_SID.ora (or config$ORACLE_SID.ora) are set
properly on the new machine.
7. Set your Oracle Environment by running ". oraenv" on Unix.
8. Start sqlplus, connect SYS AS SYSDBA (internal) and run the create
controlfile script.
9. Perform a database recovery using
RECOVER DATABASE USING BACKUP CONTROLFILE UNTIL CANCEL;
You'll be prompted to supply an archived log file, Restore that file to the
archive log destination (if not already there) and continue. Repeat for all
archived files till you get to the last one. At that point specify 'CANCEL'.
10. Issue the command:
ALTER DATABASE OPEN RESETLOGS;
11. Change the global name of the new database to avoid problems with networked
application and replication:
ALTER DATABASE RENAME GLOBAL_NAME = new_db_name;
12. Include the new database into your backup strategy.
You're all done.