This procedure generates two scripts that do a hot backup and restore of the database. It is a full backup of the database datafiles, redolog, init, and control files. You can run this script at database open and it will generate 2 files:
- The first file "_hot_bkup_$1.sql" gets information about the physical structure of the DB and has operating system copy commands. In this case is the UNIX cp.
- The second file "_hot_restore_$1.sql" is used in case of recovery of the whole DB. It puts every copied file back to where it was.
Requires Free Membership to View
#!/bin/csh -f # Filename: hot_bkp_restore.sh # Purpose: creates a hot backup script and a hot recovery script # Author: Robert Hanrahan, # Hewlett Packard Italiana S.p.a. # based on an NT script by Thomas B. Cox # Version: 1.2 # Last Mod: 05-Sep-2002 # # Added in 1.1: check for ARCHIVELOG mode # Added in 1.2: rewritten for Unix (from v1.1 the NT version) + backup of # the redo_log_files # # Known bugs: does not back up INIT.ORA file - fixed :) # # TEST THIS FIRST - NEVER RUN SCRIPTS YOU DON'T UNDERSTAND! # # Note: the names of generated files begin with an underscore '_'. # # Important: the paths (/app/oracle/database/$1/Backup must exist! setenv ORACLE_SID $1 if ($1 == "") then clear echo "" echo "*********************************************" echo "correct usage: $0 ORACLE_SID" echo "*********************************************" echo "" else sqlplus -s internal < /app/oracle/database/$1/Backup/init$1.ora' ); -- the two lines below have been decommented.... dbms_output.put_line( 'startup ' ) ; dbms_output.put_line( 'exit ' ) ; END ; / spool off spool _hot_restore_$1.sql DECLARE vc_host_copy_cmd CONSTANT VARCHAR2(2000) := 'host cp -rf ' ; vc_bkup_dest CONSTANT VARCHAR2(2000) := '/app/oracle/database/$1/Backup' ; vc_dir_sep CONSTANT VARCHAR2(1) := '/' ; CURSOR ts_cur IS SELECT ts.tablespace_name AS ts_nm FROM dba_tablespaces ts ; -- WHERE ts.contents != 'TEMPORARY' ; CURSOR ts_df_cur ( ts_nm_in IN VARCHAR2 , sep_in IN VARCHAR2 ) IS SELECT df.file_name as ts_df_nm , substr ( file_name , instr ( file_name , sep_in, -1, 1 ) + 1 , ( length ( file_name ) + - instr ( file_name , sep_in, -1, 1 ) ) ) as just_df_nm FROM dba_data_files df WHERE df.tablespace_name = ts_nm_in ; CURSOR cf_cur ( sep_in IN VARCHAR2 ) IS SELECT cf.name as cf_nm , substr ( name , instr ( name , sep_in, -1, 1 ) + 1 , ( length ( name ) + - instr ( name , sep_in, -1, 1 ) ) ) as just_cf_nm FROM v$controlfile cf ; CURSOR redo_cur (sep_in IN VARCHAR2 ) IS SELECT rd.MEMBER as redo_nm , substr ( MEMBER , instr ( MEMBER , sep_in, -1, 1 ) + 1 , ( length ( MEMBER ) + - instr ( MEMBER , sep_in, -1, 1 ) ) ) as just_redo_nm FROM v$logfile rd ; BEGIN FOR cf_rec IN cf_cur ( vc_dir_sep ) LOOP dbms_output.put_line ( vc_host_copy_cmd || ' ' || vc_bkup_dest || vc_dir_sep || cf_rec.just_cf_nm || ' ' || cf_rec.cf_nm ); END LOOP ; -- new body for redo_logs FOR redo_rec IN redo_cur ( vc_dir_sep ) LOOP dbms_output.put_line( vc_host_copy_cmd || ' ' || vc_bkup_dest || vc_dir_sep || redo_rec.just_redo_nm || ' ' || redo_rec.redo_nm ); END LOOP ; -- FOR ts_rec IN ts_cur LOOP dbms_output.put_line( 'host echo Start restoring tablespace ' || ts_rec.ts_nm || '.' ); FOR df_rec IN ts_df_cur ( ts_rec.ts_nm , vc_dir_sep ) LOOP dbms_output.put_line ( vc_host_copy_cmd || ' ' || vc_bkup_dest || vc_dir_sep || df_rec.just_df_nm || ' ' || df_rec.ts_df_nm ); END LOOP ; dbms_output.put_line( 'host echo Finished restoring tablespace ' || ts_rec.ts_nm || '.' ); END LOOP ; dbms_output.put_line( 'host echo End of restore. You may now start your database. ' ) ; dbms_output.put_line( 'host cp -rf /app/oracle/database/$1/Backup/init$1.ora $ORACLE_HOME/dbs/init$1.ora' ); END ; / spool off exit EOF endif
For More Information
- Feedback: E-mail the editor with your thoughts about this tip.
- More tips: Hundreds of free Oracle tips and scripts.
- Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
- Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
- Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
- Best Web Links: Oracle tips, tutorials, and scripts from around the Web.
This was first published in October 2002

Join the conversationComment
Share
Comments
Results
Contribute to the conversation