You are on page 1of 58

Oracle DBA: Backup and Recovery Scripts

By Rajendra Gutta Having the right backup and recovery procedures is crucial to the operation of any database. It is the responsibility of the database administrator to protect the database from system faults, crashes, and natural calamities resulting from a variety of circumstances. Learn ho to choose the best backup and recovery mechanism for your !racle system.

Having the right backup and recovery procedures is the lifeblood of any database. "ompanies live on data, and, if that data is not available, the hole company collapses. #s a result, it is the responsibility of the database administrator to protect the database from system faults, crashes, and natural calamities resulting from a variety of circumstances. $he choice of a backup and recovery mechanism depends mainly on the follo ing factors% &atabase mode '#R"HI()L!G, *!#R"HI()L!G+ ,i-e of the database Backup and recovery time uptime $ype of data '!L$., &,,, &ata /arehouse+. $he types of backup are !ffline backup '"old or closed database backup+ !nline backup 'Hot or open database backup+ Logical e0port Logical e0ports create an e0port file that contains a list of ,1L statements to recreate the database. )0port is performed hen the database is open and does not affect users ork. !ffline backups can only be performed hen the database is shut do n cleanly, and the database ill be unavailable to users hile the offline backup is being performed. !nline backups are performed hen the database is open, and it does not affect users ork. $he database needs to run in #R"HI()L!G mode to perform online backups. $he database can run in either #R"HI()L!G mode or *!#R"HI()L!G mode. In #R"HI()L!G mode, the archiver '#R"H+ process archives the redo log files to the archive destination directory. $hese archive files can be used to recover the database in the case of a failure. In *!#R"HI()L!G mode, the redo log files are not archived. /hen the database is running in #R"HI()L!G mode, the choice can be one or more of the follo ing% )0port Hot backup "old backup /hen the database is running in *!#R"HI()L!G mode, the choice of backup is as follo s% )0port "old backup

Cold Backup
!ffline or cold backups are performed hen the database is completely shutdo n. $he disadvantage of an offline backup is that it cannot be done if the database needs to be run 2345. #dditionally, you can only

recover the database up to the point hen the last backup as made unless the database is running in #R"HI()L!G mode. $he general steps involved in performing a cold backup are sho n in 6igure 7.8. $hese general steps are used in riting cold backup scripts for 9ni0 and /indo s *$.

$he steps in 6igure 7.8 are e0plained as follo s. ,tep 8:Generating 6ile List #n offline backup consists of physically copying the follo ing files% &ata files "ontrol files Init.ora and config.ora files

CAUTION
Backing up online redo log files is not advised in all cases, e0cept hen performing cold backup ith the database running in *!#R"HI()L!G mode. If you make a cold backup in #R"HI()L!G mode do not backup redo log files. $here is a chance that you may accidentally over rite your real online redo logs, preventing you from doing a complete recovery. If your database is running in #R"HI()L!G mode, hen you perform cold backup you should also backup archive logs that e0ist. Before performing a cold backup, you need to kno the location of the files that need to be backed up. Because the database structure changes day to day as more files get added or moved bet een directories, it is al ays better to ;uery the database to get the physical structure of database before making a cold backup. $o get the structure of the database, ;uery the follo ing dynamic data dictionary tables% V$datafile Lists all the data files used in the database SQL>select name from v$datafile; Backup the control file and perform a trace of the control file using SQL>alter database backup controlfile to '/u10/backup/control.ctl';

SQL>alter database backup controlfile to trace; Init.ora and config.ora Located under $ORACLE_HOME/dbs directory ,tep 2:,hut do n the database <ou can shut do n a database $su oracle $s lplus !/ as s"sdba! SQL>s#utdo$n ,tep 7:.erform a backup In the first step, you generated a list of files to be backed up. $o back up the files, you can use the 9ni0 copy command 'cp+ to copy it to a backup location, as sho n in the follo ing code. <ou have to copy all files that you generated in ,tep 8. $cp /u01/oracle/users01.dbf /u10/backup <ou can perform the backup of the Init.ora and config.ora files as follo s% $cp $%&'(L)*+%,)/dbs/init.ora /u10/backup $cp $%&'(L)*+%,)/dbs/confi-.ora /u10/backup ,tep 3:,tart the database #fter the backup is complete, you can start the database as follo s% $su oracle $s lplus !/ as s"sdba! SQL> startup ith the follo ing commands%

Hot Backup
#n online backup or hot backup is also referred to as #R"HI() L!G backup. #n online backup can only be done hen the database is running in #R"HI()L!G mode and the database is open. /hen the database is running in #R"HI()L!G mode, the archiver '#R"H+ background process ill make a copy of the online redo log file to archive backup location. #n online backup consists of backing up the follo ing files. But, because the database is open hile performing a backup, you have to follo the procedure sho n in 6igure 7.2 to backup the files% &ata files of each tablespace #rchived redo log files "ontrol file Init.ora and config.ora files

$he general steps involved in performing hot backup are sho n in 6igure 7.2. $hese general steps are used in riting hot backup scripts for 9ni0 and /indo s *$. $he steps in 6igure 7.2 are e0plained as follo s. ,tep 8:.ut the tablespace in the Backup mode and copy the data files. #ssume that your database has t o tablespaces, 9,)R, and $!!L,. $o back up the files for these t o tablespaces, first put the tablespace in backup mode by using the #L$)R statement as follo s% SQL>alter tablespace .S)&S be-in backup; #fter the tablespace is in Backup mode, you can use the ,)L)"$ statement to list the data files for the 9,)R, tablespace, and the copy 'cp+ command to copy the files to the backup location. #ssume that the 9,)R, tablespace has t o data files:users=8.dbf and users=2.dbf. SQL>select file*name from dba*data*files $#ere tablespace*name/'.S)&S'; $cp /u01/oracle/users01.dbf /u10/backup $cp /u01/oracle/users01.dbf /u10/backup $he follo ing command ends the backup process and puts the tablespace back in normal mode. SQL>alter tablespace .S)&S end backup; <ou have to repeat this process for all tablespaces. <ou can get the list of tablespaces by using the follo ing ,1L statement% SQL>select tablespace_name from dba_tablespaces; ,tep 2:Back up the control and Init.ora files. $o backup the control file, SQL>alter database backup controlfile to '/u10/backup/control.ctl'; <ou can copy the Init.ora file to a backup location using $cp $%&'(L)*+%,)/dbs/initorcl.ora /u10/backup ,tep 7:,top archiving.

#rchiving is a continuous process and, ithout stopping archiver, you might unintentionally copy the file that the archiver is currently riting. $o avoid this, first stop the archiver and then copy the archive files to backup location. <ou can stop the archiver as follo s% SQL>alter s"stem s$itc# lo-file; SQL>alter s"stem arc#ive lo- stop; $he first command s itches redo log file and the second command stops the archiver process. ,tep 3:Back up the archive files. $o avoid backing up the archive file that is currently being ritten, e find the least se;uence number that is to be archived from the (>L!G vie , and then backup all the archive files before that se;uence number. $he archive file location is defined by the L!G?#R"HI()?&),$?n parameter in the Init.ora file. select min0se uence12 from v$lo$#ere arc#ived/'3%'; ,tep @:Restart the archive process. $he follo ing command restarts the archiver process% SQL>alter s"stem arc#ive lo- start; *o you have completed the hot backup of database.

#n online backup of a database ill keep the database open and functional for 2345 operations. It is advised to schedule online backups hen there is the least user activity on the database, because backing up the database is very I4! intensive and users can see slo response during the backup period. #dditionally, if the user activity is very high, the archive destination might fill up very fast.

Database Crashes During Hot Backup


$here can be many reasons for the database to crash during a hot backup:a po er outage or rebooting of the server, for e0ample. If these ere to happen during a hot backup, chances are that tablespace ould be left in backup mode. In that case you must manually recover the files involved, and the recovery operation ould end the backup of tablespace. ItAs important to check the status of the files as soon as you restart the instance and end the backup for the tablespace if itAs in backup mode. select a.name4b.status from v$datafile a4 v$backup b $#ere a.file1/b.file1 and b.status/''(567)'; or select a.tablespace*name4a.file*name4b.status from dba*data*files a4 v$backup b $#ere a.file*id/b.file1 and b.status/''(567)'; $his statement lists files ith #"$I() status. If the file is in #"$I() state, the corresponding tablespace is in backup mode. $he second statement gives the tablespace name also, but this canAt be used unless the database is open. <ou need to end the backup mode of the tablespace ith the follo ing command% alter tablespace tablespace_name end backup;

Logical Export
)0port is the single most versatile utility available to perform a backup of the database, deBfragment the database, and port the database or individual objects from one operating system to another operating system.

Export backup detects block corruption


