Professional Documents
Culture Documents
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.
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.
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.
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.
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
)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.
# 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
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.
%$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
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.
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.
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
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
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.
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
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.
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
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
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.
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
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&.
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.
&), :::::::::::::::::::: )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
@s @s @s @s
&), (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&/
(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.
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.
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.
&), 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.
/!" 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.
(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.
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.
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.
&), 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.
(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.
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.
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.
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.
(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&
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&.
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
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;
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;
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.
%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.
%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
% 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;
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
This command imports the S0L'S table from previo s back p. 0fter the import check the import log file for an& errors.
:*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.