Thinking Out Loud

October 3, 2015

Using sed to format controlfile trace

Filed under: linux,oracle — mdinh @ 1:33 am

Do you find it cumbersome having to format controlfile trace?

For the most part, it’s a manual and tedious process.

Using sed, it may be possible to automate task.

RESUSE overwrites existing controlfile trace.

alter database backup controlfile to trace as ‘/tmp/cf.sql’ REUSE RESETLOGS;

ARROW:(SYS@hawklas):PRIMARY> alter database backup controlfile to trace as '/tmp/cf.sql' RESETLOGS;
alter database backup controlfile to trace as '/tmp/cf.sql' RESETLOGS
*
ERROR at line 1:
ORA-01277: file '/tmp/cf.sql' already exists


ARROW:(SYS@hawklas):PRIMARY> !ls -l /tmp/cf.sql
-rw-r--r--. 1 oracle oinstall 3917 Oct  2 17:36 /tmp/cf.sql

ARROW:(SYS@hawklas):PRIMARY> alter database backup controlfile to trace as '/tmp/cf.sql' REUSE RESETLOGS;

Database altered.

ARROW:(SYS@hawklas):PRIMARY> !ls -l /tmp/cf.sql
-rw-r--r--. 1 oracle oinstall 3915 Oct  2 17:50 /tmp/cf.sql

ARROW:(SYS@hawklas):PRIMARY> !date
Fri Oct  2 17:51:03 PDT 2015

ARROW:(SYS@hawklas):PRIMARY>

$ cat cf.sql

-- The following are current System-scope REDO Log Archival related
-- parameters and can be included in the database initialization file.
--
-- LOG_ARCHIVE_DEST=''
-- LOG_ARCHIVE_DUPLEX_DEST=''
--
-- LOG_ARCHIVE_FORMAT=%t_%s_%r.dbf
--
-- DB_UNIQUE_NAME="hawklas"
--
-- LOG_ARCHIVE_CONFIG='SEND, RECEIVE, NODG_CONFIG'
-- LOG_ARCHIVE_MAX_PROCESSES=2
-- ARCHIVE_LAG_TARGET=1800
-- STANDBY_FILE_MANAGEMENT=AUTO
-- STANDBY_ARCHIVE_DEST=?/dbs/arch
-- FAL_CLIENT=''
-- FAL_SERVER=''
--
-- LOG_ARCHIVE_DEST_1='LOCATION=USE_DB_RECOVERY_FILE_DEST'
-- LOG_ARCHIVE_DEST_1='MANDATORY NOREOPEN NODELAY'
-- LOG_ARCHIVE_DEST_1='ARCH NOAFFIRM EXPEDITE NOVERIFY SYNC'
-- LOG_ARCHIVE_DEST_1='NOREGISTER NOALTERNATE NODEPENDENCY'
-- LOG_ARCHIVE_DEST_1='NOMAX_FAILURE NOQUOTA_SIZE NOQUOTA_USED NODB_UNIQUE_NAME'
-- LOG_ARCHIVE_DEST_1='VALID_FOR=(PRIMARY_ROLE,ONLINE_LOGFILES)'
-- LOG_ARCHIVE_DEST_STATE_1=ENABLE

--
-- The following commands will create a new control file and use it
-- to open the database.
-- Data used by Recovery Manager will be lost.
-- The contents of online logs will be lost and all backups will
-- be invalidated. Use this only if online logs are damaged.

-- After mounting the created controlfile, the following SQL
-- statement will place the database in the appropriate
-- protection mode:
--  ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE PERFORMANCE

STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "HAWK" RESETLOGS FORCE LOGGING ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 2
    MAXDATAFILES 30
    MAXINSTANCES 1
    MAXLOGHISTORY 1168
LOGFILE
  GROUP 1 '/oradata/HAWKLAS/onlinelog/o1_mf_1_bg5n7x0p_.log'  SIZE 100M BLOCKSIZE 512,
  GROUP 2 '/oradata/HAWKLAS/onlinelog/o1_mf_2_bg5n7x51_.log'  SIZE 100M BLOCKSIZE 512,
  GROUP 3 '/oradata/HAWKLAS/onlinelog/o1_mf_3_bg5n7x9m_.log'  SIZE 100M BLOCKSIZE 512
-- STANDBY LOGFILE
DATAFILE
  '/oradata/HAWKLAS/datafile/o1_mf_system_bg5n8n6k_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_sysaux_bg5n9c44_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_undotbs1_bg5n9scg_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_users_bg5nb91z_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_ggdata_bjysrq7q_.dbf'
CHARACTER SET AL32UTF8
;

-- Configure RMAN configuration record 1
VARIABLE RECNO NUMBER;
EXECUTE :RECNO := SYS.DBMS_BACKUP_RESTORE.SETCONFIG('CONTROLFILE AUTOBACKUP','ON');
-- Configure RMAN configuration record 2
VARIABLE RECNO NUMBER;
EXECUTE :RECNO := SYS.DBMS_BACKUP_RESTORE.SETCONFIG('CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE','DISK TO ''%d_%F''');
-- Configure RMAN configuration record 3
VARIABLE RECNO NUMBER;
EXECUTE :RECNO := SYS.DBMS_BACKUP_RESTORE.SETCONFIG('RETENTION POLICY','TO RECOVERY WINDOW OF 7 DAYS');
-- Configure RMAN configuration record 6
VARIABLE RECNO NUMBER;
EXECUTE :RECNO := SYS.DBMS_BACKUP_RESTORE.SETCONFIG('ARCHIVELOG DELETION POLICY','TO NONE');
-- Commands to re-create incarnation table
-- Below log names MUST be changed to existing filenames on
-- disk. Any one log file from each branch can be used to
-- re-create incarnation records.
-- ALTER DATABASE REGISTER LOGFILE '/oradata/fra/HAWKLAS/archivelog/2015_10_02/o1_mf_1_1_%u_.arc';
-- Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
RECOVER DATABASE USING BACKUP CONTROLFILE

-- Set Database Guard and/or Supplemental Logging
ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
-- Database can now be opened zeroing the online logs.
ALTER DATABASE OPEN RESETLOGS;

-- Commands to add tempfiles to temporary tablespaces.
-- Online tempfiles have complete space information.
-- Other tempfiles may require adjustment.
ALTER TABLESPACE TEMP ADD TEMPFILE '/oradata/HAWKLAS/datafile/o1_mf_temp_bg5nb8df_.tmp'
     SIZE 269484032  REUSE AUTOEXTEND ON NEXT 134217728  MAXSIZE 8193M;
-- End of tempfile additions.
--

Commands for sed.

Removes all blank lines:

sed '/^ *$/d' 

Remove all comment lines:

sed '/^--/d' 

Remove all lines after line beginning with semi-colon (;):

sed '/^\;/q' 

Replace RESUSE with SET:

sed 's/REUSE/SET/g' 

Replace Exact Match ARCHIVELOG with NOARCHIVELOG:

sed 's/\<ARCHIVELOG\>/NOARCHIVELOG/g'

Example:
$ grep ARCHIVELOG cf.sql | sed ‘s/\<ARCHIVELOG\>/NOARCHIVELOG/g’

CREATE CONTROLFILE REUSE DATABASE "HAWK" RESETLOGS FORCE LOGGING NOARCHIVELOG
EXECUTE :RECNO := SYS.DBMS_BACKUP_RESTORE.SETCONFIG('NOARCHIVELOG DELETION POLICY','TO NONE');

Note the this does not work since it is not EXACT MATCH

$ grep ARCHIVELOG cf.sql | sed 's/ARCHIVELOG/NOARCHIVELOG/g'
CREATE CONTROLFILE REUSE DATABASE "HAWK" RESETLOGS FORCE LOGGING NONOARCHIVELOG
EXECUTE :RECNO := SYS.DBMS_BACKUP_RESTORE.SETCONFIG('NOARCHIVELOG DELETION POLICY','TO NONE');

Replace database name:

sed 's/"HAWK"/"EAGLE"/g'

Putting it all together.