$hough you perform other types of backup regularly, it is good to perform full e0port of database at regular intervals, because e0port detects any data or block corruptions in the database. By using e0port file, it is also possible to recover individual objects, hereas other backup methods do not support individual object recovery. )0port can be used to e0port the database at different levels of functionality% 6ull e0port 'full database e0port+ '69LLC<+ 9serBlevel e0port 'e0ports objects of specified users+ '!/*)RCuserlist+ $ableBlevel e0port 'e0ports specified tables and partitions+ '$#BL),Ctablelist+ $ransportable tablespaces '$#BL),.#"),Ctools, $R#*,.!R$?$#BL),.#")Cy+ $here are t o methods of )0port% "onventional .ath 'default+:9ses ,1L layer to create the e0port file. $he fact is that the ,1L layer introduces ".9 overhead due to character set, converting numbers, dates and so on. $his is time consuming. &irect path '&IR)"$C<),+:,kips the ,1L layer and reads directly from database buffers or private buffers. $herefore it is much faster than conventional path. /e ill discuss scripts to perform the full, userBlevel, and tableBlevel e0port of database. $he scripts also sho you ho to compress and split the e0port file hile performing the e0port. $his is especially useful if the underlying operating system has a limitation of 2GB ma0imum file limit.

Understand scripting
$his chapter re;uires understanding of basic 9ni0 shell and &!, batch programming techni;ues that are described in "hapter 2 DBuilding Blocks.D $hat chapter e0plained some of the common routines that ill be used across most of the scripts presented here. $his book could have provided much more simple scripts. But, considering standardi-ation across all scripts and the reusability of individual sections for your o n riting of scripts, I am focusing on providing a comprehensive script, rather than a temporary fi0. #fter you understand one script, it is easy to follo the flo for the rest of the scripts.

Backup and Recovery under Unix


$he backup and recovery scripts discussed here have been tested under ,un ,olaris 2.0, H.B9E 88.0 and #IE 3.0. $he use of a particular command is discussed if there is a difference bet een these operating systems. $hey might also ork in higher versions of the same operating system. $hese scripts are ritten based on the common ground among these three 9ni0 flavors. Ho ever, I advise that you test the scripts under your environment for both backup and recovery before using it as a regular backup script. $his testing not only gives you confidence in the script, it also gives you an understanding of ho to use the script in case a recovery is needed and gives you peace of mind hen a crisis hits.

Backup cripts !or H"#U$% un olaris% and AI$


$he backup scripts provided here ork for H.B9E, ,un ,olaris, and #IE ith one slight modification. $hat is, the scripts use v>parameter and v>controlfile to get the user dump destination and control file information. Because in 9ni0 the dollar sign '>+ is a special character, you have to precede it ith a for ard slash 'F+ that tells 9ni0 to treat it as a regular character. Ho ever, this is different in each flavor of 9ni0. #IE and H.B9E need one for ard slash, and the ,un !, needs t o for ard slashes to make the dollar sign a regular character. ,un !, @.0 needs t o FF #IE 3.0 needs one F

H.B9E 88.0 needs one F $hese scripts are presented in modular approach. )ach script consists of a number of small functions and a main section. )ach function is designed to meet a specific objective so that they are easy to understand and modify. $hese small functions are reusable and can be used in the design of your o n scripts. If you ant to change a script to fit to your uni;ue needs, you can do so easily in the function here you ant the change ithout affecting the hole script. #fter the backup is complete, it is necessary to check the backup status by revie ing log and error files generated by the scripts.

Cold Backup
"old backup program 'see Listing 7.8+ performs the cold backup of the database under the 9ni0 environment. $he script takes t o input parameters:,I& and !/*)R. ,I& is the instance to be backed up, and !/*)R is the 9ni0 account under hich !racle is running. 6igure 7.7 describes the functionality of the cold backup program. )ach bo0 represents a corresponding function in the program.

Listing &'( coldbackup_ux


111111111111111111111111111111111111111111111111111111111111111111111 1 8&%9&', 3',):coldbackup*u; 1 8.&8%S):8erforms cold backup of t#e database. <atabase 1s#ould be online $#en "ou start 1 t#e script. 6t $ill s#utdo$n and take a cold backup and brin-s 1 t#e database up a-ain

1 .S'9):$coldbackup*u; S6< %=3)& 1 638.5 8'&',)5)&S: S6<06nstance name24 %=3)&0%$ner of instance2 111111111111111111111111111111111111111111111111111111111111111111111 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*verif"02: 7erif" t#at database is online 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*verif"02> S5'5.S/?ps @fu $>%&'*%=3)&A B-rep @v -repB -rep ora*pmon*$>%&'*S6<A? funct*c#k*u;*cmd*stat !<atabase is do$n for -iven S6<0$%&'*S6<24 %$ner0$%&'*%=3)&2. (an't -enerate files to be backed up! A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*verif"*s#utdo$n02: 7erif" t#at database is do$n 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*verif"*s#utdo$n02> S5'5.S/?ps @fu $>%&'*%=3)&A B-rep @v -repB -rep ora*pmon*$>%&'*S6<A? if C $D / 0 E; t#en ec#o !?date?! >> $L%9F6L) ec#o !(%L<G'(H.8*F'6L: $>%&'*S6<A4 <atabase is up4 can't make coldbackup if t#e database is online.!Btee @a $>G'(H.8L%9F6L)A >> $L%9F6L) e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*s#utdo$n*i02: S#utdo$n database in 6mmediate mode 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*s#utdo$n*i02> $>%&'(L)*+%,)A/bin/s lplus @s II )%F / as s"sdba s#utdo$n immediate; e;it )%F A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*s#utdo$n*n02: S#utdo$n database in 3ormal mode 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*s#utdo$n*n02> $>%&'(L)*+%,)A/bin/s lplus @s II )%F / as s"sdba s#utdo$n normal; e;it )%F A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*startup*r02: Startup database in restricted mode 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*startup*r02> $>%&'(L)*+%,)A/bin/s lplus @s II )%F / as s"sdba startup restrict; e;it )%F A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*startup*n02: Startup database in normal mode 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*startup*n02>

$>%&'(L)*+%,)A/bin/s lplus @s II )%F / as s"sdba startup; e;it )%F A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*build*d"nfiles02: 6dentif" t#e files to backup 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*build*d"nfiles02> 1 Guild datafile list ec#o !Guildin- datafile list .! >> $>G'(H.8L%9F6L)A datafile*list/?$>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off select file*name from dba*data*files order b" tablespace*name; e;it )%F? ec#o !111111111111111 SQL for 5emp Files ! >> $>&)S5%&)F6L)A $>%&'(L)*+%,)A/bin/s lplus @s II)%F >> $>&)S5%&)F6L)A / as s"sdba set #eadin- off feedback off select 'alter tablespace 'BBtablespace*nameBB' add tempfile 'BB''BB file*nameBB''BB' reuse'BB';' from dba*temp*files; e;it )%F ec#o !Gackin-up controlfile and trace to trace file! >>$>G'(H.8L%9F6L)A $>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off alter database backup controlfile to '$>(%35&%LF6L)*<6&A/backup*control.ctl'; alter database backup controlfile to trace; e;it )%F 1 Gackup trace of control file (%35&%L/?ls @t $>udump*destA/J.trc B#ead @1? if C K @L !$(%35&%L! E; t#en -rep '(%35&%L' $>(%35&%LA 1> /dev/null if test $D @e 0; t#en cp $>(%35&%LA $>(%35&%LF6L)*<6&A/backup*control.s l fi fi A 1 8repare restore file for control file ec#o !111111 (ontrol File ! >> $>&)S5%&)F6L)A ec#o !1 .se "our o$n discretion to cop" control file4 not advised unless re uired...! >> $>&)S5%&)F6L)A ec#o ! )nd of backup of control file! >> $>G'(H.8L%9F6L)A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*cold*backup02: 8erform cold backup 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*cold*backup02> 1(op" datafiles to backup location ec#o !111111111111111 <ata Files ! >> $>&)S5%&)F6L)A for datafile in ?ec#o $datafile*list? do ec#o !(op"in- datafile $>datafileA ...! >> $>G'(H.8L%9F6L)A 18repare a restore file to restore coldbackup in case a restore is necessar" ec#o cp @p $><'5'F6L)*<6&A/?ec#o $datafileBa$k @F!/! '>print $3FA'? $datafile >> $>&)S5%&)F6L)A

cp @p $>datafileA $><'5'F6L)*<6&A funct*c#k*u;*cmd*stat !Failed to cop" datafile file to backup location! done 1(op" current initIS6<>.ora file to backup director" ec#o ! (op"in- current init.ora file! >> $>G'(H.8L%9F6L)A cp @p $>init*fileA $>6365F6L)*<6&A/init$>%&'*S6<A.ora funct*c#k*u;*cmd*stat !Failed to cop" init.ora file to backup location! ec#o !1111111111111111 6nit.ora File ! >> $>&)S5%&)F6L)A ec#o cp @p $>6365F6L)*<6&A/init$>%&'*S6<A.ora $>init*fileA >> $>&)S5%&)F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*parm02: (#eck for input parameters 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*parm02 > if C $>3'&9A @ne M E; t#en ec#o !(%L<G'(H.8*F'6L: $>%&'*S6<A4 3ot enou-# ar-uments passed! e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*bkup*dir02: (reate backup directories if not alread" e;istin1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*bkup*dir02 > &)S5%&)F6L)*<6&/!$>G'(H.8<6&A/restorefile*dir! G'(H.8L%9*<6&/!$>G'(H.8<6&A/backuplo-*dir! <'5'F6L)*<6&/!$>G'(H.8<6&A/datafile*dir! (%35&%LF6L)*<6&/!$>G'(H.8<6&A/controlfile*dir! &)<%L%9*<6&/!$>G'(H.8<6&A/redolo-*dir! '&(L%9*<6&/!$>G'(H.8<6&A/arclo-*dir! 6365F6L)*<6&/!$>G'(H.8<6&A/initfile*dir! G'(H.8L%9F6L)/!$>G'(H.8L%9*<6&A/backup*lo-*$>%&'*S6<A! &)S5%&)F6L)/!$>&)S5%&)F6L)*<6&A/restorefile*$>%&'*S6<A! L%9F6L)/!$>L%9<6&A/$>%&'*S6<A.lo-! if if if if if if if C C C C C C C K K K K K K K @d @d @d @d @d @d @d $>&)S5%&)F6L)*<6&A E; t#en mkdir @p $>&)S5%&)F6L)*<6&A; fi $>G'(H.8L%9*<6&A E; t#en mkdir @p $>G'(H.8L%9*<6&A; fi $><'5'F6L)*<6&A E; t#en mkdir @p $><'5'F6L)*<6&A; fi $>(%35&%LF6L)*<6&A E; t#en mkdir @p $>(%35&%LF6L)*<6&A; fi $>&)<%L%9*<6&A E; t#en mkdir @p $>&)<%L%9*<6&A; fi $>'&(L%9*<6&A E; t#en mkdir @p $>'&(L%9*<6&A; fi $>6365F6L)*<6&A E; t#en mkdir @p $>6365F6L)*<6&A; fi

if C K @d $><N3*<6&A E; t#en mkdir @p $><N3*<6&A; fi if C K @d $>L%9<6&A E; t#en mkdir @p $>L%9<6&A; fi 1 &emove old backup rm @f $>&)S5%&)F6L)*<6&A/J rm @f $>G'(H.8L%9*<6&A/J rm @f $><'5'F6L)*<6&A/J rm @f $>(%35&%LF6L)*<6&A/J rm @f $>&)<%L%9*<6&A/J rm @f $>'&(L%9*<6&A/J rm @f $>6365F6L)*<6&A/J ec#o !$>O%G3',)A: coldbackup of $>%&'*S6<A be-un on ?date PQ!RcQ!?! > $>G'(H.8L%9F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*-et*vars02: 9et environment variables

1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*-et*vars02> %&'*+%,)/?sed /1/d $>%&'5'G<6&AB-rep @i $>%&'*S6<ABna$k @F !:! '>print $MA'? %&'*G'S)/?ec#o $>%&'*+%,)ABna$k @F !/! '>for 0i/M; iI/3F@M; iPP2 print !/!$iA'? %&'(L)*G'S)/?ec#o $%&'*G'S)Btr @d ! !? init*file/$%&'*+%,)/dbs/init$%&'*S6<.ora 1lo-*arc#*dest1/?sed /1/d $init*fileB-rep @i lo-*arc#ive*destB na$k @F !/! '>print $MA'? 1lo-*arc#*dest/?ec#o $lo-*arc#*dest1Btr @d !'!Btr @d '!'? udump*dest/?$>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off select value from vQQ$parameter $#ere name/'user*dump*dest'; e;it )%F? if C ;$%&'*+%,) / ';' E; t#en ec#o !(%L<G'(H.8*F'6L: (an't -et %&'(L)*+%,) from oratab file for $%&'*S6<!Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi if C K @f $init*file E; t#en ec#o !(%L<G'(H.8*F'6L: init$%&'*S6<.ora does not e;ist in %&'(L)*+%,)/dbs!Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi if C ;$udump*dest / ';' E; t#en ec#o !(%L<G'(H.8*F'6L: user*dump*dest not defined in init$%&'*S6<.ora!B tee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi %&'(L)*+%,)/$>%&'*+%,)A; e;port %&'(L)*+%,) %&'(L)*S6</$>%&'*S6<A; e;port %&'(L)*S6< A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*u;*cmd*stat02: (#eck t#e e;it status of .ni; command 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*u;*cmd*stat02 > if C $D K/ 0 E; t#en ec#o !?date?! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A ec#o !(%L<G'(H.8*F'6L: $>1A !B tee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi A 111111111111111111111111111111111111111111111111111111111111 1 ,'63 111111111111111111111111111111111111111111111111111111111111 3'&9/$1 %&'*S6</$1 %&'*%=3)&/$M 1 Set environment variables BACKUPDIR="/u02/${ORA_SID}/cold" ORA ABDIR=/!"c/o#a"ab OO$S="/u0%/o#aco&'/ad&('/&)_dba" <N3*<6&/!$>5%%LSA/<N3*F6L)S!

L%9<6&/!$>5%%LSA/localo-! O%G3',)/!dbcoldbackup! ec#o ! Startin- coldbackup of $>%&'*S6<A ! funct*c#k*parm funct*c#k*bkup*dir funct*-et*vars funct*verif" funct*build*d"nfiles funct*s#utdo$n*i funct*startup*r funct*s#utdo$n*n funct*verif"*s#utdo$n funct*cold*backup funct*startup*n ec#o !$>%&'*S6<A4 (oldbackup (ompleted successfull" on ?date PQ!RcQ!?! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A 11111111 )3< ,'63 11111111111111111111111111

Cold Backup cript under Unix Checklist


In the main function, set correct values for the BACK !"I#$ ORATABDIR, and TOOLS variables highlighted in the cold backup script. $he default location of ORATABDIR is different for each flavor of 9ni0. 6or information about the default location of the ORATAB file for different flavors of 9ni0, refer to "hapter 87, D9ni0, /indo s *$, and !racle.D "heck for the e0istence of SID in oratab file. If not already there, you must add the instance. "heck for e0istence of initSID.ora file in the ORACLE_HOME/dbs directory. If it is in a different location, you can create a soft link to the ORACLE_HOME/dbs directory. .ass SID and OWNER as parameters to the program. $he database must be running hen you start the program. It gets re;uired information by ;uerying the database and then shuts do n the database and performs cold backup. main() $he main function defines the variables re;uired and calls the functions to be e0ecuted. $he variables BACKUPDIR defines the backup location, ORATABDIR defines the oratab file location. oratab files maintain the list of instances and their home directories on the machine. $his file is created by default hen oracle is installed. If it is not there, you must create one. OWNER is the o ner of !racle soft are directories. # sample oratab file can be found at the end of the chapter. funct_get_vars() $his function gets ORACLE_HOME from the oratab file and USER_DUMP_DEST from the initSID.ora file. $he value of USER_DUMP_DEST is used to back up the trace of the control file. funct_build_dynfiles() $his function generates a list of files from the database for backup. It also creates ,1L statements for temporary files. $hese temporary files do not need to be backed up, but can be recreated hen a restore is performed. $hese temporary files are sessionBspecific and do not have any content hen the database is closed. funct_shutdown_i() $his function shuts do n the database in Immediate mode, so that any user connected to the database ill be disconnected immediately. funct_startup_r() $his function starts up the database in Restricted mode, so that no one can connect to the database e0cept users ith Restrict privileges. funct_shutdown_n() $his function performs a clean shutdo n of the database. funct_chk_ux_cmd_stat() $his function is used to check the status of 9ni0 commands, especially after copying files to a backup location.

)estore *ile
# cold backup program creates a restore file that contains the commands to restore the database. $his functionality is added based on the fact that a lot of &B#s perform backups but, hen it comes to recovery, they ill not have any procedures to make the recovery faster. /ith the restore file, it is easier to restore files to the original location because it has all the commands ready to restore the backup. !ther ise, you need to kno the structure of the database: hat files are located here. # sample restore file is sho n in Listing 7.2.

Listing 3.2 Sample Restore File


111111111 SQL for 5emp Files alter tablespace 5),8 add tempfile '/u0S/oracle/<)7/data/temp0S.dbf' reuse; alter tablespace 5),8 add tempfile '/u0S/oracle/<)7/data/temp0T.dbf' reuse; 111111111 <ata Files cp @p /bkp/<)7/cold/datafile*dir/63<U01.dbf /u0M/oracle/<)7/data/63<U01.dbf cp @p /bkp/<)7/cold/datafile*dir/&GS01.dbf /u0M/oracle/<)7/data/&GS01.dbf cp @p /bkp/<)7/cold/datafile*dir/SNS5),01.dbf /u0M/oracle/<)7/data/SNS5),01.dbf cp @p /bkp/<)7/cold/datafile*dir/5),801.dbf /u0M/oracle/<)7/data/5),801.dbf cp @p /bkp/<)7/cold/datafile*dir/.S)&S01.dbf /u0M/oracle/<)7/data/.S)&S01.dbf 111111111 (ontrol Files cp @p /bkp/<)7/cold/controlfile*dir/cntrl01.dbf /u0M/oracle/<)7/data/cntrl01.dbf 111111111 6nit.ora File cp @p /bkp/<)7/cold/initfile*dir/init<)7.ora /u0M/apps/<)7/oracle/V.1.W/ dbs/init<)7.ora

Cold Backup Troubleshooting and tatus Check


$he important thing here is that the backup log file defined by BACK !L%&'IL( contains detailed information about each step of the backup process. $his is a very good place to start investigating backup failed or for related errors. $his file ill also have the start and end time of the backup. hy the

# single line about the success or failure of a backup is appended to SI".log file every time a backup is performed. $his file is located under the directory defined by the L!G&IR variable. $his file also has the backup completion time. # separate file is created for each instance. $his single file maintains the history of performed backups and their status and timing information. $he messages for a cold backup are A"!L&B#"G9.?6#ILA if a cold backup failed and A"oldbackup "ompleted successfullyA if a backup completes successfully. #part from the B#"G9.L!G6IL) and ,I&.log files, it is al ays good to capture the outBofBtheBordinary errors displayed onscreen if you are running the backup unattended. <ou can capture these errors by running the command sho n ne0t. $he same thing can be done for hot backups. $his command captures onscreen errors to the coldbackup.log file. coldbackup*u; S6< %=3)& 1> coldbackup.lo- M>X1

The following is an excerpt from the SID.log file:


5ue Oul 1V 1Y:TV:TY )<5 M000 (%L<G'(H.8*F'6L: <)74 Failed to cop" control file to backup location BACKUP$O*+I$,

Listing 3.3 Sample BACKUP$O*+I$,


Zdbcoldbackup: coldbackup of <)7 be-un on Sun ,a" M0 M1:1[:MW M001 dbcoldbackup: buildin- datafile list . dbcoldbackup: Guildin- controlfile list (op"in- datafile /u0M/oracle/<)7/data/63<U01.dbf ... (op"in- datafile /u0M/oracle/<)7/data/&GS01.dbf ... (op"in- datafile /u0M/oracle/<)7/data/SNS5),01.dbf ... (op"in- datafile /u0M/oracle/<)7/data/5),801.dbf ... (op"in- datafile /u0M/oracle/<)7/data/.S)&S01.dbf ... (op"in- control file /u0M/oracle/<)7/data/cntrl01.dbf ... (op"in- redolo- file /u0S/oracle/<)7/data/lo-01a.dbf ... (op"in- redolo- file /u0S/oracle/<)7/data/lo-01b.dbf ... (op"in- current init.ora file <)74 (oldbackup (ompleted successfull" on Sun ,a" M0 M1:1\:SV M001

Hot Backup
Listing 3.4 provides the script to perform the hot back p of a database nder the !nix environment. The hot back p script takes two inp t parameters"S6< and %=3)&. S6< is the instance to be backed p# and %=3)& is the !nix acco nt nder which $racle is r nning. %ig re 3.4 shows the f nctionalit& of the hot back p program. 'ach box represents a corresponding f nction in the program.

Listing 3.4 -o"backup_ux


111111111111111111111111111111111111111111111111111111111111111111111 1 8&%9&', 3',): #otbackup*u; 1 8.&8%S): 5#is utilit" $ill perform a $arm backup of 1 t#e database 1 .S'9): $#otbackup*u; S6< %=3)& 1 638.5 8'&',)5)&S: S6<06nstance name24 %=3)&0%$ner of instance2 111111111111111111111111111111111111111111111111111111111111111111111 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*verif"02: 7erif" t#at database is online 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*verif"02> S5'5.S/?ps @fu $>%&'*%=3)&A B-rep @v -repB -rep ora*pmon*$>%&'*S6<A? funct*c#k*u;*cmd*stat !<atabase is do$n for -iven S6<0$%&'*S6<24

%$ner0$%&'*%=3)&2. (an't perform #otbackup ! A 1::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*dblo-mode02: (#eck <G lo- mode 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*dblo-mode02> S5'5.S/?$>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off select lo-*mode from vQQ$database; e;it )%F? if C $S5'5.S / !3%'&(+67)L%9! E; t#en ec#o !?date?! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A ec#o !+%5G'(H.8*F'6L: $%&'*S6< is in 3%'&(+67)L%9 mode. (an't perform #otbackup ! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*control*backup02: Gackup control file 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*control*backup02> ec#o !Ge-in backup of controlfile and trace to trace file! >>$>G'(H.8L%9F6L)A $>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off alter database backup controlfile to '$>(%35&%LF6L)*<6&A/backup*control.ctl'; alter database backup controlfile to trace; e;it )%F 1 Gackup trace of control file (%35&%L/?ls @t $>udump*destA/J.trc B#ead @1? if C K @L !$(%35&%L! E; t#en -rep '(%35&%L' $>(%35&%LA 1> /dev/null if test $D @e 0; t#en cp $>(%35&%LA $>(%35&%LF6L)*<6&A/backup*control.s l fi fi 1 8repare restore file for control file ec#o !111111 (ontrol File ! >> $>&)S5%&)F6L)A ec#o !1 .se "our o$n discretion to cop" control file4 not advised unless re uired...! >> $>&)S5%&)F6L)A ec#o ! )nd of backup of control file! >> $>G'(H.8L%9F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*arc#ivelo-*backup02: Gackup arc#ivelo- files 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*arc#ivelo-*backup02> ec#o !Ge-in backup of arc#ived redo lo-s! >> $>G'(H.8L%9F6L)A 1S$itc# lo-s to flus# current redo lo- to arc#ived redo before back up $>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off alter s"stem s$itc# lo-file; alter s"stem arc#ive lo- stop;

e;it )%F 1 5#is -ets t#e redo se uence number t#at is bein- arc#ived 1 and remove t#is from t#e list of files to be backed up '&(S)Q/?$>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off select min0se uence12 from vQQ$lo$#ere arc#ived/'3%'; e;it )%F? 19et current list of arc#ived redo lo- files '&(L%9*F6L)S/?ls $>lo-*arc#*destA/JB-rep @v $'&(S)Q? $>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off alter s"stem arc#ive lo- start; e;it )%F 18repare restore file for arc lo- files ec#o !11111 'rc#ive Lo- Files! >> $>&)S5%&)F6L)A for arc*file in ?ec#o $'&(L%9*F6L)S? do ec#o cp @p $>'&(L%9*<6&A/?ec#o $arc*fileBa$k @F!/! '>print $3FA'? $arc*file >> $>&)S5%&)F6L)A done 1(op" arc lo- files to backup location 1remove t#e arc#ived redo lo-s from t#e lo-*arc#ive*dest if cop" is successful cp @p $>'&(L%9*F6L)SA $>'&(L%9*<6&A if C $D / 0 E; t#en rm $>'&(L%9*F6L)SA else ec#o !?date?! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A ec#o !+%5G'(H.8*F'6L: Failed to cop" 'rc#ive lo- files! B tee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi ec#o !)nd backup of arc#ived redo lo-s! >> $>G'(H.8L%9F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*init*backup02: Gackup init.ora file 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*init*backup02> 1(op" current initIS6<>.ora file to backup director" ec#o ! (op"in- current init$>%&'*S6<A.ora file! >> $>G'(H.8L%9F6L)A cp @p $>init*fileA $>6365F6L)*<6&A/init$>%&'*S6<A.ora funct*c#k*u;*cmd*stat !Failed to cop" init.ora file to backup location! 1 8repare restore file for init.ora ec#o !1111111111111 8arameter Files! >> $>&)S5%&)F6L)A ec#o cp @p $>6365F6L)*<6&A/init$>%&'*S6<A.ora $>init*fileA >> $>&)S5%&)F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*temp*backup02: 8repre SQL for temp files 1::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct*temp*backup02> ec#o !1111111111111 &ecreate t#e follo$in- 5emporar" Files! >> $>&)S5%&)F6L)A $>%&'(L)*+%,)A/bin/s lplus @s II)%F >> $>&)S5%&)F6L)A / as s"sdba set #eadin- off feedback off select 'alter tablespace 'BBtablespace*nameBB' add tempfile 'BB''BB file*nameBB''BB' reuse'BB';' from dba*temp*files; e;it )%F A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1funct*#ot*backup02: Gackup datafiles 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*#ot*backup02> 1 9et t#e list of tablespaces ec#o !Guildin- tablespace list ! >> $>G'(H.8L%9F6L)A tablespace*list/?$>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off select distinct tablespace*name from dba*data*files order b" tablespace*name; e;it )%F? ec#o !11111 <'5):! ?date? > $>&)S5%&)F6L)A ec#o !1111<ata Files08lease restore onl" corrupted files2! >> $>&)S5%&)F6L)A for tblspace in ?ec#o $tablespace*list? do 1 9et t#e datafiles for t#e current tablespace datafile*list/?$>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off select file*name from dba*data*files $#ere tablespace*name / '$>tblspaceA'; e;it )%F? ec#o ! Ge-innin- back up of tablespace $>tblspaceA...! >> $>G'(H.8L%9F6L)A $>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off alter tablespace $>tblspaceA be-in backup; e;it )%F 1 (op" datafiles of current tablespace for datafile in ?ec#o $datafile*list? do ec#o !(op"in- datafile $>datafileA...! >> $>G'(H.8L%9F6L)A 1 5#e ne;t command prepares restore file ec#o cp @p $><'5'F6L)*<6&A/?ec#o $datafileBa$k @F!/! '>print $3FA'? $datafile >> $>&)S5%&)F6L)A cp @p $>datafileA $><'5'F6L)*<6&A if C $D K/ 0 E; t#en ec#o !?date?! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A ec#o !+%5G'(H.8*F'6L: Failed to cop" file to backup location !B tee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A 1 )ndin- t#e tablespace backup before e;itin?$>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba

set #eadin- off feedback off alter tablespace $>tblspaceA end backup; e;it )%F? e;it 1 fi done $>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off alter tablespace $>tblspaceA end backup; e;it )%F ec#o ! )ndin- back up of tablespace $>tblspaceA..! >> $>G'(H.8L%9F6L)A done A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*parm02: (#eck for input parameters 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*parm02 > if C $>3'&9A @ne M E; t#en ec#o !+%5G'(H.8*F'6L: $>%&'*S6<A4 3ot enou-# ar-uments passed! e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*bkup*dir02: (reate backup directories if not alread" e;istin1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*bkup*dir02 > &)S5%&)F6L)*<6&/!$>G'(H.8<6&A/restorefile*dir! G'(H.8L%9*<6&/!$>G'(H.8<6&A/backuplo-*dir! <'5'F6L)*<6&/!$>G'(H.8<6&A/datafile*dir! (%35&%LF6L)*<6&/!$>G'(H.8<6&A/controlfile*dir! &)<%L%9*<6&/!$>G'(H.8<6&A/redolo-*dir! '&(L%9*<6&/!$>G'(H.8<6&A/arclo-*dir! 6365F6L)*<6&/!$>G'(H.8<6&A/initfile*dir! G'(H.8L%9F6L)/!$>G'(H.8L%9*<6&A/backup*lo-*$>%&'*S6<A! &)S5%&)F6L)/!$>&)S5%&)F6L)*<6&A/restorefile*$>%&'*S6<A! L%9F6L)/!$>L%9<6&A/$>%&'*S6<A.lo-! if if if if if if if C C C C C C C K K K K K K K @d @d @d @d @d @d @d $>&)S5%&)F6L)*<6&A E; t#en mkdir @p $>&)S5%&)F6L)*<6&A; fi $>G'(H.8L%9*<6&A E; t#en mkdir @p $>G'(H.8L%9*<6&A; fi $><'5'F6L)*<6&A E; t#en mkdir @p $><'5'F6L)*<6&A; fi $>(%35&%LF6L)*<6&A E; t#en mkdir @p $>(%35&%LF6L)*<6&A; fi $>&)<%L%9*<6&A E; t#en mkdir @p $>&)<%L%9*<6&A; fi $>'&(L%9*<6&A E; t#en mkdir @p $>'&(L%9*<6&A; fi $>6365F6L)*<6&A E; t#en mkdir @p $>6365F6L)*<6&A; fi

if C K @d $><N3*<6&A E; t#en mkdir @p $><N3*<6&A; fi if C K @d $>L%9<6&A E; t#en mkdir @p $>L%9<6&A; fi 1 &emove old backup rm @f $>&)S5%&)F6L)*<6&A/J rm @f $>G'(H.8L%9*<6&A/J rm @f $><'5'F6L)*<6&A/J rm @f $>(%35&%LF6L)*<6&A/J

rm @f $>&)<%L%9*<6&A/J rm @f $>'&(L%9*<6&A/J rm @f $>6365F6L)*<6&A/J ec#o !$>O%G3',)A: #otbackup of $>%&'*S6<A be-un on ?date PQ!RcQ!?! > $>G'(H.8L%9F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*-et*vars02: 9et environment variables 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*-et*vars02> %&'*+%,)/?sed /1/d $>%&'5'G<6&AB-rep @i $>%&'*S6<ABna$k @F !:! '>print $MA'? %&'*G'S)/?ec#o $>%&'*+%,)ABna$k @F !/! '>for 0i/M; iI/3F@M; iPP2 print !/!$iA'? %&'(L)*G'S)/?ec#o $%&'*G'S)Btr @d ! !? init*file/$%&'*+%,)/dbs/init$%&'*S6<.ora 1lo-*arc#*dest1/?sed /1/d $init*fileB-rep @i lo-*arc#ive*destB na$k @F !/! '>print $MA'? 1lo-*arc#*dest/?ec#o $lo-*arc#*dest1Btr @d !'!Btr @d '!'? udump*dest/?$>%&'(L)*+%,)A/bin/s lplus @s II)%F / as s"sdba set #eadin- off feedback off select value from vQQ$parameter $#ere name/'user*dump*dest'; e;it )%F? if C ;$%&'*+%,) / ';' E; t#en ec#o !+%5G'(H.8*F'6L: can't -et %&'(L)*+%,) from oratab file for $%&'*S6<! B tee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi if C K ec#o B tee e;it fi @f $init*file E; t#en !+%5G'(H.8*F'6L: init$%&'*S6<.ora does not e;ist in %&'(L)*+%,)/dbs! @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A 1

if C ;$lo-*arc#*dest / ';' @o ;$udump*dest / ';' E; t#en ec#o !+%5G'(H.8*F'6L: user*dump*dest or lo-*arc#ive*dest not defined ! B tee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi %&'(L)*+%,)/$>%&'*+%,)A; e;port %&'(L)*+%,) %&'(L)*S6</$>%&'*S6<A; e;port %&'(L)*S6< A 1::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*u;*cmd*stat02: (#eck t#e e;it status of .ni; command 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*u;*cmd*stat02 > if C $D K/ 0 E; t#en ec#o !?date?! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A ec#o !+%5G'(H.8*F'6L:$>1A !Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi A

111111111111111111111111111111111111111111111111111111111111 1 ,'63 111111111111111111111111111111111111111111111111111111111111 3'&9/$1 %&'*S6</$1 %&'*%=3)&/$M 1 Set environment variables BACKUPDIR="/u02/${ORA_SID}/-o"" ORA ABDIR=/!"c/o#a"ab OO$S="/u0%/o#aco&'/ad&('/&)_dba" lo._a#c-_d!/"="/!xpo#"/-o&!/o#cl/a#c-" <N3*<6&/!$>5%%LSA/<N3*F6L)S! L%9<6&/!$>5%%LSA/localo-! O%G3',)/!db#otbackup! ec#o ! Startin- #otbackup of .... $>%&'*S6<A! funct*c#k*parm funct*c#k*bkup*dir funct*-et*vars funct*verif" funct*c#k*dblo-mode funct*#ot*backup funct*temp*backup funct*control*backup funct*arc#ivelo-*backup funct*init*backup ec#o !$>%&'*S6<A4 #otbackup (ompleted successfull" on ?date PQ!RcQ!?! B tee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A 11111111 )3< ,'63 1111111111111111111111111

Hot Backup Script under Unix Checklist

In the main f nction# set the correct val es for G'(H.8<6&# %&'5'G<6&# 5%%LS# and lo-*arc#*dest variables highlighted in the script. The defa lt location of %&'5'G<6& is different for each flavor of !nix. (heck for existence of the S6< instance in the oratab file. If not alread& there# &o m st add the instance. (heck for the existence of the initS6<.ora file in the %&'(L)*+%,)/dbs director&. If it is in a different location# &o m st create a soft link to the %&'(L)*+%,)/dbs director&. )ass S6< and %=3)& as parameters to the program:

main02 G'(H.8<6& defines the back p location. %&'5'G<6& defines the oratab file location. oratab files maintain the list of instances and their home directories on the

machine. This file is created b& defa lt when $racle is installed. If it is not there# &o m st create one. %=3)& is the owner of the $racle software directories. funct*-et*vars02 *ake s re that the .S)&*<.,8*<)S5 parameter is set correctl& in 6nit.ora file. I was rel ctant to get L%9*'&(+67)*<)S5 from the 6nit.ora file beca se there are some changes between $racle + and , in the wa& the archive destination is defined. There are a variet& of wa&s that &o can define lo-*arc#ive*dest based on how man& destinations &o are sing. (onse- entl&# I have given the option to define lo-*arc#ive*dest in main f nction. funct*temp*backup02 $racle + and $racle , s pport permanent temporar& tablespaces .created with create tablespce tablespace_name ... temporary/. 0part from this#

$racle ,I has new feat res to create temporar& tablespaces that do not need back p .created with create tablespace temporary.../. Data in these temporar& tablespaces is session1specific and gets deleted as soon as the session is disconnected. 2eca se of the nat re of these temporar& tablespaces# &o do not need to back them p3 in the case of a restore# &o can 4 st add the data file for these temporar& tablespaces. The files for these temporar& tablespaces are listed nder the dba*temp*files data dictionar& view. funct*control*backup02 In addition to taking back p of control file# this f nction also backs p the trace of the control file. The trace of the control file will be sef l to examine the str ct re of the database. This is the single most important piece of information that &o need to perform a good recover&# especiall& if the database has h ndreds of files. funct*c#k*bkup*dir02 This f nction creates back p directories for data# control# redo log# archivelog# init files# restore files# and back p log files.

Restore file
The restore file for hot back p looks similar to cold back p. )lease refer to the explanation nder the heading restore file for cold back p.

Hot Backup Troubleshooting and tatus Check


The important thing here is that the back p log file defined b& .20(5!)L$6%IL'/ contains detailed information abo t each step of the back p process. This is a ver& good place to start investigating wh& a back p has failed or for related errors. This file will also have the start and end time of the back p. 0 single line abo t the s ccess or fail re of a back p is appended to the S6<.lo- file ever& time a back p is performed. This file is located nder the director& defined b& the L%9<6& variable. This file also has the back p completion time. 0 separate file is created for each instance. This single file maintains the histor& of the performed back ps# their stat s# and timing information. The messages for a hot back p are '+%5G'(H.8*F'6L7# if the hot back p failed# and '+otbackup (ompleted successfull"7# if the back p completes s ccessf ll&. The following is an excerpt from the log file:
5ue Oul 1V 1Y:TV:TY )<5 M000 +%5G'(H.8*F'6L: <)74 3ot enou-# ar-uments passed

Export
The export program .see Listing 3.8/ performs a f ll export of the database nder !nix environment. The export script takes two inp t parameters"S6< and %=3)&. S6< is the instance to be backed p# and %=3)& is the !nix acco nt nder which $racle is r nning. %ig re 3.8 shows the f nctionalit& of the export and split export programs. 'ach box represents a corresponding f nction in the program. Figure 3.5 Functions in export and split export scripts for Unix.

Listing 3.5 xpo#"_ux


1111111111111111111111111111111111111111111111111111111111111111111111 1 8&%9&', 3',): ;port*u; 1 8.&8%S): 8erforms e;port of t#e database 1 .S'9): $;port*u; S6< %=3)& 1 638.5 8'&',)5)&S: S6<06nstance name24 %=3)&0%$ner of instance2 2

1111111111111111111111111111111111111111111111111111111111111111111111 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*verif"02: 7erif" t#at database is online 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*verif"02> S5'5.S/?ps @fu $>%&'*%=3)&A B-rep @v -repB -rep ora*pmon*$>%&'*S6<A? funct*c#k*uni;*command*status !<atabase is do$n for -iven S6<0$%&'*S6<24 %$ner0$%&'*%=3)&2. (an't perform e;port ! A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*cleanup02: (leanup interim files 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*cleanup02 > ec#o !Left for user convenience! > /dev/null A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*build*parfile02: 5#is $ill create parameter file 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*build*parfile02 > 1 5#is line makes sure t#at it al$a"s creates a ne$ parameter file ec#o ! ! >$>8'&F6L)A ec#o !userid/s"stem/mana-er!>>$>8'&F6L)A 1 if "ou use connect strin-. see ne;t line. 1userid/s"stem/mana-er]$>(%3*S5&639A 1ec#o !%$ner/scott!>>$>8'&F6L)A 1ec#o !5ables/scott.51!>>$>8'&F6L)A ec#o !Full/N!>>$>8'&F6L)A 1ec#o !<irect/N!>>$>8'&F6L)A ec#o !9rants/N!>>$>8'&F6L)A ec#o !6nde;es/N!>>$>8'&F6L)A ec#o !&o$s/N!>>$>8'&F6L)A ec#o !(onstraints/N!>>$>8'&F6L)A ec#o !(ompress/3!>>$>8'&F6L)A ec#o !(onsistent/N!>>$>8'&F6L)A ec#o !File/$>F6L)A!>>$>8'&F6L)A ec#o !Lo-/$>)U8%&5*<6&A/$>%&'*S6<A.e;p.lo-!>>$>8'&F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*e;port02: );port t#e database 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*e;port02 > 1 &emove old e;port file rm @f $>F6L)A $>%&'(L)*+%,)A/bin/e;p parfile/$>8'&F6L)A if C $D K/ 0 E; t#en ec#o ?date? >> $L%9<6&/$>%&'*S6<A.loec#o !)U8%&5*F'6L: $>%&'*S6<A4 );port Failed! >> $L%9<6&/$>%&'*S6<A.lofunct*cleanup e;it 1 fi A

1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*parm02: (#eck for input parameters 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*parm02 > if C $>3'&9A @ne M E; t#en ec#o !)U8%&5*F'6L: $>%&'*S6<A4 3ot enou-# ar-uments passed! e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*bkup*dir02: (reate backup directories if not alread" e;ist 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*bkup*dir02 > )U8%&5*<6&/$>G'(H.8<6&A if C K @d $>)U8%&5*<6&A E; t#en mkdir @p $>)U8%&5*<6&A; fi if C K @d $><N3*<6&A E; t#en mkdir @p $><N3*<6&A; fi if C K @d $>L%9<6&A E; t#en mkdir @p $>L%9<6&A; fi A F6L)/!$>)U8%&5*<6&A/$>%&'*S6<A.dmp!

1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*-et*vars02: 9et environment variables 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*-et*vars02> %&'*+%,)/?sed /1/d $>%&'5'G<6&AB-rep @i $>%&'*S6<ABna$k @F !:! '>print $MA'? %&'*G'S)/?ec#o $>%&'*+%,)ABna$k @F !/! '>for 0i/M; iI/3F@M; iPP2 print !/!$iA'? %&'(L)*G'S)/?ec#o $%&'*G'S)Btr @d ! !? %&'(L)*+%,)/$>%&'*+%,)A; e;port %&'(L)*+%,) %&'(L)*S6</$>%&'*S6<A; e;port %&'(L)*S6< 1(%3*S5&639/$>%&'*S6<A.compan".com A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*uni;*command*status02: (#eck e;it status of .ni; command 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*uni;*command*status02 > if C $D K/ 0 E; t#en ec#o !?date?! >> $>L%9<6&A/$>%&'*S6<A.loec#o !)U8%&5*F'6L: $>1A ! >> $>L%9<6&A/$>%&'*S6<A.loe;it 1 fi A 11111111111111111111111111111111111111 1 ,'63 11111111111111111111111111111111111111 3'&9/$1 %&'*S6</$1 %&'*%=3)&/$M 1 Set up t#e environment BACKUPDIR="/u02/${ORA_SID}/!xpo#"" ORA ABDIR=/!"c/o#a"ab

OO$S="/u0%/o#aco&'/ad&('/&)_dba" <N3*<6&/!$>5%%LSA/<N3*F6L)S! 8'&F6L)/!$><N3*<6&A/e;port.par! L%9<6&/!$>5%%LSA/localo-! ec#o !... 3o$ e;portin- .... $>%&'*S6<A! funct*c#k*parm funct*-et*vars funct*verif" funct*c#k*bkup*dir funct*build*parfile funct*e;port funct*cleanup ec#o ?date? >> $L%9<6&/$>%&'*S6<A.loec#o !$>%&'*S6<A4 e;port completed successfull"! >> $L%9<6&/$>%&'*S6<A.lo11111111111111111111111 )3< ,'63 1111111111111111111111111111111

xport Script under Unix Checklist

In the main f nction# set the correct val es for G'(H.8<6&4 %&'5'G<6&# and 5%%LS variables highlighted in the export script. The defa lt location of %&'5'G<6& is different for each flavor of !nix. (heck for existence of S6< in the oratab file. If not alread& there# &o m st add the instance. The funct*build*parfile02 f nction b ilds the parameter file. 2& defa lt# it performs a f ll export. 9o can modif& the parameters to perform a ser1 or table1level export. )ass S6< and %=3)& as parameters to the program:

2 ilds the e;port.par parameter file d&namicall&# based on the information provided in this f nction. This f nction is config red for a f ll export of the database. To perform a different t&pe of export . ser1 or table1level/# set the correct parameters. funct*cleanup02 :emoves the interim files.
funct*build*parfile02

xport !rou"leshooting and Status Check


The 'Lo-' parameter val e set in the parameter file will have detailed information abo t the stat s of export. This is a ver& good place to start investigating wh& an export has failed or for related errors. 0 single line abo t the s ccess or fail re of export is appended to SID.log file ever& time an export is performed. This file is located nder the director& defined b& the L%9<6& variable. This file also has the back p completion time. 0 separate file is created for each instance. This single file maintains the histor& of performed back ps# their stat s# and timing information. The messages for an export are ')U8%&5*F'6L7# if the export failed# and 7);port (ompleted successfull"7# if the export completes s ccessf ll&. The following is an excerpt from a log file:
5ue 'pr V 1Y:0W:1M )S5 M000 <)7 4 e;port completed successfull"

plit Export
The split export program .see Listing 3.;/ performs an export of the database. 0dditionall&# if the export file is larger than <62# the script compresses the export file and splits into m ltiple files to overcome the export limitation of a <62 file s&stem si=e. This is the onl& wa& to split the export file prior to $racle ,i. >ew feat res in ,I allow &o to split the export file into m ltiple files# b t it does not compress the files on1the1fl& to save space. The script ses the !nix commands split and compress to perform splitting and compressing of the files. The f nctions of the script are explained in %ig re 3.8. The split export script takes two inp t parameters"S6< and %=3)&. S6< is the instance to be backed p# and %=3)& is the !nix acco nt nder which $racle is r nning. xport #e$ Features in %i In ,i# $racle introd ced two new export parameters called F6L)S6^) and Q.)&N. F6L)S6^) specifies the maxim m file si=e of each d mp file. This overcomes the <62 file s&stem limitations of export command operating s&stems. 2& sing the Q.)&N parameter# &o can export the s bset of a table data. D ring an import# when sing split export files# &o have to specif& the same F6L)S6^) limit.

Listing 3.& /pl("0xpo#"_ux


1111111111111111111111111111111111111111111111111111111111111111111111 1 8&%9&', 3',): split^;port*u; 1 1 8.&8%S): 8erforms e;port of t#e database 1 (ompresses t#e e;port file on t#e fl" $#ile splittin-. 1 .seful if t#e siLe of e;port file -oes be"ond M9G 1 .S'9): $split^;port*u; S6< %=3)& 1 638.5 8'&',)5)&S: S6<06nstance name24 %=3)&0%$ner of instance2 1111111111111111111111111111111111111111111111111111111111111111111111 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*verif"02: 7erif" t#at database is online 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*verif"02> S5'5.S/?ps @fu $>%&'*%=3)&A B-rep @v -repB -rep ora*pmon*$>%&'*S6<A? funct*c#k*uni;*command*status !<atabase is do$n for -iven S6<0$%&'*S6<24 %$ner0$%&'*%=3)&2. (an't perform e;port ! A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*cleanup02: (leanup interim files 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*cleanup02 > rm f $>868)*<)76()A rm f $>S8L65*868)*<)76()A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*splitcompress*pipe02: (reates pipe for compressin- and splittin- of file 1::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct*splitcompress*pipe02 > 1 (reates pipe for compressinif C K @r $>868)*<)76()A E; t#en /etc/mknod $>868)*<)76()A p fi 1(reates pipe for splittinif C K @r $>S8L65*868)*<)76()A E; t#en /etc/mknod $>S8L65*868)*<)76()A p fi 1 Splits t#e file for ever" [00,G 1 's it splits it adds aa4bb4cc ... LL to t#e name no#up split @b1000m @ $>^F6L)A I $>S8L65*868)*<)76()A X no#up compress I $>868)*<)76()A >$>S8L65*868)*<)76()A X A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*build*parfile02: (reates parameter file 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*build*parfile02 > 1 5#is line makes sure t#at it al$a"s creates a ne$ parameter file ec#o ! ! >$>8'&F6L)A ec#o !userid/s"stem/mana-er!>>$>8'&F6L)A ec#o !Full/N!>>$>8'&F6L)A 1ec#o !tables/scott.t1!>>$>8'&F6L)A ec#o !9rants/N!>>$>8'&F6L)A ec#o !6nde;es/N!>>$>8'&F6L)A ec#o !&o$s/N!>>$>8'&F6L)A ec#o !(onstraints/N!>>$>8'&F6L)A ec#o !(ompress/3!>>$>8'&F6L)A ec#o !(onsistent/N!>>$>8'&F6L)A ec#o !File/$>868)*<)76()A!>>$>8'&F6L)A ec#o !Lo-/$>)U8%&5*<6&A/$>%&'*S6<A.e;p.lo-!>>$>8'&F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*e;port02: );port t#e database 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*e;port02 > 1 &emove old e;port file rm @f $>^F6L)A $>%&'(L)*+%,)A/bin/e;p parfile/$>8'&F6L)A if C $D K/ 0 E; t#en ec#o ?date? >> $L%9<6&/$>%&'*S6<A.loec#o !)U8%&5*F'6L: $>%&'*S6<A4 );port Failed! >> $L%9<6&/$>%&'*S6<A.lofunct*cleanup e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*parm02: (#eck for input parameters 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*parm02 > if C $>3'&9A @ne M E; t#en ec#o !)U8%&5*F'6L: $>%&'*S6<A4 3ot enou-# ar-uments passed! e;it 1

fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*bkup*dir02: (reate backup directories if not alread" e;istin1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*bkup*dir02 > )U8%&5*<6&/$>G'(H.8<6&A if C K @d $>)U8%&5*<6&A E; t#en mkdir @p $>)U8%&5*<6&A; fi if C K @d $><N3*<6&A E; t#en mkdir @p $><N3*<6&A; fi if C K @d $>L%9<6&A E; t#en mkdir @p $>L%9<6&A; fi ^F6L)/!$>)U8%&5*<6&A/$>%&'*S6<A.dmp.^! A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*-et*vars02: 9et environment variables 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*-et*vars02> %&'*+%,)/?sed /1/d $>%&'5'G<6&AB-rep @i $>%&'*S6<ABna$k @F !:! '>print $MA'? %&'*G'S)/?ec#o $>%&'*+%,)ABna$k @F !/! '>for 0i/M; iI/3F@M; iPP2 print !/!$iA'? %&'(L)*G'S)/?ec#o $%&'*G'S)Btr @d ! !? %&'(L)*+%,)/$>%&'*+%,)A; e;port %&'(L)*+%,) %&'(L)*S6</$>%&'*S6<A; e;port %&'(L)*S6< A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*uni;*command*status02: (#eck e;it status of .ni; command 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*uni;*command*status02 > if C $D K/ 0 E; t#en ec#o !?date?! >> $>L%9<6&A/$>%&'*S6<A.loec#o !)U8%&5*F'6L: $>1A ! >> $>L%9<6&A/$>%&'*S6<A.loe;it 1 fi A 111111111111111111111111111111111111111 11 ,'63 111111111111111111111111111111111111111 3'&9/$1 %&'*S6</$1 %&'*%=3)&/$M 1 Set up environment BACKUPDIR="/u02/${ORA_SID}/!xpo#"" ORA ABDIR=/!"c/o#a"ab OO$S="/u0%/o#aco&'/ad&('/&)_dba" <N3*<6&/!$>5%%LSA/<N3*F6L)S! 8'&F6L)/!$><N3*<6&A/e;port.par! L%9<6&/!$>5%%LSA/localo-! 868)*<)76()/!/tmp/e;port*$>%&'*S6<A*pipe! S8L65*868)*<)76()/!/tmp/split*$>%&'*S6<A*pipe! ec#o !... 3o$ e;portin- .... $>%&'*S6<A! funct*c#k*parm

funct*-et*vars funct*verif" funct*c#k*bkup*dir funct*splitcompress*pipe funct*build*parfile funct*e;port funct*cleanup ec#o ?date? >> $L%9<6&/$>%&'*S6<A.loec#o !$>%&'*S6<A4 e;port completed successfull"! >> $L%9<6&/$>%&'*S6<A.lo11111111111111111111111 )3< ,'63 1111111111111111111111111111111

Split xport Script under Unix Checklist


The checklist of things to verif& before the split^;port is r n is the same as for the export program.
funct*splitcompress*pipe02 This

f nction creates two pipes"one for compressing and another for splitting. The export d mp file is passed to the compress pipe for compression# and the o tp t of the compress command is passed to the split command for the split operation. The o tp t of split command is passed to a file. The split command splits the d mp file into pieces of ?@@@*2. Ahen the split operation occ rs# it appends aa# bb# cc...== to the name of the original file to maintain different names for individ al pieces. compress and split are !nix commands. funct*build*parfile02 In b ilding the parameter file# we pass the pipe name as a filename to the e;port command. The pipe acts as a medi m to transfer o tp t from one command to another.

plit I+port
The split import program .see Listing 3.+/ performs an import sing the compressed split export d mp files created b& the split^;port program. The script takes two inp t parameters"S6< and %=3)&. S6< is the instance to be backed p# and %=3)& is the !nix acco nt nder which $racle is r nning.

Listing 3.' /pl("0&po#"_ux


1111111111111111111111111111111111111111111111111111111111111111111111 1 8&%9&', 3',): split^mport*u; 1 8.&8%S): 8erforms import of t#e database usin- e;port files created b" 1split^;port pro-ram. .ncompresses t#e dump file on t#e fl" $#ile desplittin-. 1 .S'9): $split^mport*u; S6< %=3)& 1 638.5 8'&',)5)&S: S6<06nstance name24 %=3)&0%$ner of instance2 1111111111111111111111111111111111111111111111111111111111111111111111 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*verif"02: 7erif" t#at database is online 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*verif"02> S5'5.S/?ps @fu $>%&'*%=3)&A B-rep @v -repB -rep ora*pmon*$>%&'*S6<A? funct*c#k*uni;*command*status !<atabase is do$n for -iven S6<0$%&'*S6<24 %$ner0$%&'*%=3)&2. (an't perform impot! A

1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*cleanup02: (leanup interim files 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*cleanup02 > rm f $>868)*<)76()A rm f $>S8L65*868)*<)76()A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*desplitcompress*pipe02: (reates pipe for uncompressin- and desplittin- of file 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*desplitcompress*pipe02 > 1 (reates pipe for uncompressinif C K @r $>868)*<)76()A E; t#en /etc/mknod $>868)*<)76()A p fi 1(reates pipe for desplittinif C K @r $>S8L65*868)*<)76()A E; t#en /etc/mknod $>S8L65*868)*<)76()A p fi no#up sleep no#up sleep A cat $>^F6L)SA > $>S8L65*868)*<)76()A X [ uncompress I $>S8L65*868)*<)76()A >$>868)*<)76()A X S0

1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*build*parfile02: (reates parameter file 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*build*parfile02 > 1 5#is line makes sure t#at it al$a"s creates a ne$ parameter file ec#o ! ! >$>8'&F6L)A ec#o !userid/s"stem/mana-er!>>$>8'&F6L)A 1ec#o !inde;file/$>G'(H.8<6&A/$>%&'*S6<A.ddl!>>$>8'&F6L)A 1ec#o !%$ner/scott!>>$>8'&F6L)A 1ec#o !Fromuser/kis#an!>>$>8'&F6L)A 1ec#o !5ouser/aravind!>>$>8'&F6L)A 1ec#o !5ables/5145M4tS4tT!>>$>8'&F6L)A ec#o !Full/N!>>$>8'&F6L)A ec#o !6-nore/N!>>$>8'&F6L)A ec#o !(ommit/"!>>$>8'&F6L)A ec#o !File/$>868)*<)76()A!>>$>8'&F6L)A ec#o !Lo-/$>G'(H.8<6&A/$>%&'*S6<A.imp.lo-!>>$>8'&F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*import02: 6mport t#e database 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*import02 > $>%&'(L)*+%,)A/bin/imp parfile/$>8'&F6L)A if C $D K/ 0 E; t#en ec#o ?date? >> $L%9<6&/$>%&'*S6<A.loec#o !6,8%&5*F'6L: $>%&'*S6<A4 6mport Failed! >> $L%9<6&/$>%&'*S6<A.lofunct*cleanup

fi A

e;it 1

1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*parm02: (#eck for input parameters 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*parm02 > if C $>3'&9A @ne M E; t#en ec#o !6,8%&5*F'6L: $>%&'*S6<A4 3ot enou-# ar-uments passed! e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*bkup*dir02: (#eck for backup directories 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*bkup*dir02 > if C K @d $><N3*<6&A E; t#en mkdir @p $><N3*<6&A; fi if C K @d $>L%9<6&A E; t#en mkdir @p $>L%9<6&A; fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*-et*vars02: 9et environment variables 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*-et*vars02> %&'*+%,)/?sed /1/d $>%&'5'G<6&AB-rep @i $>%&'*S6<ABna$k @F !:! '>print $MA'? %&'*G'S)/?ec#o $>%&'*+%,)ABna$k @F !/! '>for 0i/M; iI/3F@M; iPP2 print !/!$iA'? %&'(L)*G'S)/?ec#o $%&'*G'S)Btr @d ! !? %&'(L)*+%,)/$>%&'*+%,)A; e;port %&'(L)*+%,) %&'(L)*S6</$>%&'*S6<A; e;port %&'(L)*S6< A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*uni;*command*status02: (#eck e;it status of .ni; command 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*uni;*command*status02 > if C $D K/ 0 E; t#en ec#o !?date?! >> $>L%9<6&A/$>%&'*S6<A.loec#o !6,8%&5*F'6L: $>1A ! >> $>L%9<6&A/$>%&'*S6<A.loe;it 1 fi A 111111111111111111111111111111111111111 11 ,'63 111111111111111111111111111111111111111 3'&9/$1 %&'*S6</$1 %&'*%=3)&/$M 1 Set up environment BACKUPDIR="/u02/${ORA_SID}/!xpo#"" ORA ABDIR=/!"c/o#a"ab OO$S="/u0%/o#aco&'/ad&('/&)_dba"

1 List all split files in ^F6L)S variable 1^F6L)S/?ec#o $>G'(H.8<6&A/file.dmp.^DDBsort? ^F6L)S/!$>G'(H.8<6&A/file.dmp.^aa $>G'(H.8<6&A/file.dmp.^ab! <N3*<6&/!$>5%%LSA/<N3*F6L)S! 8'&F6L)/!$><N3*<6&A/import.par! L%9<6&/!$>5%%LSA/localo-! 868)*<)76()/!/tmp/import*$>%&'*S6<A*pipe! S8L65*868)*<)76()/!/tmp/split*$>%&'*S6<A*pipe! 3LS*L'39/',)&6('3*',)&6('.=)V6S%VV[\81;e;port 3LS*L'39 ec#o !... 3o$ importin- .... $>%&'*S6<A! funct*c#k*parm funct*-et*vars funct*verif" funct*c#k*bkup*dir funct*desplitcompress*pipe funct*build*parfile funct*import funct*cleanup ec#o ?date? >> $L%9<6&/$>%&'*S6<A.loec#o !$>%&'*S6<A4 import completed successfull"! >> $L%9<6&/$>%&'*S6<A.lo11111111111111111111111 )3< ,'63 1111111111111111111111111111111

Split()port Script under Unix Checklist

In the main02 f nction# set the correct val es for the G'(H.8<6&# %&'5'G<6&# and 5%%LS variables highlighted in the import script. The defa lt location of %&'5'G<6& is different for each flavor of !nix. (heck for the existence of the S6< in the oratab file. If not alread& there# &o m st add the instance. List all split filenames in the ^F6L)S variable in the main02 f nction. The funct*build*parfile02 f nction b ilds the parameter file. 2& defa lt# it performs a f ll import. 9o can modif& the settings to perform a ser or table import. )ass S6< and %=3)& as parameters to the program: onl& trick here is that we need to split and ncompress the files before we se them as inp t to import command. That is accomplished b& creating two pipes. Bere# we se the cat command to send o tp t from split files to the split pipe device. The split pipe device is passed to the uncompress command. The o tp t from the uncompress command is sent to the $racle import command. cat and uncompress are !nix commands. 'ver&thing else is the same as a reg lar import.
funct*desplitcompress*pipe02 The

Oracle o!t,are Backup


This section disc sses backing p the software directories of $racle. Ae have alread& disc ssed how to back p the database. 2acking p software is also a ver& important part of a back p strateg&. The software might not need to be backed p as often as the database beca se it does not change - ite as often. 2 t as &o pgrade# or before &o appl& an& patches to existing software# it is important to make a back p cop& of it to avoid getting into tro ble.

Listing 3., contains the script to perform a back p of $racle software. The script takes two inp t parameters"S6< and %=3)&. S6< is the instance to be backed p# and %=3)& is the !nix acco nt nder which $racle is r nning.

Listing 3.% O#aSo1"2a#!_ux


111111111111111111111111111111111111111111111111111111111111111111111 1 8&%9&', 3',): %raSoft$are*u; 1 8.&8%S): Gackup %&'(L)*+%,) X %&'(L)*G'S) 1 .S'9): $%raSoft$are*u; S6< %=3)& 1 638.5 8'&',)5)&S: S6<06nstance name24 %=3)&0%$ner of instance2 111111111111111111111111111111111111111111111111111111111111111111111 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*verif"*s#utdo$n02: 7erif" t#at database is do$n 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*verif"*s#utdo$n02> S5'5.S/?ps @fu $>%&'*%=3)&A B-rep @v -repB -rep ora*pmon*$>%&'*S6<A? if C $D / 0 E; t#en ec#o !?date?! >> $>L%9F6L)A ec#o !S%F5='&)G'(H.8*F'6L: $>%&'*S6<A4 <atabase is up4 can't do soft$are backup if t#e database is online.! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*s#utdo$n*i02: S#utdo$n database in 6mmediate mode 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*s#utdo$n*i02> $>%&'(L)*+%,)A/bin/s lplus @s II )%F / as s"sdba s#utdo$n immediate; e;it )%F A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*startup*n02: Startup database in normal mode 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*startup*n02> $>%&'(L)*+%,)A/bin/s lplus @s II )%F / as s"sdba startup; e;it )%F A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*soft$are*bkup02: Gackup soft$are 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*soft$are*bkup02>

ec#o !tarrin- $>%&'*+%,)A! >> $>G'(H.8L%9F6L)A ec#o !tarrin- $>%&'*G'S)A! >> $>G'(H.8L%9F6L)A no#up tar cvlpf @ $>%&'*+%,)A B compress > $>%&'+%,)F6L)A M> $>G'(H.8L%9F6L)A no#up tar cvlpf @ $>%&'*G'S)A B compress > $>%&'G'S)F6L)A M> $>G'(H.8L%9F6L)A 18repare restore file ec#o !Lcat $>%&'+%,)F6L)AB tar ;vlpf @ $>%&'*+%,)A! > $>&)S5%&)F6L)A ec#o !Lcat $>%&'G'S)F6L)AB tar ;vlpf @ $>%&'*G'S)A! >> $>&)S5%&)F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*parm02: (#eck for input parameters 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*parm02 > if C $>3'&9A @ne M E; t#en ec#o !S%F5='&)G'(H.8*F'6L: $>%&'*S6<A4 3ot enou-# ar-uments passed! e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*bkup*dir02: (reate backup directories if not alread" e;ist 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*bkup*dir02 > &)S5%&)F6L)*<6&/!$>G'(H.8<6&A/restorefile*dir! G'(H.8L%9*<6&/!$>G'(H.8<6&A/backuplo-*dir! S%F5='&)*<6&/!$>G'(H.8<6&A/soft$are*dir! G'(H.8L%9F6L)/!$>G'(H.8L%9*<6&A/backup*lo-*$>%&'*S6<A! &)S5%&)F6L)/!$>&)S5%&)F6L)*<6&A/restorefile*$>%&'*S6<A! %&'+%,)F6L)/!$>S%F5='&)*<6&A/ora#ome*$>%&'*S6<A.tar.^! %&'G'S)F6L)/!$>S%F5='&)*<6&A/orabase*$>%&'*S6<A.tar.^! L%9F6L)/!$>L%9<6&A/$>%&'*S6<A.lo-! if C K @d $>&)S5%&)F6L)*<6&A E; t#en mkdir @p $>&)S5%&)F6L)*<6&A; fi if C K @d $>G'(H.8L%9*<6&A E; t#en mkdir @p $>G'(H.8L%9*<6&A; fi if C K @d $>S%F5='&)*<6&A E; t#en mkdir @p $>S%F5='&)*<6&A; fi if C K @d $><N3*<6&A E; t#en mkdir @p $><N3*<6&A; fi if C K @d $>L%9<6&A E; t#en mkdir @p $>L%9<6&A; fi 1 &emove old files rm @f $>&)S5%&)F6L)*<6&A/J rm @f $>G'(H.8L%9*<6&A/J rm @f $>S%F5='&)*<6&A/J ec#o !$>O%G3',)A: soft$are backup of $>%&'*S6<A be-un on ?date PQ!RcQ!? ! >> $>G'(H.8L%9F6L)A A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*-et*vars02: 9et environment variables 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*-et*vars02> %&'*+%,)/?sed /1/d $>%&'5'G<6&AB-rep @i $>%&'*S6<ABna$k @F !:! '>print $MA'? %&'*G'S)/?ec#o $>%&'*+%,)ABna$k @F !/! '>for 0i/M; iI/3F@M; iPP2 print !/!$iA'? %&'(L)*G'S)/?ec#o $%&'*G'S)Btr @d ! !? init*file/$%&'*+%,)/dbs/init$%&'*S6<.ora

%&'(L)*+%,)/$>%&'*+%,)A; e;port %&'(L)*+%,) %&'(L)*S6</$>%&'*S6<A; e;port %&'(L)*S6< if C ;$%&'*+%,) / ';' E; t#en ec#o !S%F5='&)G'(H.8*F'6L: (an't -et %&'(L)*+%,) from oratab for $%&'*S6<!B tee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi if C K @f $init*file E; t#en ec#o !S%F5='&)G'(H.8*F'6L: int$%&'*S6<.ora does not e;ist in %&'(L)*+%,)/dbs. .sed b" funct*startup*n to start database!Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi A 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 funct*c#k*uni;*command*status02: (#eck t#e e;it status of .ni; command 1:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: funct*c#k*uni;*command*status02 > if C $D K/ 0 E; t#en ec#o !?date?! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A ec#o !S%F5='&)G'(H.8*F'6L: $>1A!B tee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A e;it 1 fi A 111111111111111111111111111111111111111111111111111111111111 11 ,'63 111111111111111111111111111111111111111111111111111111111111 3'&9/$1 %&'*S6</$1 %&'*%=3)&/$M 1 Set environment variables BACKUPDIR="/u02/${ORA_SID}//o1"2a#!" ORA ABDIR=/!"c/o#a"ab OO$S="/u0%/o#aco&'/ad&('/&)_dba" <N3*<6&/!$>5%%LSA/<N3*F6L)S! L%9<6&/!$>5%%LSA/localo-! O%G3',)/!orasoft$are! ec#o !8reparin- to make soft$are backup of $>%&'*S6<A! funct*c#k*parm funct*c#k*bkup*dir funct*-et*vars funct*s#utdo$n*i funct*verif"*s#utdo$n funct*soft$are*bkup funct*startup*n ec#o !$>%&'*S6<A4 Soft$are Gackup (ompleted successfull" on ?date PQ!RcQ!? ! Btee @a $>G'(H.8L%9F6L)A >> $>L%9F6L)A 11111111 )3< ,'63 1111111111111111111111111

*racle Soft$are Backup Script under Unix Checklist

In the main f nction# set correct val es for G'(H.8<6&# %&'5'G<6&# and 5%%LS variables highlighted in the software back p script. The defa lt location of %&'5'G<6& is different for each flavor of !nix.

(heck for the existence of the S6< in the oratab file. If not alread& there# &o m st add the instance. If &o r $racle software director& str ct re does not follow $%0 g idelines# set %&'*G'S) and %&'*+%,) man all& in funct*-et*vars02. )ass S6< and %=3)& as parameters to the program:

funct*soft$are*bkup02 This f nction tars the software directories of %&'(L)*+%,) %&'(L)*G'S) and compresses the o tp t sing the compress command. Bere# we are

and

ass ming that the $racle software is installed sing $%0 .optimal %lexible 0rchitect re/ g idelines. If not# &o have to man all& set %&'*G'S) and %&'*+%,) in the funct*-et*vars02 f nction. 'no#up' commands s bmit the tar command at the server. main02 If the database is r nning# it sh ts down the database and starts backing p the software directories. Ahen the back p is complete# it restarts the database. Tro bleshooting and stat s check: The important thing here is that the back p log file defined b& G'(H.8L%9F6L) contains detailed information abo t each step of the back p process. This is a ver& good place to start investigating wh& a back p has failed or for related errors. This file will also have the start and end time of back p. 0 single line abo t the s ccess or fail re of back p is appended to S6<.lo- file ever& time back p is performed. This file is located nder the director& defined b& the L%9<6& variable. The messages for a software back p are 'S%F5='&)G'(H.8*F'6L'# if the software back p failed# and 'Soft$are Gackup (ompleted'# successfull"7# if the back p completes s ccessf ll&.

)estoring Oracle o!t,are


The steps to restore the software are as follows: ?. Sh tdown database. <. !se restore file from the back p to restore the directories. 3. Start p the database. The restore command in the restore file first does a Lcat .uncompress and cat/ of the o tp t file and passes it to tar for extraction. %or example#
Lcat ora*#ome.tar.^ B tar ;vlpf @ /oracle/oraV1 ora*#ome.tar.^: /oracle/oraV1: File to e;tract <estination director"

Backup and Recovery under Windo s !"


2efore reading thro gh this section# I strongl& recommend that &o go thro gh the Aindows >T programming section in (hapter <. This section presents and explains the scripts for taking a back p and recovering a database in the Aindows >T environment. Bere# we se the D$S Shell batch programming techni- es to a tomate the back p process. 0fter the back p is complete# it is important to check the back p stat s b& reviewing log and error files generated b& the scripts.

Cold Backup
Listing 3.C performs a cold back p of a database nder the Aindows >T environment. The cold back p script takes S6<# the instance to be backed p# as the inp t parameter. The general steps to write a back p script in !nix and Aindows >T are the same. The onl& difference is that we will be sing commands that are nderstood b& Aindows >T. %ig re 3.; shows the f nctionalit& of a cold back p program nder Aindows >T. 'ach box represents a corresponding section in the program. %or example# the )arameter (hecking section checks for the necessar& inp t parameters and also checks for the existence of the back p directories.

Listing 3.+ coldbackup_'".bat


]ec#o off &), 11111111111111111111111111111111111111111111111111111111111111 &), 8&%9&', 3',): coldbackup*nt.bat &), 8.&8%S): 5#is utilit" performs cold backup of &), t#e database on =indo$s 35 &), .S'9): c:Q>coldbackup*nt.bat S6< &), 638.5 8'&',)5)&S: S6< 06nstance name2 '' &), 111111111111111111111111111111111111111111111111111111111111111 &), ::::::::::::::::::::Ge-in <eclare 7ariables Section /!" ORA_3O4,=c56o#acl!6o#a7%6b(' /!" CO88,C _US,R="/ a/ /)/dba" set %&'(L)*S6</R1 /!" BACKUP_DIR=c56backup69ORAC$,_SID96cold /!" I8I _+I$,=c56o#acl!6ad&('6o#cl6p1(l!6('(":o#a /!" OO$S=c56o#aco&'6ad&('6&)_dba set L%9<6&/R5%%LSRQlocaloset L%9F6L)/RL%9<6&RQR%&'(L)*S6<R.loset set set set (F6L)/RG'(H.8*<6&RQlo-Qcoldbackup.s l )&&*F6L)/RG'(H.8*<6&RQlo-Qcerrors.loL%9*F6L)/RG'(H.8*<6&RQlo-Qcbackup.loGH8*<6&/RG'(H.8*<6&R

&), :::::::::::::::::::: )nd <eclare 7ariables Section &), :::::::::::::::::::: Ge-in 8arameter (#eckin- Section if !R1! // ! -oto usa-e &), (reate backup directories if alread" not e;ist if not e;ist RG'(H.8*<6&RQdata mkdir RG'(H.8*<6&RQdata if not e;ist RG'(H.8*<6&RQcontrol mkdir RG'(H.8*<6&RQcontrol if not e;ist RG'(H.8*<6&RQredo mkdir RG'(H.8*<6&RQredo if not e;ist RG'(H.8*<6&RQlo- mkdir RG'(H.8*<6&RQloif not e;ist RL%9<6&R mkdir RL%9<6&R &), (#eck to if not e;ist if not e;ist if not e;ist if not e;ist see t#at t#ere $ere no create errors RG'(H.8*<6&RQdata -oto backupdir RG'(H.8*<6&RQcontrol -oto backupdir RG'(H.8*<6&RQredo -oto backupdir RG'(H.8*<6&RQlo- -oto backupdir

&), <eletes previous backup. ,ake sure "ou #ave it on tape. del/ RG'(H.8*<6&RQdataQJ del/ RG'(H.8*<6&RQcontrolQJ del/ RG'(H.8*<6&RQredoQJ del/ RG'(H.8*<6&RQlo-QJ ec#o. > R)&&*F6L)R ec#o. > RL%9*F6L)R 0ec#o (old Gackup started X date/5 X time/52 >> RL%9*F6L)R ec#o 8arameter (#eckin- (ompleted >> RL%9*F6L)R &), :::::::::::::::::::: )nd 8arameter (#eckin- Section &), :::::::::::::::::::: Ge-in (reate <"namic files Section ec#o. >R(F6L)R ec#o set termout off #eadin- off feedback off >>R(F6L)R ec#o set linesiLe S00 pa-esiLe 0 >>R(F6L)R ec#o set serveroutput on siLe 1000000 >>R(F6L)R ec#o. >>R(F6L)R ec#o spool RG'(H.8*<6&RQlo-Qcoldbackup*list.bat >>R(F6L)R ec#o. >>R(F6L)R ec#o e;ec dbms*output.put*line0']ec#o off' 2; >>R(F6L)R ec#o. >>R(F6L)R ec#o e;ec dbms*output.put*line0'&), JJJJJJ<ata files' 2; >>R(F6L)R ec#o select 'cop" '_B_B file*name_B_B ' RGH8*<6&RQdata ' >>R(F6L)R ec#o from dba*data*files order b" tablespace*name; >>R(F6L)R ec#o. >>R(F6L)R ec#o e;ec dbms*output.put*line0'&), JJJJJJ(ontrol files' 2; >>R(F6L)R ec#o select 'cop" '_B_B name_B_B ' RGH8*<6&RQcontrol ' >>R(F6L)R ec#o from v$controlfile order b" name; >>R(F6L)R ec#o. >>R(F6L)R ec#o e;ec dbms*output.put*line0'&), JJJJJJ6nit.ora file ' 2; >>R(F6L)R ec#o select ' cop" R6365*F6L)R RGH8*<6&RQcontrol ' >>R(F6L)R ec#o from dual; >>R(F6L)R ec#o e;ec dbms*output.put*line0'e;it;'2; >>R(F6L)R ec#o spool off >>R(F6L)R ec#o e;it >>R(F6L)R ec#o <"namic files Section (ompleted >> RL%9*F6L)R &), :::::::::::::::::::: )nd (reate <"namic files Section &), :::::::::::::::::::: Ge-in (oldGackup Section

R%&'*+%,)RQs R%&'*+%,)RQs R%&'*+%,)RQs R%&'*+%,)RQs

lplus lplus lplus lplus

@s @s @s @s

R(%33)(5*.S)&R R(%33)(5*.S)&R R(%33)(5*.S)&R R(%33)(5*.S)&R

]R(F6L)R ]s#utdo$n*i*nt.s l ]startup*r*nt.s l ]s#utdo$n*n*nt.s l

&), (op" t#e files to backup location start/b RG'(H.8*<6&RQlo-Qcoldbackup*list.bat 1>> RL%9*F6L)R M>> R)&&*F6L)R R%&'*+%,)RQs lplus @s R(%33)(5*.S)&R ]startup*n*nt.s l 0ec#o (oldGackup (ompleted Successfull" X date/5 X time/52 >> RL%9*F6L)R 0ec#o (oldGackup (ompleted Successfull" X date/5 X time/52 >> RL%9F6L)R -oto end &), :::::::::::::::::::: )nd (oldGackup Section &), :::::::::::::::::::::::::::: Ge-in )rror #andlin- section :usa-e ec#o )rror4 .sa-e: coldbackup*nt.bat S6< -oto end :backupdir ec#o )rror creatin- Gackup director" structure >> R)&&*F6L)R 0ec#o (%L<G'(H.8*F'6L:)rror creatin- Gackup director" structure X date/5 X time/52 >> RL%9F6L)R &), :::::::::::::::::::: )nd )rror #andlin- section &), :::::::::::::::::::: (leanup Section :end set %&'*+%,)/ set %&'(L)*S6</ set (%33)(5*.S)&/ set G'(H.8*<6&/ set 6365*F6L)/ set (F6L)/ set )&&*F6L)/ set L%9*F6L)/ set GH8*<6&/

Cold Backup Script for ,indo$s #! Checklist


(heck to see that %&'*+%,)# G'(H.8*<6&# and 5%%LS are set to correct val es according to &o r director& str ct re. These variables are highlighted in the script. Derif& that (%33)(5*.S)& is set to correct the sername and password. Define the 6365*F6L) variable to the location of the 6nit.ora file. 2e s re that the ser r nning the program has Arite access to back p directories. Ahen &o r n the program# pass S6< as a parameter.

Cold Backup under ,indo$s #! !rou"leshooting and Status Check


2ack p log files defined b& L%9*F6L) contain detailed information abo t each step of the back p process. This is a ver& good place to start investigating wh& a back p has failed or for related errors. This file will also have the start and end time of back p. )&&*F6L) has error information. 0 single line abo t the s ccess or fail re of back p is appended to the S6<.lo- file ever& time a back p is performed. This file is located nder the director& defined b& the L%9<6& variable. The messages for a cold back p are '(%L<G'(H.8*F'6L'# if the cold back p failed# and '(old Gackup (ompleted successfull"'# if the back p completes s ccessf ll&.

9o can sched le a tomatic back ps sing the 'at' command# as shown in the following:
at MS:00 !c:QbackupQcoldbackup*nt.bat %&(L! &uns at MS:00 #rs on current date. at MS:00 /ever":,454=45#4F !c:QbackupQcoldbackup*nt.bat %&(L !

This command r ns a back p at <3:@@ ho rs ever& *onda&# T esda&# Aednesda&# Th rsda&# and %rida&. The E(reate D&namic %ilesE section in the coldbackup*nt.bat program creates the coldbackup.s l file .see Listing 3.?@/ nder the log director&. coldbackup.s l is called from coldbackup*nt.bat and generates a list of data# control# and redo log files to be backed p from the database. 0 sample coldbackup.s l is shown in Listing 3.?@ for &o r nderstanding. The contents of this file are derived based on the str ct re of the database.

Listing 3.-. coldbackup:/;l


set termout off #eadin- off feedback off set linesiLe S00 pa-esiLe 0 set serveroutput on siLe 1000000 spool c:QbackupQorclQcoldQlo-Qcoldbackup*list.bat e;ec dbms*output.put*line0']ec#o off' 2; e;ec dbms*output.put*line0'&), JJJJJJ<ata files' 2; select 'cop" 'BB file*nameBB ' c:QbackupQorclQcoldQdata from dba*data*files order b" tablespace*name; e;ec dbms*output.put*line0'&), JJJJJJ(ontrol files' 2; select 'cop" 'BB nameBB ' c:QbackupQorclQcoldQcontrol from v$controlfile order b" name; e;ec dbms*output.put*line0'&), JJJJJJ6nit.ora file ' 2; select ' cop" c:QoracleQadminQorclQpfileQinit.ora c:QbackupQorclQcoldQcontrol ' from dual; e;ec dbms*output.put*line0'e;it;'2; spool offe;it ' '

Ahen the coldback p.s-l file is called from the coldback pFnt.bat program# it spools o tp t to the coldback pFlist.bat D$S batch file .see Listing 3.??/. This file has the commands necessar& for performing the cold back p. This is onl& a sample file. >ote that in the contents of file data# control# redo log# and 6nit.ora files are copied to respective back p directories.

Listing 3.-- coldbackup_l(/":ba"


]ec#o off &), JJJJJJ<ata files cop" (:Q%&'<'5'Q<S9301.<GF c:QbackupQorclQcoldQdata cop" (:Q%&'<'5'Q63<U01.<GF c:QbackupQorclQcoldQdata cop" (:Q%&'<'5'Q%),01.<GF c:QbackupQorclQcoldQdata cop" (:Q%&'<'5'Q&GS01.<GF c:QbackupQorclQcoldQdata cop" (:Q%&'<'5'QSNS5),01.<GF c:QbackupQorclQcoldQdata cop" (:Q%&'<'5'Q5),801.<GF c:QbackupQorclQcoldQdata cop" (:Q%&'<'5'Q.S)&S01.<GF c:QbackupQorclQcoldQdata

&), JJJJJJ(ontrol files cop" (:Q%&'<'5'Q(%35&%L01.(5L c:QbackupQorclQcoldQcontrol cop" (:Q%&'<'5'Q(%35&%L0M.(5L c:QbackupQorclQcoldQcontrol &), JJJJJJ6nit.ora file cop" c:QoracleQadminQorclQpfileQinit.ora c:QbackupQorclQcoldQcontrol e;it;

Hot Backup
The hot back p program .see Listing 3.?</ performs a hot back p of a database nder the Aindows >T environment. The hot back p script takes S6<# the instance to be backed p# as the inp t parameter.

Listing 3.-/ -o"backup_'":ba"


]ec#o off &), 111111111111111111111111111111111111111111111111111111111111111111111 &), 8&%9&', 3',): #otbackup*nt.bat &), 8.&8%S): 5#is utilit" performs #ot backup of &), t#e database on =indo$s 35 &), .S'9): c:Q>#otbackup*nt.bat S6< &), 638.5 8'&',)5)&S: S6< 06nstance name2 '' &), 111111111111111111111111111111111111111111111111111111111111111111111 &), :::::::::::::::::::: Ge-in <eclare 7ariables Section /!" /!" set /!" /!" /!" ORA_3O4,=c56o#acl!6o#a7%6b(' CO88,C _US,R="/ a/ /)/dba" %&'(L)*S6</R1 BACKUP_DIR=c56backup69ORAC$,_SID96-o" I8I _+I$,=c56o#acl!6ad&('6o#cl6p1(l!6('(":o#a ARC_D,S =c56o#acl!6o#ada"a6o#cl6a#c-(<!

/!" OO$S=c56o#aco&'6ad&('6&)_dba set L%9<6&/R5%%LSRQlocaloset L%9F6L)/RL%9<6&RQR%&'(L)*S6<R.loset +F6L)/RG'(H.8*<6&RQlo-Q#otbackup.s l set )&&*F6L)/RG'(H.8*<6&RQlo-Q#errors.loset L%9*F6L)/RG'(H.8*<6&RQlo-Q#backup.loset GH8*<6&/RG'(H.8*<6&R &), :::::::::::::::::::: )nd <eclare 7ariables Section &), :::::::::::::::::::: Ge-in 8arameter (#eckin- Section if !R1! // ! -oto usa-e &), (reate if not e;ist if not e;ist if not e;ist if not e;ist if not e;ist backup directories if alread" not e;ist RG'(H.8*<6&RQdata mkdir RG'(H.8*<6&RQdata RG'(H.8*<6&RQcontrol mkdir RG'(H.8*<6&RQcontrol RG'(H.8*<6&RQarc# mkdir RG'(H.8*<6&RQarc# RG'(H.8*<6&RQlo- mkdir RG'(H.8*<6&RQloRL%9<6&R mkdir RL%9<6&R

&), (#eck to see t#at t#ere $ere no create errors if not e;ist RG'(H.8*<6&RQdata -oto backupdir if not e;ist RG'(H.8*<6&RQcontrol -oto backupdir if not e;ist RG'(H.8*<6&RQarc# -oto backupdir if not e;ist RG'(H.8*<6&RQlo- -oto backupdir

&), <eletes previous backup. ,ake sure "ou #ave it on tape. del/ RG'(H.8*<6&RQdataQJ del/ RG'(H.8*<6&RQcontrolQJ del/ RG'(H.8*<6&RQarc#QJ del/ RG'(H.8*<6&RQlo-QJ ec#o. > R)&&*F6L)R ec#o. > RL%9*F6L)R 0ec#o +ot Gackup started X date/5 X time/52 >> RL%9*F6L)R ec#o 8arameter (#eckin- (ompleted >> RL%9*F6L)R &), :::::::::::::::::::: )nd 8arameter (#eckin- Section &), :::::::::::::::::::: Ge-in (reate <"namic files Section ec#o. >R+F6L)R ec#o set termout off #eadin- off feedback off >>R+F6L)R ec#o set linesiLe S00 pa-esiLe 0 >>R+F6L)R ec#o set serveroutput on siLe 1000000 >>R+F6L)R ec#o spool RG'(H.8*<6&RQlo-Q#otbackup*list.s l >>R+F6L)R ec#o <eclare >>R+F6L)R ec#o cursor c1 is select distinct tablespace*name from dba*data*files order b" tablespace*name; >>R+F6L)R ec#o cursor cM0 ptbs varc#arM2 is select file*name from dba*data*files $#ere tablespace*name / ptbs order b" file*name; >>R+F6L)R ec#o Ge-in >>R+F6L)R ec#o dbms*output.put*line0'set termout off #eadin- off feedback off'2; >>R+F6L)R ec#o. >>R+F6L)R ec#o dbms*output.put*line0c#r0102 2; >>R+F6L)R ec#o dbms*output.put*line0'#ost &), JJJJJJ<ata files' 2; >>R+F6L)R ec#o for tbs in c1 loop >>R+F6L)R ec#o dbms*output.put*line0' alter tablespace '_B_B tbs.tablespace*name _B_B' be-in backup;'2; >>R+F6L)R ec#o for dbf in cM0tbs.tablespace*name2 loop >>R+F6L)R ec#o dbms*output.put*line0' #ost cop" '_B_Bdbf.file*name_B_B' RGH8*<6&RQdata 1_>_> RL%9*F6L)R M_>_> R)&&*F6L)R'2; >>R+F6L)R ec#o end loop; >>R+F6L)R ec#o dbms*output.put*line0' alter tablespace '_B_Btbs.tablespace*name _B_B' end backup;'2; >>R+F6L)R ec#o end loop; >>R+F6L)R ec#o. >>R+F6L)R ec#o dbms*output.put*line0c#r0102 2; >>R+F6L)R ec#o dbms*output.put*line0'#ost &), JJJJJJ(ontrol files ' 2; >>R+F6L)R ec#o dbms*output.put*line0' alter database backup controlfile to '_B_B ''_B_B'RGH8*<6&R QcontrolQcoltrol*file.ctl'_B_B''_B_B';'2; >>R+F6L)R ec#o dbms*output.put*line0' alter database backup controlfile to trace;'2; >>R+F6L)R ec#o. >>R+F6L)R ec#o dbms*output.put*line0c#r0102 2; >>R+F6L)R ec#o dbms*output.put*line0'#ost &), JJJJJJ6nit.ora file ' 2; >>R+F6L)R ec#o dbms*output.put*line0' #ost cop" R6365*F6L)R RGH8*<6&RQcontrol 1_>_> RL%9*F6L)R M_>_> R)&&*F6L)R'2; >>R+F6L)R ec#o. >>R+F6L)R ec#o dbms*output.put*line0c#r0102 2; >>R+F6L)R ec#o dbms*output.put*line0'#ost &), JJJJJJ'rc#ivelo- files' 2; >>R+F6L)R ec#o dbms*output.put*line0' alter s"stem s$itc# lo-file;'2; >>R+F6L)R ec#o dbms*output.put*line0' alter s"stem arc#ive lo- stop;'2; >>R+F6L)R ec#o dbms*output.put*line0'#ost move R'&(*<)S5RQJ RGH8*<6&RQarc#

1_>_> RL%9*F6L)R M_>_> R)&&*F6L)R' 2; >>R+F6L)R ec#o dbms*output.put*line0' alter s"stem arc#ive lo- start;'2; >>R+F6L)R ec#o ec#o ec#o ec#o ec#o dbms*output.put*line0'e;it;'2; )nd; >>R+F6L)R / >>R+F6L)R spool off >>R+F6L)R e;it; >>R+F6L)R >>R+F6L)R

ec#o <"namic files Section (ompleted >> RL%9*F6L)R &), :::::::::::::::::::: )nd (reate <"namic files Section &), :::::::::::::::::::: Ge-in +otGackup Section R%&'*+%,)RQs lplus @s R(%33)(5*.S)&R ]R+F6L)R &), (op" t#e files to backup location R%&'*+%,)RQs lplus @s R(%33)(5*.S)&R ]RG'(H.8*<6&RQlo-Q#otbackup*list.s l 0ec#o +otGackup (ompleted Successfull" X date/5 X time/52 >> RL%9*F6L)R 0ec#o +otGackup (ompleted Successfull" X date/5 X time/52 >> RL%9F6L)R -oto end &), :::::::::::::::::::: )nd +otGackup Section &), :::::::::::::::::::: Ge-in )rror #andlin- section :usa-e ec#o )rror4 .sa-e: #otbackup*nt.bat S6< -oto end :backupdir ec#o )rror creatin- Gackup director" structure >> R)&&*F6L)R 0ec#o +%5G'(H.8*F'6L:)rror creatin- Gackup director" structure X date/5 X time/52 >> RL%9F6L)R &), :::::::::::::::::::: )nd )rror #andlin- section &), :::::::::::::::::::: (leanup Section :end set %&'*+%,)/ set %&'(L)*S6</ set (%33)(5*.S)&/ set G'(H.8*<6&/ set 6365*F6L)/ set '&(*<)S5/ set +F6L)/ set )&&*F6L)/ set L%9*F6L)/ set GH8*<6&/

Bot back p program f nctionalit& can be shown with the similar diagram as for a cold back p. The sections and their p rposes in the program are the same as for a cold back p.

Hot Backup Script under ,indo$s #! Checklist


(heck to see that %&'*+%,)# G'(H.8*<6&# and 5%%LS are set to the correct val es according to &o r director& str ct re. These variables are highlighted in the script. Derif& that (%33)(5*.S)& is set to the correct sername and password. Define the 6365*F6L) variable to the location of the 6nit.ora file. Define the '&(*<)S5 variable to the location archive destination. 2e s re that the ser r nning the program has Arite access to the back p directories.

Ahen &o r n the program# pass S6< as a parameter.

Hot Backup under ,indo$s #! !rou"leshooting and Status Check


The back p log file defined b& L%9*F6L) contains detailed information abo t each step of the back p process. This is a ver& good place to start investigating wh& a back p has failed or for related errors. This file will also have the start and end time of back p. )&&*F6L) has error information. 0 single line abo t the s ccess or fail re of back p is appended to the S6<.lo- file ever& time a back p is performed. This file is located nder the director& defined b& the L%9<6& variable. The messages for a hot back p are '+%5G'(H.8*F'6L'# if a hot back p failed# and '+ot Gackup (ompleted successfull"'# if a back p completes s ccessf ll&. The E(reate D&namic %ilesE section# in the #otbackup*nt.bat creates the #otbackup.s l file .see Listing 3.?3/ nder the log director&. This generates a list of tablespaces# data# control# and redo log files from the database. It is called from the #otbackup*nt.bat program.

Listing 3.-3 -o"backup:/;l


set termout off #eadin- off feedback off set linesiLe S00 pa-esiLe 0 set serveroutput on siLe 1000000 spool c:QbackupQorclQ#otQlo-Q#otbackup*list.s l <eclare cursor c1 is select distinct tablespace*name from dba*data*files order b" tablespace*name; cursor cM0 ptbs varc#arM2 is select file*name from dba*data*files $#ere tablespace*name / ptbs order b" file*name; Ge-in dbms*output.put*line0'set termout off #eadin- off feedback off'2; dbms*output.put*line0c#r0102 2; dbms*output.put*line0'#ost &), JJJJJJ<ata files' 2; for tbs in c1 loop dbms*output.put*line0' alter tablespace 'BB tbs.tablespace*name BB' be-in backup;'2; for dbf in cM0tbs.tablespace*name2 loop dbms*output.put*line0' #ost cop" 'BBdbf.file*nameBB' c:QbackupQorclQ#otQdata 1>> #backup.lo- M>> #errors.lo-'2; end loop; dbms*output.put*line0' alter tablespace 'BBtbs.tablespace*name BB ' end backup;'2; end loop; dbms*output.put*line0c#r0102 2; dbms*output.put*line0'#ost &), JJJJJJ(ontrol files ' 2; dbms*output.put*line0' alter database backup controlfile to 'BB ''BB'c:QbackupQorclQ#otQcontrolQcoltrol*file.ctl 'BB''BB';'2; dbms*output.put*line0' alter database backup controlfile to trace;'2; dbms*output.put*line0c#r0102 2; dbms*output.put*line0'#ost &), JJJJJJ6nit.ora file ' 2; dbms*output.put*line0'#ost cop" c:QoracleQadminQorclQpfileQinit.orac:QbackupQorclQ#otQcontrol 1>> #backup.lo- M>> #errors.lo-'2; dbms*output.put*line0c#r0102 2; dbms*output.put*line0'#ost &), JJJJJJ'rc#ivelo- files' 2; dbms*output.put*line0' alter s"stem s$itc# lo-file;'2; dbms*output.put*line0' alter s"stem arc#ive lo- stop;'2;

dbms*output.put*line0'#ost move c:QoracleQoradataQorclQarc#iveQJ c:QbackupQorclQ#otQarc# 1>> #backup.lo- M>> #errors.lo-' 2; dbms*output.put*line0' alter s"stem arc#ive lo- start;'2; dbms*output.put*line0'e;it;'2; )nd; / spool off e;it;

The hotback p.s-l file is called from hotback pFnt.bat and it spools o tp t to the hotback pFlist.s-l SGL file .see Listing 3.?4/. This file has the commands necessar& for performing a hot back p. This is onl& a sample file. >ote in the file that the data# control# archive log# and 6nit.ora files are copied to their respective back p directories. %irst# it p ts the tablespace into 2ack p mode# copies the corresponding files to back p location# and then t rns off the 2ack p mode for that tablespace. This process is repeated for each tablespace# and each cop" command p ts the stat s of the cop& operation to #backup.lo- and reports an& errors to the #errors.lo- file. Listing 3.?4 is generated based on the str ct re of the database. In a real environment# the database str ct re changes as more data files or tablespaces get added. 2eca se of this# it is important to generate the back p commands d&namicall&# as shown in #otbackup*list.s l. It performs the act al back p and is called from #otbackup*nt.bat.

Listing 3.-4 -o"backup_l(/":/;l


set termout off #eadin- off feedback off #ost &), JJJJJJ<ata files alter tablespace <)S693)& be-in backup; #ost cop" (:Q%&'<'5'Q<S9301.<GF c:backupQorclQ#otQdata 1>> #backup.lo- M>> #errors.lo-alter tablespace <)S693)& end backup; alter tablespace <)S693)&*63<U be-in backup; #ost cop" (:Q%&'<'5'Q<S93*63<U01.<GF c:backupQorclQ#otQdata 1>> #backup.lo- M>> #errors.loalter tablespace <)S693)&*63<U end backup; alter tablespace 63<U be-in backup; #ost cop" (:Q%&'<'5'Q63<U01.<GF c:backupQorclQ#otQdata 1>> #backup.loM>> #errors.loalter tablespace 63<U end backup; alter tablespace %),*&)8%S65%&N be-in backup; #ost cop" (:Q%&'<'5'Q%),&)801.<GF c:backupQorclQ#otQdata 1>> #backup.lo- M>> #errors.loalter tablespace %),*&)8%S65%&N end backup; #ost &), JJJJJJ(ontrol files alter database backup controlfile to 'c:Q#otQcontrolQcoltrol*file.ctl'; alter database backup controlfile to trace; #ost &), JJJJJJ6nit.ora file #ost cop" c:QoracleQadminQorclQpfileQinit.ora 1>> #backup.lo-M>> #errors.lo#ost &), JJJJJJ'rc#ivelo- files alter s"stem s$itc# lo-file; alter s"stem arc#ive lo- stop; #ost move c:QoracleQoradataQorclQarc#iveQJ 1>> #backup.lo- M>> #errors.loalter s"stem arc#ive lo- start; e;it; c:backupQorclQ#otQcontrol

c:QbackupQorclQ#otQarc#

Export
The export program .see Listing 3.?8/ performs a f ll export of the database nder a Aindows >T environment. The export script takes S6<# the instance to be backed p# as the inp t parameter.

Listing 3.-5 !xpo#"_'":ba"


]ec#o off &), 111111111111111111111111111111111111111111111111111111111111111111111 &), 8&%9&', 3',): e;port*nt.bat &), 8.&8%S): &), &), .S'9): 5#is utilit" performs a full e;port of database on =indo$s 35 c:Q>e;port*nt.bat S6<

&), 638.5 8'&',)5)&S: S6< 06nstance name2 '' &), 111111111111111111111111111111111111111111111111111111111111111111111 &), :::::::::::::::::::: Ge-in <eclare 7ariables Section /!" set /!" /!" ORA_3O4,=c56o#acl!6o#a7%6b(' %&'(L)*S6</R1 CO88,C _US,R=/)/"!&/&a'a.!# BACKUP_DIR=c56backup69ORAC$,_SID96!xpo#"

/!" OO$S=c56o#aco&'6ad&('6&)_dba set L%9<6&/R5%%LSRQlocaloset L%9F6L)/RL%9<6&RQR%&'(L)*S6<R.lo&), :::::::::::::::::::: )nd <eclare 7ariables Section &), :::::::::::::::::::: Ge-in 8arameter (#eckin- Section if !R1! // ! -oto usa-e &), (reate backup directories if alread" not e;ist if not e;ist RG'(H.8*<6&R mkdir RG'(H.8*<6&R if not e;ist RL%9<6&R mkdir RL%9<6&R &), (#eck to see t#at t#ere $ere no create errors if not e;ist RG'(H.8*<6&R -oto backupdir &), <eletes previous backup. ,ake sure "ou #ave it on tape. del/ RG'(H.8*<6&RQJ &), :::::::::::::::::::: )nd 8arameter (#eckin- Section &), :::::::::::::::::::: Ge-in );port Section R%&'*+%,)RQe;p R(%33)(5*.S)&R parfile/e;port*par.t;t 0ec#o );port (ompleted Successfull" X date/5 X time/52 >> RL%9F6L)R -oto end &), :::::::::::::::::::: )nd );port Section &), :::::::::::::::::::: Ge-in )rror #andlin- section :usa-e ec#o )rror4 .sa-e: coldbackup*nt.bat S6< -oto end :backupdir

ec#o )rror creatin- Gackup director" structure 0ec#o )U8%&5*F'6L:)rror creatin- Gackup director" structure X date/5 X time/52 >> RL%9F6L)R &), :::::::::::::::::::: )nd )rror #andlin- section &), ::::::::::::::::::::(leanup Section :end set %&'*+%,)/ set %&'(L)*S6</ set (%33)(5*.S)&/ set G'(H.8*<6&/

This program performs an export of the database b& sing the parameter file specified b& exportFpar.txt. In Listing 3.?; is a sample parameter file that performs a f ll export of the database. 9o can modif& the parameter file to s it to &o r re- irements.

Listing 3.-& !xpo#"_pa#:"x"


file/ RG'(H.8*<6&RQe;port.dmp lo-/ RG'(H.8*<6&RQe;port.lofull/" compress/n consistent/"

xport Script under ,indo$s #! Checklist


(heck to see that %&'*+%,) and G'(H.8*<6&4 5%%LS are set to correct val es according to &o r director& str ct re. These variables are highlighted in the program. Derif& that (%33)(5*.S)& is set to the correct sername and password. 2e s re that the ser r nning the program has Arite access to the back p directories. 'dit the parameter file to &o r specific re- irements. Specif& the f ll path of the location of &o r parameter file in the program. Ahen &o r n the program# pass S6< as a parameter.

xport under ,indo$s #! !rou"leshooting and Status Check


The log file specified in the parameter file contains detailed information abo t each step of the export process. This is a ver& good place to start investigating wh& an export has failed or for related errors. 0 single line abo t the s ccess or fail re of export is appended to the S6<.lo- file ever& time an export is performed. This file is located nder the director& defined b& the L%9<6& variable. The messages for an export are ')U8%&5*F'6L'# if the export failed# and ');port (ompleted successfull"'# if the export completes s ccessf ll&.

Recovery #rinciples
:ecover& principles are the same# regardless of whether &o are in a !nix or Aindows >T environment. The following are general g idelines for recover& sing a cold back p# hot back p# and export.

De!initions

Control File"The control file contains records that describe and maintain information abo t the ph&sical str ct re of a database. The control file is pdated contin o sl& d ring

database se and m st be available for writing whenever the database is open. If the control file is not accessible# the database will not open. System Change Number (SCN)"The s&stem change n mber is a clock val e for the database that describes a committed version of the database. The S(> f nctions as a se- ence generator for a database and controls conc rrenc& and redo record ordering. Think of the S(> as a timestamp that helps ens re transaction consistenc&. Checkpoint"0 checkpoint is a data str ct re in the control file that defines a consistent point of the database across all threads of a redo log. (heckpoints are similar to S(>s and the& also describe which threads exist at that S(>. (heckpoints are sed b& recover& to ens re that $racle starts reading the log threads for the redo application at the correct point. %or a parallel server# each checkpoint has its own redo information.

-edia )eco.er/ Co++ands


To perform either a complete media recover& or incomplete media recover&# &o need to be familiar with the following three media recover& commands.
&)(%7)& <'5'G'S) This

command performs a media recover& on all the data files that re- ire the application of redo. This can be sed onl& when the database is mounted but not open. This command is generall& sed when the s&stem data file is lost. &)(%7)& 5'GL)S8'() tablespace_name This command performs a media recover& on all the data files in the tablespaces listed. The database m st be mounted and open. The tablespace in - estion m st be offline to perform the media recover&. To recover the tablespace# &o need to mo nt the database first# p t the data file that is in tro ble offline# and then open the database and p t the tablespace offline. Then give the recover tablespace tablespace_name command and p t the tablespace online when the recover& is complete. &)(%7)& <'5'F6L) 'filename' This command performs a recover& on listed data files. The database can be open or closed. If the database is open# data file recover& can onl& recover offline files. To recover the data file in - estion# mo nt the database and p t the tro bled data file offline# open the database and iss e the '&)(%7)& <'5'F6L) 'FILE_NAME' command# and p t the data file online. This command is generall& sed when a non1s&stem data file is lost. 0erfor)ing Reco1er23 ,here to Start4 9o are a new D20 and &o get a call from the pro4ect manager sa&ing that the sers are not able to connect to the database. 0s a first step# tr& to establish a connection for &o rself as a D20 as shown. If the connection s cceeds# tr& to connect as a reg lar ser and see if &o receive an& errors d ring connection# beca se some errors that are seen b& reg lar sers do not show p when &o connect as Internal or S9SD20 .s ch as *ax sessions reached/.
$s lplus user/p$d

>ow &o determined that &o are not able to connect to the database. 0s a second step# tr& to see whether the processes are r nning b& sing the following command.
$ps efB-rep i %&(L

This sho ld list the processes that are r nning. If it does not list an& processes# &o are s re that the database is down. 0s a third step# check the alert log file for an& errors. The alert log file is located nder the director& defined b& G'(H9&%.3<*<.,8*<)S5 in the 6nit.ora file. This file lists an& errors enco ntered b& database. If &o see an& errors# note the time of the error# error n mber# and error message. If &o do not see an& errors# start p the database .sometimes it will report an error when &o tr& to start p the database/. If the database starts# that is wonderf lH If it doesn7t start# it will generall& complain abo t the error onscreen and also report the error in the alert log file. (heck the alert log again for more information. >ow &o determined from the error that the database is not finding one of the data files. 0s a fo rth step# inform the pro4ect manager that somebod& has ca sed a problem in the database and tr& to find o t what happened .a hard disk problem or perhaps somebod& deleted the file/. Limit &o r time to this research based on time available. 0s a fifth step# tr& to determine what kind of back ps &o have taken recentl& and see which one is most beneficial for recovering as m ch data as possible. This depends on the t&pes of back ps &o r site is emplo&ing to protect from database crashes. If &o have a hot back p mechanism in place# &o can be s re that &o can recover all or most of the data. If &o have an export or cold back p mechanism in place# the data changes since the time of last back p will be lost. 0s a sixth step# follow the instr ctions in this chapter# given &o r recover& scenario.

)eco.er/ Using Cold Backup


To restore a f ll database# do the following: ?. Sh tdown the database. <. (op& all data files# control files# and redo log files from the back p location to the original location. Derif& the owner and permissions for the files .for !nix onl&/. 3. Start p the database.

Reco1er2 ,hen a 5ata File (s Lost


To recover a database sing c cold back p# 4 st restore all the files from the back p location to their original locations and open the database. 9o can find the original ph&sical location in the trace file &o generated as part of the back p. 9o cannot recover the transactions that occ rred between the last back p and the point of fail re"that information is lost.

Reco1er2 ,hen a Redo Log File (s Lost!o reco1er the data"ase $hen a redo log file is lost or corrupted
alter database clear lo-file -roup 1;

Ahere gro p ? is the corr pted log gro p n mber. $r &o can create a new control file and open the database in the :eset Logs mode .alter database open resetlo-s/. %or this the database need to be in >$*$!>T state .start p >$*$!>T/. The reset lo-s option resets the redo log se- ence n mbering and recreates an&

missing logfiles. To create the new control file# &o need to know the f ll str ct re of the database. Ae have taken the trace of control file b& sing 'lter database backup controlfile to trace as part of the back p. %ollow the steps explained in (hapter ?@# EDatabase *aintenance and :eorgani=ation#E for creating a new control file.

Reco1er2 ,hen a Control File (s Lost


To recover the database in case of a lost control file# &o simpl& recreate the control file knowing the str ct re of the database .from the trace of control file/ and open the database with reset logs. %ollow the steps explained in (hapter ?@ for creating a new control file.

)eco.er/ Using Hot Backup


Ahen the database is r nning in 0:(BID'L$6 mode and online back p is being sed# there are a variet& of options for recovering the database# p to the point of fail re# that provide maxim m protection for &o r data. :ecover& can be classified as follows:

(omplete media recover& (losed database recover& $pen databaseIoffline tablespace recover& $pen databaseIoffline tablespaceIindivid al data file recover& Incomplete media recover& (ancel1based recover& Time1based recover& (hange1based recover&

Co)plete 6edia Reco1er2


0t all costs# we want to be able to f ll& recover the data in case of a database fail re. (onse- entl&# we alwa&s tr& to perform a complete recover& nless the need is to recover the database onl& to a specific point in time for specific reasons# s ch as those disc ssed in the next section# EIncomplete *edia :ecover&.E The choice of whether to se a closed or open database recover& is based on the t&pe of fail re. If &o lose s&stem data files# the onl& choice is a closed database recover&. If a non1s&stem data file is lost# &o can perform recover& b& sing either a closed or open database method. S ppose that &o are r nning a <4I+# mission1critical database# and onl& part of the database .non1s&stem/ is damaged. In this sit ation# &o can open the database for sers b& taking the damaged data files offline and then performing a recover& on the damaged files. This wa&# sers can access the rest of the database while the recover& is being performed on the damaged data files.

(nco)plete 6edia Reco1er2


Incomplete media recover& is ver& sef l as well# if a ser drops a table accidentall& and comes to &o for help# for example. If &o know the time the table drop occ rred# &o can restore the database from a back p. 2& sing the latest control file# &o can roll forward the changes b& appl&ing redo log files p to the point 4 st before the accidental drop .time1based recover&/. 0oint in !i)e Reco1er2

There was a database corr ption at 8 p.m. in the evening and the database crashed. Ahen I tried to bring p the database# the database opened and immediatel& died as soon as I started exec ting an& SGL statement. This crippled m& abilit& to perform tro bleshooting of the problem. I restored the database from a back p and applied the archive redo log files p to 4 st before the time of the crash and the database came p fine. :emember# &o have to se the latest control file to roll forward with the archived redo log files# so that the $racle knows what archived redo log files to appl&.

Closed 5ata"ase Reco1er2 Steps


?. :estore the damaged files from back p. <. Aith the following command# mo nt the database b t do not open it:
startup mount

3. Start media recover& as follows:


recover database

0t this point# &o will be prompted for the location of the archived redo log files# if necessar&. 4. $pen the database:
alter database open

Derif& that the recover& worked.

*ffline !a"lespace Reco1er2 Steps


?. :estore the damaged files from the back p. <. Aith the following command# mo nt database b t do not open it:
startup mount

3. Take the corr pted data file offline:


alter datafile '/u01/oradata/users01.dbf' offline;

4. $pen the database as follows:


alter database open;

8. 0fter the database is open# take the tablespace offline. %or example# if the corr pted data file belongs to !S':S tablespace# se the following command:
alter tablespace users offline;

Bere# tablespace can be taken offline either with a normal# temporar&# or immediate priorit&. If possible# take the damaged tablespace offline with a normal or temporar& priorit& to minimi=e the amo nt of recover&. ;. Start the recover& on the tablespace:
recover tablespace users;

0t this point# &o will be prompted for the location of the archived redo log files# if necessar&. +. 2ring the tablespace online:
alter tablespace users online;

,. Derif& that the recover& worked.

*ffline 5atafile Reco1er2 Steps


?. :estore the damaged files from the back p. <. !sing the following command# mo nt the database b t do not open it:
Startup mount

3. Take the corr pted data file offline:


alter datafile '/u01/oradata/users01.dbf' offline;

4. $pen the database:


alter database open;

8. 0fter the database is open# take the tablespace offline. %or example# if the corr pted data file belongs to !S':S tablespace# se the following command:
alter tablespace users offline;

Bere# tablespace can be taken offline either with a normal# temporar&# or immediate priorit&. If possible# take the damaged tablespace offline with a normal or temporar& priorit& to minimi=e the amo nt of recover&. ;. Start the recover& on the data file:
recover datafile '/u01/oradata/users01.dbf';

0t this point# &o will be prompted for the location of the archived redo log files# if necessar&. +. 2ring the tablespace online:
alter tablespace users online;

,. Derif& that the recover& worked.

Cancel7Based Reco1er2 Steps


?. :estore the damaged files from the back p. <. !sing the following command# mo nt the database b t do not open it:
startup mount

3. Start the recover&:

recover database until cancel Cusin- backup controlfileE

0t this point# &o will be prompted for the location of the archived redo log files# if necessar&. 'nter cancel to cancel recover& after $racle has applied the archived redo log file 4 st prior to the point of corr ption. If a back p control file or recreated control file is being sed with incomplete recover&# &o sho ld specif& the using backup controlfile option. In cancel1based recover&# &o cannot stop in the middle of appl&ing a redo log file. 9o either completel& appl& a redo log file or &o don7t appl& it at all. In time1based recover&# &o can appl& to a specific point in time# regardless of the archived redo log n mber. 4. $pen the database:
alter database open resetlo-s

Ahenever an incomplete media recover& is being performed or the back p control file is sed for recover&# the database sho ld be opened with the resetlogs option. The resetlogs option will reset the redo log files. 8. )erform a f ll back p of database. If &o open the database with resetlo-s# a f ll back p of the database sho ld be performed immediatel& after recover&. $therwise# &o will not be able to recover changes made after &o reset the logs. ;. Derif& that the recover& worked.

!i)e7Based Reco1er2 Steps


?. :estore the damaged files from the back p. <. !sing the following command# mo nt the database b t do not open it:
startup mount

3. Start the recover&:


recover database until time Cusin- backup controlfileE

%or example
recover database until time '1\\\@01@01:1M:00:00' usin- backup controlfile

0t this point# &o will be prompted for the location of the archived redo log files# if necessar&. $racle a tomaticall& terminates the recover& when it reaches the correct time. If a back p control file or recreated control file is being sed with incomplete recover&# &o sho ld specif& the sing back p controlfile option. 4. $pen the database:
alter database open resetlo-s

Ahenever an incomplete media recover& is being performed or the back p control file is sed# the database sho ld be opened with the resetlogs option# so that it resets the log n mbering.

8. )erform a f ll back p of the database. If the database is opened with resetlo-s# a f ll back p of the database sho ld be performed immediatel& after recover&. $therwise# &o will not be able to recover the changes made after &o reset the logs. ;. Derif& that the recover& worked.

Change7Based Reco1er2 Steps


?. :estore the damaged files from the back p. <. !sing the following command# mo nt the database b t do not open it:
startup mount

3. Start the recover&:


recover database until c#an-e Cusin- backup controlfileE

%or example
recover database until c#an-e 2315 usin- backup controlfile

0t this point# &o will be prompted for the location of the archived redo log files# if necessar&. $racle a tomaticall& terminates the recover& when it reaches the correct s&stem change n mber .S(>/. If a back p control file or a recreated control file is being sed with an incomplete recover&# &o sho ld specif& sing the backup controlfile option. 4. $pen the database.
alter database open resetlo-s

8. )erform a f ll back p of the database. If the database is opened with resetlo-s# a f ll back p of the database sho ld be performed immediatel& after recover&. $therwise# &o will not be able to recover the changes made after &o reset the logs. ;. Derif& that the recover& worked. S2ste) !a"lespace 8ersus a #on7S2ste) !a"lespace Reco1er2 Ahen a s&stem data file is lost or damaged# the onl& wa& to recover the database is b& doing a closed database recover& sing &)(%7)& <'5'G'S) command. Checking for Files #eeding Reco1er2 The following command can be sed to check the data file stat s. This command works when the database is mo nted or open.
select name4 status from v$datafile;

2efore &o act all& start recovering the database# &o can obtain information abo t the files that need recover& b& exec ting the following command. To exec te the statement# the database m st be mo nted. The command also gives error information.
select b.name4 a.error from v$recover*file a4 v$datafile b $#ere a.file1 / b.file1

)eco.er/ Using I+port


The import tilit& is sed to import the database from the d mp file generated thro gh the e;port tilit&. This is ver& sef l for transferring data across platforms and importing onl& specific ob4ects or sers. It works whether archiving is t rned on or off. % ll database import performance can be improved b& t rning off archiving d ring the import. There are three levels of Import:

% ll !ser1level Table1level

Full ()port
0 f ll import can be sed to restore the database in case of a database crash. %or example# &o have a f ll export of the database from &esterda& and &o r database crashed this afternoon. 9o can se the import command to restore the database from the previo s da&7s back p. The restore steps are as follows. ?. Create a blank database":efer to (hapter ?@ for instr ctions on how to create a database. <. Import the database"The following command performs a f ll database import# ass ming that &o r export d mp filename is e;port.dmp. The 693%&)/N option ignores an& create errors# and the <)S5&%N/3 option does not destro& the existing tablespaces. (:JKimp s&stemImanager fileLexport.dmp logLimport.log f llL& ignoreL& destro&Ln 3. Verify the import log for any errors"Aith this import# the data changes between &o r previo s back p and the crash will be lost.

!a"le7Le1el ()port
0 table level import allows &o to import specific ob4ects witho t importing the whole database. xa)ple -9 %or example# if one of the developers re- ests that &o transfer the ),8 and <)85 tables of ser S($TT from database %&(L to 5)S5. 9o can se the following steps to transfer these two tables. ?. Set &o r %&'(L)*S6< to %&(L. (:JKset $:0(L'FSIDL$:(L This step sets the correct database to which to connect.

<. )erform an export of ),8 and <)85. (:JKexp s&stemImanager tablesL.scott.emp#scott.dept/ fileLexport.dmp logLexport.log This command exports table data# constraints and an& indexes on the table. 2eca se the tables belong to owner scott# we need to precede them with the owner in the export command. Derif& the export.log file to make s re there are no errors in the export. 3. (onnect to T'ST database.
SQL>(onnect s"stem/mana-er]5)S5

4. Drop the tables if it alread& exists. If the 5)S5 database alread& has ),8 and <)85 tables# &o can tr ncate the tables or drop the tables as shown.
SQL>5runcate table ),8;SQL>5runcate table <)85;

$r
SQL><rop table ),8;SQL><rop table <)85;

8. Import the tables to T'ST.

Y. (:Q>set %&'(L)*S6</5)S5 W. (:Q>imp s"stem/mana-er fromuser/scott touser/scott tables/0),84<)852 V. file/e;port.dmp lo-/import.lo- i-nore/N

(heck for an& errors in the import log file. xa)ple /9 S ppose &o walk into the office in the morning and a developer meets &o in the hallwa& and sa&s that he accidentall& dropped the S'L)S table. Be wants to see whether &o can do an&thing to restore the table. Aell# &o co ld do something if &o have an export d mp file from &o r previo s back p. The steps to restore the table are as follows .ass ming that this happened in the 5)S5 database/: ?. Set &o r %&'(L)*S6< to the 5)S5 database.
(:Q>set %&'(L)*S6</5)S5

<. Import the table from previo s back p.

S. (:Q>imp s"stem/mana-er tables/0S(%55.S'L)S2 file/e;port.dmp T. lo-/import.loi-nore/N

This command imports the S0L'S table from previo s back p. 0fter the import check the import log file for an& errors.

Backup and Recovery "ools


)eco.er/ -anager 0)-AN1
:*0> is an $racle provided tool that allows &o perform back p and recover& operations on the database. !sing :*0> &o can back p and restore datafiles# control files and archived redo log files. :*0> operates sing the recover& catalog to store metadata information abo t back p and recover& operations. T&picall& the recover& catalog is stored in a separate database. If &o do not want to se the recover& catalog :*0> can se the target database control file to perform back p and recover& operations. 2eca se most information in the recover& catalog is also available in the target database7s control file# :*0> s pports sing the target database control file instead of a recover& catalog. The disadvantage of sing the control file is that :*0> does not s pport restore or recover& when the control file is lost. To avoid this &o sho ld make fre- ent back ps of the control file. !sing the control file is especiall& appropriate for small databases where installation and administration of another database for the sole p rpose of maintaining the recover& catalog is b rdensome. 0 single recover& catalog is able to store information for m ltiple target databases. (onse- entl&# loss of the recover& catalog can be disastro s. 9o sho ld back p the recover& catalog fre- entl&. If the recover& catalog is destro&ed and no back ps of it are available# then &o can partiall& reconstr ct the catalog from the c rrent control file or control file back ps. Ahen &o perform a back p sing :*0># information abo t the back p is stored in the catalog and the act al back ps.ph&sical files/ are stored on disk or tape.re- ires media management software/. Ahen &o se :*0> with a recover& catalog# the :*0> environment is comprised of the following components

:*0> exec table :ecover& catalog database .Database to hold the catalog/ :ecover& catalog schema in the recover& catalog database .Schema to hold the metadata information/ $ptional *edia *anagement Software .for tape back ps/

Sample Files
a+ple o#a"ab *ile
Listing 3.?+ is created b& the $racle installer when &o install the $racle database nder !nix operating s&stem. The installer adds the instance name# $racle home director&# and a to start p flag .9I>/ for the database in the format C I!:"#A$LE*%"ME:FLA&E. The a to start p flag tells whether the $racle database sho ld be started a tomaticall& when the s&stem is rebooted.

Listing 3.-' o#a"ab


1 'll t#e entries in oratab file follo$ t#e 1 follo$in- s"nta;. )ac# instance listed on a separate line 1 S6<:%&'(L)*+%,):N/3 <)7:/u0M/oracle/<)7/oracle/V.1.W:3 5)S5:/u0[/oracle/5)S5/oracle/V.1.W:3 18&)8&%<:/u0Y/oracle/8&)8&%</oracle/V.1.W:3

a+ple Trace o! Control *ile


Listing 3.?, will have the str ct re of the database. It lists the data files# control files# and the redo log files and their location. This is sef l if &o need to recreate the control file. 0 trace of the control file can be generated b& sing the alter database back p control file to trace.

Listing 3.-% trace of control file


/u0M/oracle/<)7/common/admin/udump/<)7*ora*11V1W.trc %racleVi )nterprise )dition &elease V.1.W.1.0 @ 8roduction =it# t#e 8artitionin- option OServer &elease V.1.W.1.0 @ 8roduction %&'(L)*+%,) / /u0M/oracle/<)7/oracle/V.1.W S"stem name: Sun%S 3ode name: mkin-0W &elease: [.Y 7ersion: 9eneric*10[1V1@M[ ,ac#ine: sunTu 6nstance name: <)7 &edo t#read mounted b" t#is instance: 1 %racle process number: 10 .ni; process pid: 11V1W4 ima-e: oracle]mkin-0W 053S 71@7S2 JJJ S)SS6%3 6<:0\.1S2 M001@0[@1W M1:1[:MV.WS0 JJJ M001@0[@1W M1:1[:MV.WS0 1 5#e follo$in- commands $ill create a ne$ control file and use it 1 to open t#e database. 1 <ata used b" t#e recover" mana-er $ill be lost. 'dditional lo-s ma" 1 be re uired for media recover" of offline data files. .se t#is 1 onl" if t#e current version of all online lo-s are available. S5'&5.8 3%,%.35 (&)'5) (%35&%LF6L) &).S) <'5'G'S) !<)7! 3%&)S)5L%9S 3%'&(+67)L%9 ,'UL%9F6L)S 1Y ,'UL%9,),G)&S T ,'U<'5'F6L)S 10MM ,'U63S5'3()S 1 ,'UL%9+6S5%&N T[S L%9F6L) 9&%.8 1 0 '/u0S/oracle/<)7/data/lo-01a.dbf'4 '/u0S/oracle/<)7/data/lo-01b.dbf' 2 S6^) T00,4 9&%.8 M 0 '/u0S/oracle/<)7/data/lo-0Ma.dbf'4 '/u0S/oracle/<)7/data/lo-0Mb.dbf' 2 S6^) T00,4 <'5'F6L) '/u0M/oracle/<)7/data/s"stem01.dbf'4 '/u0M/oracle/<)7/data/ind;01.dbf'4 '/u0M/oracle/<)7/data/rbs01.dbf'4 '/u0M/oracle/<)7/data/temp01.dbf'4 '/u0M/oracle/<)7/data/users.dbf'4 (+'&'(5)& S)5 =)V6S%VV[\81 ; 1 &ecover" is re uired if an" of t#e datafiles are restored backups4 1 or if t#e last s#utdo$n $as not normal or immediate. &)(%7)& <'5'G'S) 1 <atabase can no$ be opened normall". 'L5)& <'5'G'S) %8)3; 1 (ommands to add tempfiles to temporar" tablespaces. 1 %nline tempfiles #ave complete space information. 1 %t#er tempfiles ma" re uire ad`ustment. 'L5)& 5'GL)S8'() 5),8 '<< 5),8F6L) '/u0S/oracle/<)7/data/temp0T.dbf' &).S); 'L5)& 5'GL)S8'() 5),8 '<< 5),8F6L) '/u0S/oracle/<)7/data/temp0S.dbf' &).S); 1 )nd of tempfile additions.

You might also like