Clone an Oracle Database Using an Online_hot Backup

Embed Size (px)

Citation preview

  • 8/12/2019 Clone an Oracle Database Using an Online_hot Backup

    1/6

    Clone an Oracle database using an online/hot backup

    tnsManager - Distribute tnsnames the easy way and for free!

    This procedure will clone a database using a online copy of thesource database files. Before beginning though, there are a fewthings that are worth noting about online/hot backups:

    When a tablespace is put into backup mode, Oracle willwrite entire blocks to redo rather than the usual changevectors. For this reason, do not perform a hot backup during

    periods of heavy database activity - it could lead to a lot ofarchive logs being created.

    This procedure will put all tablespaces into backup mode atthe same time. If the source database is quite large and youthink that it might take a long time to copy, consider copyingthe tablespaces one at a time, or in groups.

    While the backup is in progress, it will not be possible totake the tablespaces offline normally or shut down theinstance.

    Ok, lets get started...

    1. Make a note of the current archive log change numberBecause the restored files will require recovery, somearchive logs will be needed. This applies even if you are notintending to put the cloned database into archive log mode.Work out which will be the first required log by running thefollowing query on the source database. Make a note of thechange number that is returned:

    select max(first_change#) chng from v$archived_log / 2. Prepare the begin/end backup scripts

    The following sql will produce two scripts; begin_backup.sqland end_backup.sql. When executed, these scripts will either

    put the tablespaces into backup mode or take them out of it:

    cr_hot_backup.sql

    3. Put the source database into backup modeFrom sqlplus, run the begin backup script created in the last

    step:

    http://www.shutdownabort.com/tnsmanager/index.phphttp://www.shutdownabort.com/tnsmanager/index.phphttp://www.shutdownabort.com/scripts/showscript.php?script=cr_hot_backup.sqlhttp://www.shutdownabort.com/scripts/showscript.php?script=cr_hot_backup.sqlhttp://www.shutdownabort.com/andy/http://www.shutdownabort.com/scripts/showscript.php?script=cr_hot_backup.sqlhttp://www.shutdownabort.com/tnsmanager/index.php
  • 8/12/2019 Clone an Oracle Database Using an Online_hot Backup

    2/6

    @begin_backupThis will put all of the databases tablespaces into backupmode.

    4. Copy the files to the new locationCopy, scp or ftp the files from the source database/machineto the target. Do not copy the control files across. Make surethat the files have the correct permissions and ownership.

    5. Take the source database out of backup modeOnce the file copy has been completed, take the sourcedatabase out of backup mode. Run the end backup scriptcreated in step 2. From sqlplus:

    @end_backup 6. Copy archive logs

    It is only necessary to copy archive logs created during thetime the source database was in backup mode. Begin byarchiving the current redo:

    alter system archive log current;Then, identify which archive log files are required. Whenrun, the following query will ask for a change number. Thisis the number noted in step 1.

    select name

    from v$archived_logwhere first_change# >= &change_noorder by name/

    Create an archive directory in the clone database.s filesystem and copy all of the identified logs into it.

    7. Produce a pfile for the new databaseThis step assumes that you are using a spfile. If you are not,

    just copy the existing pfile.

    From sqlplus: create pfile='init.ora' from

    spfile;

    This will create a new pfile in the $ORACLE_HOME/dbsdirectory.

    Once created, the new pfile will need to be edited. If thecloned database is to have a new name, this will need to bechanged, as will any paths. Review the contents of the file

    and make alterations as necessary. Also think about adjusting

  • 8/12/2019 Clone an Oracle Database Using an Online_hot Backup

    3/6

    memory parameters. If you are cloning a production databaseonto a slower development machine you might want toconsider reducing some values.

    Ensure that the archive log destination is pointing to thedirectory created in step 6.

    8. Create the clone controlfileCreate a control file for the new database. To do this,connect to the source database and request a dump of thecurrent control file. From sqlplus:

    alter database backup controlfile to trace as'/home/oracle/cr_.sql'

    /The file will require extensive editing before it can be used.Using your favourite editor make the following alterations:

    o Remove all lines from the top of the file up to but notincluding the second 'STARTUP MOUNT' line (it'sroughly halfway down the file).

    o Remove any lines that start with --o Remove any lines that start with a #o Remove any blank lines in the 'CREATE

    CONTROLFILE' section.

    o Remove the line 'RECOVER DATABASE USINGBACKUP CONTROLFILE'

    o Remove the line 'ALTER DATABASE OPENRESETLOGS;'

    o Make a copy of the 'ALTER TABLESPACETEMP...' lines, and then remove them from the file.

    Make sure that you hang onto the command, it willbe used later.

    o Move to the top of the file to the 'CREATECONTROLFILE' line. The word 'REUSE' needs to

    be changed to 'SET'. The database name needs settingto the new database name (if it is being changed).Decide whether the database will be put intoarchivelog mode or not.

    o If the file paths are being changed, alter the file to

  • 8/12/2019 Clone an Oracle Database Using an Online_hot Backup

    4/6

    reflect the changes.

    Here is an example of how the file would look for a small

    database called dg9a which isn't in archivelog mode:

    STARTUP NOMOUNTCREATE CONTROLFILE SET DATABASE "DG9A" RESETLOGSFORCE LOGGING NOARCHIVELOG

    MAXLOGFILES 50MAXLOGMEMBERS 5MAXDATAFILES 100MAXINSTANCES 1MAXLOGHISTORY 453

    LOGFILEGROUP 1 '/u03/oradata/dg9a/redo01.log' SIZE

    100M,

    GROUP 2 '/u03/oradata/dg9a/redo02.log' SIZE100M,

    GROUP 3 '/u03/oradata/dg9a/redo03.log' SIZE100MDATAFILE

    '/u03/oradata/dg9a/system01.dbf','/u03/oradata/dg9a/undotbs01.dbf','/u03/oradata/dg9a/cwmlite01.dbf','/u03/oradata/dg9a/drsys01.dbf','/u03/oradata/dg9a/example01.dbf','/u03/oradata/dg9a/indx01.dbf','/u03/oradata/dg9a/odm01.dbf','/u03/oradata/dg9a/tools01.dbf',

    '/u03/oradata/dg9a/users01.dbf','/u03/oradata/dg9a/xdb01.dbf','/u03/oradata/dg9a/andy01.dbf','/u03/oradata/dg9a/psstats01.dbf','/u03/oradata/dg9a/planner01.dbf'

    CHARACTER SET WE8ISO8859P1;

    9. Add a new entry to oratab and source the environmentEdit the /etc/oratab (or /opt/oracle/oratab) and add an entryfor the new database.Source the new environment with '. oraenv' and verify that it

    has worked by issuing the following command: echo $ORACLE_SID

    If this doesn't output the new database sid go back andinvestigate.

    10. Create the a password fileUse the following command to create a password file (add anappropriate password to the end of it):

    orapwd file=${ORACLE_HOME}/dbs/orapw${ORACLE_SID}password=

    11. Create the new control file(s)

  • 8/12/2019 Clone an Oracle Database Using an Online_hot Backup

    5/6

    Ok, now for the exciting bit! It is time to create the newcontrolfiles and open the database:

    sqlplus "/ as sysdba" @/home/oracle/cr_

    If all goes to plan you will see the instance start and then themessage 'Control file created'.

    12. Recover and open the databaseThe archive logs that were identified and copied in step 6must now be applied to the database. Issue the followingcommand from sqlplus:

    recover database using backup controlfile untilcancel

    When prompted to 'Specify log' enter 'auto'. Oracle will thenapply all the available logs, and then error with ORA-00308.This is normal, it simply means that all available logs have

    been applied. Open the database with reset logs:

    alter database open resetlogs;

    13. Create temp filesUsing the 'ALTER TABLESPACE TEMP...' command fromstep 8, create the temp files. Make sure the paths to thefile(s) are correct, then run it from sqlplus.

    14. Perform a few checksIf the last couple of steps went smoothly, the database should

    be open. It is advisable to perform a few checks at this point:

    o Check that the database has opened with:o select status from v$instance;

    The status should be 'OPEN'

    o Make sure that the datafiles are all ok:o select distinct status from v$datafile;

    It should return only ONLINE and SYSTEM.

    o Take a quick look at the alert log too.

    15. Set the databases global name

  • 8/12/2019 Clone an Oracle Database Using an Online_hot Backup

    6/6

    The new database will still have the source databases globalname. Run the following to reset it:

    alter database rename global_name to

    /Note. no quotes!

    16. Create a spfileFrom sqlplus:

    create spfile from pfile; 17. Change the database ID

    If RMAN is going to be used to back-up the database, thedatabase ID must be changed. If RMAN isn't going to beused, there is no harm in changing the ID anyway - and it's a

    good practice to do so.

    From sqlplus: shutdown immediate startup mount exit

    From unix:

    nid target=/

    NID will ask if you want to change the ID. Respond with 'Y'.Once it has finished, start the database up again in sqlplus:

    shutdown immediatestartup mountalter database open resetlogs/

    18. Configure TNSAdd entries for new database in the listener.ora andtnsnames.ora as necessary.

    19. FinishedThat's it!

    Copyright 2007-2011 Andrew Barry

    http://www.shutdownabort.com/contact.phphttp://www.shutdownabort.com/contact.phphttp://www.shutdownabort.com/contact.php