sed '/^\;/q' cf.sql |sed '/^ *$/d' |sed '/--/d' |sed 's/REUSE/SET/g' |sed 's/\<ARCHIVELOG\>/NOARCHIVELOG/g' |sed 's/"HAWK"/"EAGLE"/g'
or
sed -e '/^\;/q' -e '/^ *$/d' -e '/--/d' -e 's/REUSE/SET/' -e 's/\<ARCHIVELOG\>/NOARCHIVELOG/' -e 's/"HAWK"/"EAGLE"/' cf.sql

STARTUP NOMOUNT
CREATE CONTROLFILE SET DATABASE "EAGLE" RESETLOGS FORCE LOGGING NOARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 2
    MAXDATAFILES 30
    MAXINSTANCES 1
    MAXLOGHISTORY 1168
LOGFILE
  GROUP 1 '/oradata/HAWKLAS/onlinelog/o1_mf_1_bg5n7x0p_.log'  SIZE 100M BLOCKSIZE 512,
  GROUP 2 '/oradata/HAWKLAS/onlinelog/o1_mf_2_bg5n7x51_.log'  SIZE 100M BLOCKSIZE 512,
  GROUP 3 '/oradata/HAWKLAS/onlinelog/o1_mf_3_bg5n7x9m_.log'  SIZE 100M BLOCKSIZE 512
DATAFILE
  '/oradata/HAWKLAS/datafile/o1_mf_system_bg5n8n6k_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_sysaux_bg5n9c44_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_undotbs1_bg5n9scg_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_users_bg5nb91z_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_ggdata_bjysrq7q_.dbf'
CHARACTER SET AL32UTF8
;

Updated to use sed script.

Create sed script:
$ cat cf.sed

/^\;/q
/^ *$/d
/--/d
s/REUSE/SET/
s/\<ARCHIVELOG\>/NOARCHIVELOG/
s/"HAWK"/"EAGLE"/

Run sed script interactive:
$ sed -f cf.sed cf.sql

STARTUP NOMOUNT
CREATE CONTROLFILE SET DATABASE "EAGLE" RESETLOGS FORCE LOGGING NOARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 2
    MAXDATAFILES 30
    MAXINSTANCES 1
    MAXLOGHISTORY 1168
LOGFILE
  GROUP 1 '/oradata/HAWKLAS/onlinelog/o1_mf_1_bg5n7x0p_.log'  SIZE 100M BLOCKSIZE 512,
  GROUP 2 '/oradata/HAWKLAS/onlinelog/o1_mf_2_bg5n7x51_.log'  SIZE 100M BLOCKSIZE 512,
  GROUP 3 '/oradata/HAWKLAS/onlinelog/o1_mf_3_bg5n7x9m_.log'  SIZE 100M BLOCKSIZE 512
DATAFILE
  '/oradata/HAWKLAS/datafile/o1_mf_system_bg5n8n6k_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_sysaux_bg5n9c44_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_undotbs1_bg5n9scg_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_users_bg5nb91z_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_ggdata_bjysrq7q_.dbf'
CHARACTER SET AL32UTF8
;

Run sed script output to file:
$ sed -f cf.sed < cf.sql > newcf.sql
$ cat newcf.sql

STARTUP NOMOUNT
CREATE CONTROLFILE SET DATABASE "EAGLE" RESETLOGS FORCE LOGGING NOARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 2
    MAXDATAFILES 30
    MAXINSTANCES 1
    MAXLOGHISTORY 1168
LOGFILE
  GROUP 1 '/oradata/HAWKLAS/onlinelog/o1_mf_1_bg5n7x0p_.log'  SIZE 100M BLOCKSIZE 512,
  GROUP 2 '/oradata/HAWKLAS/onlinelog/o1_mf_2_bg5n7x51_.log'  SIZE 100M BLOCKSIZE 512,
  GROUP 3 '/oradata/HAWKLAS/onlinelog/o1_mf_3_bg5n7x9m_.log'  SIZE 100M BLOCKSIZE 512
DATAFILE
  '/oradata/HAWKLAS/datafile/o1_mf_system_bg5n8n6k_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_sysaux_bg5n9c44_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_undotbs1_bg5n9scg_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_users_bg5nb91z_.dbf',
  '/oradata/HAWKLAS/datafile/o1_mf_ggdata_bjysrq7q_.dbf'
CHARACTER SET AL32UTF8
;
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: