This document discusses various topics related to using the UNIX shell for database administration tasks. It provides examples of common shell commands and techniques including piping, redirection, checking file attributes, conditional statements, loops, reading user input, finding open ports, common UNIX tools like grep, awk, and kill processes. It also discusses setting up aliases and choosing the right shell based on features.
3. DO NOT DANCE
•
examples is not tested on production
•
one size does not fits all
•
before copy and paste, check manual
•
afterwards check manual
4. UNIX SHELL IN DBA
EVERYDAY
• how
to get shell
• echo $SHELL
• how to roll shell
• examples
• real life
• resources
5. SHELL IS SEXY!
#
who | grep -i blonde | date; cd ~; unzip; touch;
strip; finger; mount; gasp; yes; uptime; umount;
sleep
6. SHELL IS SEXY!
•
•
•
•
•
•
•
•
•
#
who - show who is logged on
grep - print lines matching a pattern
mount - mount a filesystem
touch - change file timestamps
strip - discard symbols from object files
finger - user information lookup program
umount - unmount a filesystem
gasp - a preprocessor for assembly programs
yes - output a string repeatedly until killed
who | grep -i blonde | date; cd ~; unzip; touch; strip; finger; mount; gasp;
yes; uptime; umount; sleep
11. WHY ME?
•
read founded and existing scripts
•
automate systems or databases
•
manual or scheduled tasks from shell side
•
monitoring
•
backups
•
multiple databases tasks
15. SHELL
•
sh - Bourne shell
•
ksh - Korn shell
•
bash - Bourne-Again shell
•
csh - C shell
•
tcsh, zsh, rc, es (10+)
http://en.wikipedia.org/wiki/Unix_shell
16. SHELL
Job control
Aliases
Shell functions
Command history
Command line editing
Vi Command line editing
Emacs Command line editing
User name look up
Login/Logout watching
Filename completion
Username completion
Hostname completion
History completion
Builtin artithmetic evaluation
Can follow symbolic links invisibly
Periodic command execution
Custom Prompt (easily)
Underlying Syntax
Freely Available
Can cope with large argument lists
Has non-interactive startup file
Has non-login startup file
Has anonymous functions
List Variables
Full signal trap handling
Local variables
Exceptions
sh
N
N
Y(1)
N
N
N
N
N
N
N
N
N
N
N
N
N
N
sh
N
Y
N
N
N
N
Y
N
N
csh
Y
Y
N
Y
N
N
N
Y
N
Y(1)
Y(2)
Y(2)
N
Y
N
N
N
csh
N
N
Y
Y
N
Y
N
N
N
ksh
Y
Y
Y
Y
Y
Y
Y
Y
N
Y
Y
Y
N
Y
Y
N
Y
sh
N(4)
Y
Y(5)
Y(5)
N
Y
Y
Y
N
bash
Y
Y
Y
Y
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Y
N
Y
sh
Y
Y
Y(5)
Y
N
N
Y
Y
N
tcsh
Y
Y
N
Y
Y
Y(3)
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
csh
Y
Y
Y
Y
N
Y
N
N
N
zsh
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
sh
Y
Y
Y
Y
N
Y
Y
Y
N
rc
N
N
Y
L
L
L
L
L
F
L
L
L
L
N
N
N
Y
rc
Y
Y
N
N
Y
Y
Y
Y
N
es
N
N
Y
L
L
L
L
L
F
L
L
L
L
N
N
N
Y
rc
Y
Y
N
N
Y
Y
Y
Y
Y
20. KSH vs. BASH
•
•
•
•
•
•
•
bash is much easier to set a prompt that displays the current directory
ksh has associative arrays and bash doesn’t
ksh handles loop syntax a bit better
bash handles getting exit codes from pipes in a cleaner way
ksh has the print command which is way better than the echo command
bash has tab completions
ksh has the syntax cd old new which replaces old with new in your
directory and cd over there
!
pwd - /foo/bar/barfoo/one/bar/bar/foo/bar
cd to /foo/bar/barfoo/two/bar/bar/foo/bar
ksh - cd one two
bash - cd ../../../../../two/bar/bar/foo/bar
23. PROCESS RUN
•
cmd1 && cmd2 - run cmd1; if it returns 0 (success), run cmd2
•
cmd1 || cmd2 - run cmd1; if it returns non-zero, run cmd2
•
cmd1 & cmd2 - run cmd1 and also cmd2
•
(ls -1) - run the command "ls -1" in a sub shell
•
cmd1 | cmd2 - run cmd1 and output as input to cmd2
25. REDIRECTION
•
command > outfile - redirect output to file
•
command >> outfile - redirect output and append to file
•
command 2> outfile - redirect STDERR
•
command &> outfile - redirect STDOUT and STDERR
•
tee - redirect STDOUT to file and STDOUT
26. REDIRECTION
#
ls
#
ls > file
#
ls | tee file
#
ls | tee –a file
#
ls | tee file1 file2 file3
mailx –s ‘alert.log errors’ dba@seb.lv <
tail -10 alert_$ORACLE_SID.log | grep ORA-|
tee error.log
28. IF
•
•
•
•
•
•
•
•
•
•
[ -a FILE ]
True if FILE exists.
[ -d FILE ]
True if FILE exists and is a directory.
[ -h FILE ]
True if FILE exists and is a symbolic link.
[ -s FILE ]
True if FILE exists and has a size greater than zero.
[ -r FILE ]
True if FILE exists and is readable.
[ -w FILE ]
True if FILE exists and is writable.
[ -x FILE ]
True if FILE exists and is executable.
[ -O FILE ]
True if FILE exists and is owned by the effective user ID.
[ -G FILE ]
True if FILE exists and is owned by the effective group ID.
[ -L FILE ]
True if FILE exists and is a symbolic link.
29. IF
•
•
•
•
•
•
•
[ -z STRING ]
True of the length if "STRING" is zero.
[ -n STRING ]
True if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ]
True if the strings are equal.
[ STRING1 != STRING2 ]
True if the strings are not equal.
[ STRING1 < STRING2 ]
True if "STRING1" sorts before
"STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ]
True if "STRING1" sorts after
"STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ]
"OP" is one of -eq, -ne, -lt, -le, -gt or -ge.
30. IF
•
[ ! EXPR ]
True if EXPR is false.
•
[ EXPR1 -a EXPR2 ]
True if both EXPR1 and
EXPR2 are true.
•
[ EXPR1 -o EXPR2 ]
True if either EXPR1 or
EXPR2 is true.
31. IF
if [ -f alert_ORCL.log ]; then
echo "alert_ORCL.log exists."
fi
if [ "$STATUS" == "PRIMARY" ]; then
echo "Database status - ${STATUS}"
else
echo "Database not in correct mode (${STATUS})"
exit 0;
fi
if [ "$num_of_beer" -gt "3" -a "$num_of_beer" -lt "5" ]; then
echo "You've worked hard enough for today."
fi
32. FOR WHILE
•
for loop
for FILE in udump/*.trc
do
tkprof $FILE $FILE.out sort=exeela,fchela,prsela
done
•
while loop
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
33. INPUT
•
get data from user
stty -echo
echo -n “Please enter sys password: "
read pw
stty echo
cont=n
echo -n "Do you really want to continue? (y/n) "
read cont
if [ "$cont" != "y" ]; then
echo "Quitting..."
exit
fi
34. OPEN PORTS
echo "Scanning TCP ports..."
for p in {1..1023}
do
(echo >/dev/tcp/localhost/$p) >/dev/null 2>&1 && echo "$p open"
done
35. TOOLSET
•
grep - searching for text pattern
•
awk - script language for text-processing
•
ps - information on running processes
•
find - searching for files
•
sed - stream editor for filtering and transforming text
•
watch - execute a program periodically
•
wc - counting lines/chars
•
cat - join files
http://en.wikipedia.org/wiki/List_of_Unix_programs
38. ONE LINERS
•
http://www.pement.org/awk/awk1line.txt
# delete trailing whitespace (spaces, tabs) from end of each line
awk '{sub(/[ t]+$/, "")};1'
!!
# change "scarlet" or "ruby" or "puce" to "red"
awk '{gsub(/scarlet|ruby|puce/, "red")}; 1'
!
! # print any line where field #5 is equal to "abc123"
awk '$5 == "abc123"'
•
http://sed.sourceforge.net/sed1line.txt
44. ALIAS
•
alias mount='mount |column -t’
/dev/sda1
proc
sysfs
none
none
none
udev
devpts
!
!
•
on
on
on
on
on
on
on
on
/
/proc
/sys
/sys/fs/fuse/connections
/sys/kernel/debug
/sys/kernel/security
/dev
/dev/pts
type
type
type
type
type
type
type
type
ext4
proc
sysfs
fusectl
debugfs
securityfs
devtmpfs
devpts
(rw,errors=remount-ro)
(rw,noexec,nosuid,nodev)
(rw,noexec,nosuid,nodev)
(rw)
(rw)
(rw)
(rw,mode=0755)
(rw,noexec,nosuid,gid=5,mode=0620)
alias path='echo -e ${PATH//:/n}'
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
64. say() {
echo $1 | tee -a oraup_stat.txt
}
!
up_db() {
say ">>>>>>>> ORACLE_SID=$ORACLE_SID, celju augshaa..."
sqlplus "/ as sysdba" <<EOF | tee -a oraup_stat.txt
startup
exit
EOF
}
!
if [ ! -f $ORACLE_HOME/bin/oracle ]; then
say "Nav uzstadita ORACLE_HOME."
exit
else
say "ORACLE_HOME=$ORACLE_HOME"
fi
!
if [ $# -eq 0 ]; then
say ">>> Nav noraditi parametri"
die()
fi
!
while [ $# -ne 0 ]; do
export ORACLE_SID=$1
up_db
done
65. TEST ENV
Ir paceltas sekojoshas datubaazes:
======================================================================================================================
DB SID
Memory
ORACLE_HOME and Product Version
Actual Database Version
---------------------------------------------------------------------------------------------------------------------APVDIG
1029 M /export/home/oracle/product/10.2.0
10.2.0.4.0 64-bit
10.2.0.4.0 64-bit
ARHTEST
545 M /export/home/oracle/product/8.1.7
8.1.7.4.0 32-bit
8.1.7.4.0 32-bit
EBRTEST
1895 M /export/home/oracle/product/11.2.0.
11.2.0.2.0 64-bit
11.2.0.2.0 64-bit
ECRMUAT
1496 M /export/home/oracle/product/11.2.0.
11.2.0.3.0 64-bit
11.2.0.3.0 64-bit
IDM
856 M /export/home/oracle/product/11.2.0.
11.2.0.3.0 64-bit
11.2.0.3.0 64-bit
MBPTEST
1736 M /export/home/oracle/product/11.2.0.
11.2.0.3.0 64-bit
11.2.0.3.0 64-bit
PRO11
515 M /export/home/oracle/product/11.2.0.
11.2.0.2.0 64-bit
11.2.0.2.0 64-bit
RMSTEST
429 M /export/home/oracle/product/10.2.0
10.2.0.4.0 64-bit
10.2.0.4.0 64-bit
ROSMET11
856 M /export/home/oracle/product/11.2.0.
11.2.0.3.0 64-bit
11.2.0.3.0 64-bit
SMTEST
183 M /export/home/oracle/product/9.2.0.1
9.2.0.8.0 64-bit
9.2.0.8.0 64-bit
SYMDW
1511 M /export/home/oracle/product/9.2.0.1
9.2.0.8.0 64-bit
9.2.0.8.0 64-bit
SYMIBM
4903 M /export/home/oracle/product/9.2.0.1
9.2.0.8.0 64-bit
9.2.0.8.0 64-bit
SYMUAT
6503 M /export/home/oracle/product/9.2.0.1
9.2.0.8.0 64-bit
9.2.0.8.0 64-bit
TPENS11
828 M /export/home/oracle/product/11.2.0.
11.2.0.3.0 64-bit
11.2.0.3.0 64-bit
TPENSNEW
588 M /export/home/oracle/product/11.1.0
11.1.0.7.0 64-bit
11.1.0.7.0 64-bit
TRISD
960 M /export/home/oracle/product/11.2.0.
11.2.0.3.0 64-bit
11.2.0.3.0 64-bit
UFTEST
381 M /export/home/oracle/product/10.2.0
10.2.0.4.0 64-bit
10.2.0.4.0 64-bit
VOIS11T
884 M /export/home/oracle/product/11.2.0.
11.2.0.3.0 64-bit
11.2.0.3.0 64-bit
======================================================================================================================
Kopaa DB: 19
26472 M
66. _run_sql() {
selekts=`sqlplus "/ as sysdba"<<endsql
set head off;
set serveroutput on;
declare
x number;
i number;
n number;
comp varchar2(255);
vers varchar2(255);
c varchar2(1000);
begin
select count(*) into x from dba_views where view_name = 'DBA_REGISTRY' and rownum = 1;
if x = 0 then -- versijas zem 9.0.0.0.0
c := 'select product, version from product_component_version where product
like ''Oracle%Enterprise Edition%'' or product like ''Oracle%Server%''';
else -- versijas virs 9.0.0.0.0
c := 'select comp_name, version from dba_registry where comp_name
like ''Oracle%Catalog Views%''';
end if;
i := dbms_sql.open_cursor;
dbms_sql.parse(i, c, dbms_sql.native);
dbms_sql.define_column(i, 1, comp, 255);
dbms_sql.define_column(i, 2, vers, 255);
n := dbms_sql.execute(i);
if dbms_sql.fetch_rows(i) > 0 then
dbms_sql.column_value(i, 1, comp);
dbms_sql.column_value(i, 2, vers);
dbms_output.put_line('>>>'||comp||' '||vers||'<<<');
end if;
dbms_sql.close_cursor(i);
end;
/
select '>>>'||length(addr)*4||'<<<' from v$process where ROWNUM = 1;
exit
endsql`
echo $selekts
}
75. # datu bazes statusa noskaidrosana
STATUS_SQL="select DATABASE_ROLE FROM v$database;nexitn"
STATUS=`echo $STATUS_SQL | sqlplus -S / as sysdba | tail -2| head -1`
!
echo $STATUS
!
if [ "$STATUS" == "PRIMARY" ]; then
echo "Database status - ${STATUS}"
else
echo "Database not in correct mode (${STATUS})"
exit 0;
fi
77. # check for business backup required
PAPILDUS_BACKUP=`cat ${BACKUP_CFG} | grep -v "#" | grep "PAPILDUS_BACKUP.${D_DATE}" | awk
'BEGIN {FS="="} {print $2} '`
IKDIENAS_BACKUP=`cat ${BACKUP_CFG} | grep -v "#" | grep "IKDIENAS_BACKUP.${D_WEEK_DAY}" |
awk 'BEGIN {FS="="} {print $2} '`
!
BACKUP_TIME=28;
!
if [ "${PAPILDUS_BACKUP}" = "MONTH" ] ; then
# start business backup
BACKUP_TIME=120;
BACKUP_TAPE=tdpo_monthly.opt
BACKUP_LEVEL=0;
BACKUP_TAG=${PAPILDUS_BACKUP}` date '+%Y%m' `
#
elif [ "${IKDIENAS_BACKUP}" = "INCR" ] ; then
BACKUP_TIME=28;
BACKUP_TAPE=tdpo.opt
PAPILDUS_BACKUP=INCR
BACKUP_LEVEL=1;
BACKUP_TAG=${PAPILDUS_BACKUP}` date '+%Y%m%d' `
#
elif [ "${IKDIENAS_BACKUP}" = "FULL" ] ; then
BACKUP_TIME=28;
BACKUP_TAPE=tdpo.opt
PAPILDUS_BACKUP=FULL
BACKUP_LEVEL=0;
BACKUP_TAG=${PAPILDUS_BACKUP}` date '+%Y%m%d' `
#
fi
78. cat > $RUNCMD <<!
connect target /;
!
# Parameters set section
CONFIGURE ENCRYPTION FOR DATABASE OFF;
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 28 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE 'sbt_tape' TO 'online_ctl_
%F.ctl';
CONFIGURE CHANNEL DEVICE TYPE 'sbt_tape' PARMS 'ENV=(TDPO_OPTFILE=/usr/tivoli/tsm/
client/oracle/bin64/${BACKUP_TAPE})' FORMAT 'backup_df_%t_%s_%p_%c' MAXPIECESIZE 100G;
CONFIGURE DEVICE TYPE 'sbt_tape' PARALLELISM 2 BACKUP TYPE TO BACKUPSET;
CONFIGURE DEFAULT DEVICE TYPE TO SBT;
CONFIGURE ARCHIVELOG DELETION POLICY TO SHIPPED TO ALL STANDBY;
!
run {
set duplex 1;
backup incremental level ${BACKUP_LEVEL} database tag '${BACKUP_TAG}' KEEP UNTIL TIME
'sysdate + ${BACKUP_TIME}' LOGS;
backup current controlfile;
backup spfile format 'spfile_%d_%s_%T_dbid%I.ora' tag 'spfile ${BACKUP_TAG}';
backup as compressed backupset archivelog all delete input filesperset=20;
}
!
!
#
${ORACLE_HOME}/bin/rman msgno cmdfile=${RUNCMD} log=${RMANLOG} 1>>${RUNLOG} 2>&1
81. HA WORKAROUND
!
•
database is PHYSICAL STANDBY (RULE_1)
•
if dgmgrl show configuration hang (RULE_2)
•
has errors "DMON: Database ora is still
working on the task." (RULE_3)
•
manual failover
95. HAVE YOURSELF A MERRY
LITTLE CHRISTMAS
SQL> set pagesize 0
SQL> set head off
SQL> with Mx as (select 60 as MaxWidth from dual)
select decode
( sign(floor(MaxWidth /2)-rownum)
, 1
, lpad( ' ', floor(MaxWidth /2)(rownum-1)) || rpad( '*', 2*(rownum-1)+1, ' *')
, lpad( '* * *', floor(MaxWidth/2)+3)
)
from all_tables ,Mx
where rownum < floor(MaxWidth /2) + 6;