APEX-AT-WORK no image
Tags:

Linux bash: Save command and command output in logfile

Von Tobias Arnhold 10.01.2009
Creating logfiles in a linux environment can be done with > or >> or &> or tee or script but using these commands inside a shell script which should save both the command and the command output becomes really tricky.

With > and >> you just get the command output.
With tee you just get the command output.
With script you get both but it didn't work inside my shell script.

I went on googleling and found a solution:
Capture the command run in the log file

{
set -x
pwd
set +x
} > yourlog.txt 2>&1

I used this small script inside my backup routine:

#!/bin/bash
{
set -x

echo ------------------------------------------------
echo -- - - - - RMAN LEVEL 0 Backup Script - - - - --
echo -- - - - - - - - - - - - - - - - - - - - - - ---
echo -- - - - - Creator: Tobias Arnhold - - - - - ---
echo ------------------------------------------------
echo

echo ------------------------------------------------
echo -- - - - - - - - Start backup - - - - - - - - --
echo ------------------------------------------------
echo

echo -- Set variables
ORACLE_SID=XE
ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
FRA_HOME=/oracle/flash_recovery_area/XE
ORA_SCRIPTS=/oracle/scripts
DATUM=`date +"%Y%m%d%H%M%S"`
echo

echo -- Delete old log files
rm -r $FRA_HOME/backuplog/XE_*
echo

echo -- Start RMAN backup
echo -- See logfile: $FRA_HOME/backuplog/XE_FULL_BACKUP.log
rman target / nocatalog cmdfile=$ORA_SCRIPTS/FULL_BACKUP.sql log=$FRA_HOME/backuplog/XE_FULL_BACKUP.log
echo

echo -- Delete old INIT.ORA files
rm -r $FRA_HOME/initora/*
echo -- Create new INIT.ORA files
cp /oracle/admin/pfile/init.ora $FRA_HOME/initora
echo

... More commands

echo -- Delete empty directories
find $FRA_HOME/backupset -type d -empty -exec rmdir {} \;
echo

echo -- Rename RMAN backup file
cd $FRA_HOME/backuplog/
mv XE_FULL_BACKUP.log XE_FULL_BACKUP.$DATUM.log
mv XE_ARC_BACKUP.log XE_ARC.$DATUM.log
echo

echo ------------------------------------------------
echo -- - - - - - - - Backup finished - - - - - - ---
echo ------------------------------------------------
echo

set +x
} &> /oracle/flash_recovery_area/XE/backuplog/FULL_BACKUP.log
# write log file

# replace all "+ echo" lines inside the log file
LOG_HOME=/oracle/flash_recovery_area/XE/backuplog
sed "/+ echo/d" $LOG_HOME/FULL_BACKUP.log > $LOG_HOME/tmp
mv $LOG_HOME/tmp $LOG_HOME/FULL_BACKUP.log
exit

Now I have a really nice logfile:

------------------------------------------------
-- - - - - RMAN LEVEL 0 Backup Script - - - - --
-- - - - - - - - - - - - - - - - - - - - - - ---
-- - - - - Creator: Tobias Arnhold - - - - - ---
------------------------------------------------

------------------------------------------------
-- - - - - - - - Start backup - - - - - - - - --
------------------------------------------------

-- Set variables
+ ORACLE_SID=XE
+ ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
+ FRA_HOME=/oracle/flash_recovery_area/XE
+ ORA_SCRIPTS=/oracle/scripts
++ date +%Y%m%d%H%M%S
+ DATUM=20090901095244

-- Delete old log files
+ rm -r /oracle/flash_recovery_area/XE/backuplog/XE_FULL_BACKUP-2009-08-31.log

-- Start RMAN backup
-- See logfile: /oracle/flash_recovery_area/XE/backuplog/XE_FULL_BACKUP.log

-- Delete old INIT.ORA files
+ rm -r /oracle/flash_recovery_area/XE/initora/init.ora
-- Create new INIT.ORA files
+ cp /oracle/admin/pfile/init.ora /oracle/flash_recovery_area/XE/initora

... More log informationen

-- Delete empty directories
+ find /oracle/flash_recovery_area/XE/backupset -type d -empty -exec rmdir {} \;
find: /oracle/flash_recovery_area/XE/backupset/2009_08_30: Datei oder Verzeichnis nicht gefunden
find: /oracle/flash_recovery_area/XE/backupset/2009_08_31: Datei oder Verzeichnis nicht gefunden

-- Rename RMAN backup file
+ cd /oracle/flash_recovery_area/XE/backuplog/
+ mv XE_FULL_BACKUP.log XE_FULL_BACKUP.20090901095244.log
+ mv XE_ARC.log XE_ARC.20090901095244.log

------------------------------------------------
-- - - - - - - - Backup finished - - - - - - ---
------------------------------------------------

+ set +x

Hope it can help some of you who are searching for better looking logs.

Post Tags: