Cloning Oracle databases with RMAN
Some of the hats I currently wear include being the backup administrator and sometime DBA. The two overlap, since I use IBM’s Tivoli Storage Manager (TSM) to handle all of our Oracle-related backups and restores. By restoring backups of a production environment to a nonproduction one (eg, a development or testing instance), you can make a copy or clone of the production environment. In this post, I’ll detail how to clone a target Oracle database to an auxiliary one, named prod and dev, respectively.
The basic steps of making a clone are:
- Configure parameter files
- Backup target database
- Prep dev database
- Restore backup to dev database
Step 1: Configuring the parameter files
Edit the following files for the dev instance:
$ORACLE_HOME/dbs/dev_devserver_ifile.ora
If the pathnames differ between the prod and dev instances, you will need to set these in an ifile (referenced by the init_dev.ora file).
instance_name = dev
log_archive_dest_1 = ‘LOCATION=/u03/oracle/dev/devarch’
log_archive_format=’%t_%s_%r.dbf’db_file_name_convert =
(“/u02/oracle/prod/proddata”, “/u02/oracle/dev/devdata”,
“/u03/oracle/prod/prodarch”, “/u03/oracle/dev/devarch”)log_file_name_convert =
(“/u02/oracle/prod/proddata”, “/u02/oracle/dev/devdata”,
“/u03/oracle/prod/prodarch”, “/u03/oracle/dev/devarch”)
$TNS_ADMIN/tnsnames.ora
Since RMAN on the dev instance will need to connect to the prod database, you will need an entry for the prod database in the tnsnames.ora file on dev. Here is an example:
prod= (DESCRIPTION= (ADDRESS=(PROTOCOL=tcp)(HOST=prodserver.bigcorp.com)(PORT=1521)) (CONNECT_DATA= (SID=prod) ) )
Use the tnsping utility to test connectivity from dev to the prod instance.
Step 2: Backing up the target database
Here’s a script that can backup your production database:
#!/bin/bash
export ORACLE_HOME=/u01/oracle/prod/proddb/10.2.0.3
export ORACLE_SID=prod
$ORACLE_HOME/bin/rman target / << END_OF_RMANrun {
ALLOCATE AUXILIARY CHANNEL t1 DEVICE TYPE sbt_tape parms
‘ENV=(TDPO_OPTFILE=/path/to/bin/tdpo.opt)’;backup database;
delete noprompt obsolete device type sbt_tape;
release channel t1;
}
END_OF_RMAN
Please note that you must also backup your archivelogs, since your database will need to be rolled forward to a consistent state. Again, another script:
#!/bin/bash
export ORACLE_HOME=/u01/oracle/prod/proddb/10.2.0.3
export ORACLE_SID=prod
$ORACLE_HOME/bin/rman target / << END_OF_RMANsql ‘alter system switch logfile’;
sql ‘alter system switch logfile’;
sql ‘alter system switch logfile’;
sql ‘alter system switch logfile’;
sql ‘alter system switch logfile’;
sql ‘alter system switch logfile’;
sql ‘alter system archive log current’;run {
ALLOCATE AUXILIARY CHANNEL t1 DEVICE TYPE sbt_tape parms
‘ENV=(TDPO_OPTFILE=/path/to/tdpo.opt)’;BACKUP ARCHIVELOG UNTIL TIME ‘sysdate’ NOT BACKED UP 1 TIMES DELETE ALL INPUT;
DELETE NOPROMPT OBSOLETE DEVICE TYPE sbt_tape;}
END_OF_RMAN
Please note that if you are using a catalog server, you should reference it when invoking RMAN.
Step 3: Prep aux database
Prior to beginning the clone, the dev database will need to be shutdown. Once it is down, delete the datafiles (typically named *.dbf) for this instance, since cloning over existing datafiles cause the cloned instance to be inconsistent (and you will need to repeat steps 3 and 4 again…). This step may take some time, particularly if you have very many, very large files. Don’t forget to delete the archive logs, since you won’t need them any more!
Finally, restart the database with the ‘nomount’ option.
Step 4: Restore backup to dev database
Here’s a script, which calls RMAN to restore the contents of the target database “prod” to this auxiliary database, “dev.” Again, if you are using a catalog server (and you really should be!), you will need to reference it here as well.
The parameters passed to sbt_tape in this example are TSM-specific. You can ignore the line from “parms” if you are not using TSM. Better yet, ask your storage admin. If you are the storage admin, you’ll have some docs to read.
#!/bin/bashexport ORACLE_HOME=/u01/oracle/dev/devdb/10.2.0.3 export ORACLE_SID=dev $ORACLE_HOME/bin/rman target sys/abc123@prod auxiliary / << END_OF_RMANrun {ALLOCATE AUXILIARY CHANNEL t1 DEVICE TYPE sbt_tape parms 'ENV=(TDPO_OPTFILE=/path/to/tdpo.opt)';set until time "to_date('Jan 01 2011 12:00:00','Mon DDYYYY HH24:MI:SS')";duplicate target database to dev;}END_OF_RMAN
I find it useful to invoke this script from a VNC session, since it will typically run for a very long time. That way, a loss of power or dropped network connection won’t result in a failed clone. Also, I like to use ‘tee’ to send the output to a file, and then pipe stdout to mail, so that I get an email when the job is finished:
$ ./rman_clone.sh | tee clone.log | mail sysadmin@domain.com
Once the script completes (hopefully successfully!), the new database will be mounted. Enjoy!
Recent Comments