Saturday, March 29, 2008

Bash Shell Scripting Guide

1. Basics
a. The first line of the script must be
#!/bin/bash
b. Make the script exicutable
chmod +x test.sh

2. Conditional Statement
2.1. if statement
syntax:
if [ condition ]
then

commands
fi

2.2. if else statement
syntax:
if [ condition ]
then
commands
else
commands
fi

2.3. if elif statement
syntax:
if
[ condition1 ]
then
commands
elif [ condition2]
then

commands
else
commands
fi

3.1 Relational operators
-eg Equal to
-lt Less than
-gt Greater than
-ge Greater than or equal to
-le Less than or equel to

3.2 File Related Tests
-f file True if file exists and is a regular file
-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
-d file True if file exists and is a directory
-s file True if file exists and its size is greater than zero

3.3 String tests
-n str True if string str is not a null string
-z str True if string str is a null string
str1 == str2 True if both the strings are equel
str1 != str2 True if both the strings are not equel
str True if str is assigned a value and is not null

3.4 Multiple conditions
-a Performs the AND function
-o Performs the OR function

4. Case Statement
Syntax:
case expression in
pattert1) execute commands;;
pattert2) execute commands;;
pattert3) execute commands;;
....
esac

Example:
#!/bin/bash
case `datacut -d" " -f1` in
Mon) commands;;
Tue) commands;;
Wen) commands;;
....
esac

5. Looping Statements
5.1 while loop
syntax:
while [ condtion_is_true ]
do

execute commands
....
done

Example:
while [ $NUM -gt 100 ]
do
sleep
done

5.2 until loop
Syntax:
until [ condition_is_false ]
do
execute commands
done

Example:
until [ -f $FILE ]
do
sleep 5
done

5.3 for loop
Syntax:
for
variable in list
do
execute commands
done

Example:
for I in 1 2 3 4 5
do
echo "The value of I is $I";
done

Example 2:
#!/bin/bash
LIMIT=10
for ((a=1;a<=$LIMIT;a++)) do echo "$a" done
6. Special symbols
$0 Name of the shell script being executed
$1 First parameter passed to the script
$* All the paramerts passes to the script
$# Number of parameters passed to the script
$? Exit status of the last command

7. Read statement
#!/bin/bash
echo "Enter your name:"
read NAME
echo "Hello $NAME, Have a nice day."

8. Functions
Syntax:
Function_name ()
{
statements
}

Example:
#!/bin/bash
sumcalc ()
{
SUM= $[ $1 + $2 ]
echo "Result = $SUM"
}

echo -e "Enter the first number:\c"
read NUM1
echo -e "Enter the second number:\c"
read NUM2
read NUM2

# Call the function
sumcal $NUM1 $NUM2

Friday, March 21, 2008

Standby Database Creation

Steps to create the physical standby database:
1. Take the backup of production database
2. Create standby controlfile
3. Copy the backup, standy controfile and init.ora files to standby box
4. Create necessary directories
5. Edit the init.ora file
6. Mount the database
7. Restore the database
8. Start the recovery

1. Take the backup of production database
rman> run {
allocate channel c1 type disk format '/backup/%d_rman_bkp_%T_p%p_s%s' maxpiecesize 1800M;
allocate channel c2 type disk format '/backup/%d_rman_bkp_%T_p%p_s%s' maxpiecesize 1800M;
allocate channel c3 type disk format '/backup/%d_rman_bkp_%T_p%p_s%s' maxpiecesize 1800M;
allocate channel c4 type disk format '/backup/%d_rman_bkp_%T_p%p_s%s' maxpiecesize 1800M;
backup as compressed backupset databse plus archivelog;
}

2. Create standby controlfile
sql> alter database create controlfile as '/backup/standby_controlfile.ctl';

3. Copy the backup, controfile and init.ora files to standby box
use ftp or scp to copy the files

4. Create necessary directories
$ mkdir adump bdump cdump udump datafiles controlfiles redofiles archive

5. Edit the init.ora file
a. Change controfile path
b. Change the paths of all dump_dest parametes
c. Change the LOG_ARCHIVE_DEST path
d. Add below 3 parametes
DB_FILE_NAME_CONVERT='/old_path1/','/new_path1/' ,'/old_path2/','/new_path2/'
LOG_FILE_NAME_CONVERT='/old_path/','/new_path/'
STANDBY_FILE_MANAGEMENT='AUTO'

6. Mount the database
sql> startup mount pfile='init.ora'

7. Restore the database
rman> run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
restore database;
}

8. Start the recovery
sql> alter database recover manged standby database disconnect;