An Introduction To Linux Shell Scripting For DBAs
An Introduction To Linux Shell Scripting For DBAs
by Casimir Saternos
Learn some basic bash shell scripts for installing, running, and maintaining Oracle
databases on Linux.
About seven years ago, Oracle released the first commercial database on Linux. Since
then, Oracle, Red Hat, and Novell/SUSE have been steadily collaborating on changes to
Linux kernel as they relate to database and application performance. For that reason,
Oracle Database 10g for Linux includes enhancements that are closely related to the
operating system. Now more than ever, DBAs need to have knowledge of and experience
on this platform to best administer the systems under their watch.
One method that is used to automate such tasks is shell scripting. Shell scripts play a
significant role in the Linux System from the time it is installed. Various scripts are
called when the system is started up and shut down. Utilities by Oracle and other third-
party vendors are invoked through shell scripts. Because they can be developed quickly,
they have historically been used for prototyping applications. System Administrators
have taken advantage of the functionality available through shell scripting to provide
solutions that are tailored for the particular requirements and idiosyncrasies of the
systems under their watch.
In this article, I will introduce functionality available through "bash" shell scripting that is
relevant to installing, running, and maintaining Oracle databases on a Linux platform.
Note that this article is designed for Linux scripting beginners or DBAs who are
relatively new to Linux; most experienced Linux sysadmins will not find it helpful.
A shell script is simply a text file containing a sequence of commands. When you run the
file—or script—it executes the commands contained in the file. The term shell simply
refers to the particular command-line user interface you use to communicate with the
Linux kernel. Several different shells are available, including the C shell (csh), Korn shell
(ksh), Bourne shell (sh), and Bourne-Again shell (bash). The shell itself is a command
that reads lines from either a file or the terminal, interprets them, and generally executes
other commands. The Bourne-Again shell incorporates features of the other shells
mentioned and is the one that was used for the scripts this article demonstrates.
The first line in the script file can be used to dictate which shell will be used to run the
script. This is the meaning of the first line found in all of the script examples:
#!/bin/bash
Depending on your background, you may not see any immediate value to shell scripting
as it relates to the DBA’s work. If you do not have experience with UNIX or UNIX-like
systems, the myriad of cryptic commands might be cause for concern. Besides, in
addition to being a relational database, Oracle 10g provides a robust platform for
processing data within the database as well as several methods of interacting with the OS
outside of the database.
However, there are several reasons you might find yourself delving into the world of
shell scripting, including the following:
• You are in a situation in which you must support already existing scripts.
• You want to automate system setup that occurs prior to the installation of the
Oracle software. For instance, you could write a script to check the initial state of
the OS and report any prerequisites that must be met before installation of the
software. The script might also create relevant OS users and groups and set
environmental variables for the users.
• A running Oracle database can be used to execute manual or scheduled tasks.
However, some tasks need to be run when the database is not running. You can
use a script to stop or start a database (as well as a listener or a related database
process). Such an action cannot be initiated from within the database itself.
• You need a mechanism for monitoring the state of a database (i.e. if it is running
and available to process queries). Such a script could also monitor other processes
and resources that are not Oracle-specific to provide a fuller picture of what is
occurring in the system.
• You need to automate backups. Oracle Recovery Manager (RMAN) is a utility
that allows you to develop backup scripts that can run on any platform. You can
call Oracle Recovery Manager from a shell script and use it to perform a wide
range of backup and recovery activities.
• You might have a requirement that is not specific to a single database. Perhaps
you have several databases on a single machine. It may not be advisable to fulfill
the requirement by using a single database, and it introduces potential security
issues as well. Under such circumstances, shell scripting provides a means for
fulfilling the requirement in a manner that does not associate the process with a
single database.
When Not to Use Shell Scripts
Oracle Database includes functionality that extends beyond the traditional definition of
an RDBMS. Like any other piece of software, it uses resources that are provided by the
operating system, but it can “see” and “change” its environment to a much greater degree
than other software. SQL and Oracle’s fixed views provide a picture of the system from
inside the database, whereas shell scripting provides a view of the system from outside of
the database. Shell scripting is not the solution for every problem.
It important to recognize that many aspects of the operating system can be monitored and
modified from within the database. Oracle’s fixed views (the views with a v$ prefix) can
be used to determine the host name of the machine (v$instance) or the platform name on
which the database is running (v$database). The location and other attributes of the files
associated with the database can be determined in this manner as well. The location and
other attributes of datafiles (v$datafile, dba_data_files), temp files (v$tempfile,
dba_temp_files), redo logs (v$logfile), archive logs (v$archived_log), and control files
(v$controlfile) can be queried directly from the database. You can determine information
about the flash recovery area ($recovery_file_dest) from this view, as well as by looking
at some init.ora parameters (db_recovery_file_dest, db_recovery_file_dest_size). The
status of processes (v$process) and memory (v$sga, v$sgastat, and so on) can be queried
as well. There are various built-in PL/SQL packages as well as the ability to create Java
and C database objects that allow for additional access to the underlying OS.
If you are considering scripting for a task that requires a good deal of database access,
scripting is probably not your best option. Later in this article, there is a description of
how to access the database by using SQL*Plus but in many cases, you are better off
approaching the problem by using another language.
The charts below summarize information accessible from within the database itself:
Server/OS Information
Host name where select host_name You can also obtain this
from v$instance;
the instance is information by running the
running following from bash:
hostname
or
uname –n
Session Processes select p.spid, s.username, The spid can be correlated with
s.program
ps –ef results to compare the
information available within the
from v$process p, v$session s
database with OS information for
where p.addr=s.paddr order by a given process.
2, 3, 1
BASH Scripts
Scripts are either called as part of an automated process (with no human intervention) or
run interactively, with a user responding to prompts. As long as you have executable
permission for a file, you can run it from the command line by typing its name. If you do
not have executable permission for the file but do have read permission, you can run the
script by preceding it with sh.
If a script is designed to be run without user input, several options are available for
calling it. You can run a script in the background and continue even if you disconnect, by
entering a command in the following form:
The following examples cover the essential aspects of providing output to view (using
echo), looping, conditional logic, and variable assignment.
print_args.sh. Arguments are words to the right of the command name that are passed
into the script. To access the first parameter, you use the $1 variable. The $0 variable
contains the name of the script itself. The $# variable contains the number of arguments
in the script. A handy way to iterate through all of the parameters passed involves the use
of a while loop and the shift command. This command is what lets you iterate through
all the arguments in the argument list (rather than remaining in an infinite loop).
while [ $# -ne 0 ]
do
echo $1
shift
done
If a script takes a filename as an argument (or prompts a user for a filename) and the file
will be read later in the script, it is advisable to check whether it is accessible and
readable. For example, a recovery script that involves the selection of a backed-up
control file might prompt the user to make a selection that will be used later in the script
to restore the file.
if [ ! -r $1 ]; then # not exists and is readable
echo "File $1 does not exist or is not readable."
exit;
fi
The sequence of characters
if [ ! -r $1 ];
is what actually performs the test. If the contents between the brackets evaluate to true,
the commands that appear between if and fi will be executed. The actual test appears
between the brackets. The exclamation point serves to negate the test you are doing. The
-r option checks to see if the file is readable. What is being tested in this particular case
is the first argument being passed to the script. By using a different test ( -d ), you can
check to find out if a given entry is a directory (see is_a_directory.sh).
doContinue=n
echo -n "Do you really want to continue? (y/n) "
read doContinue
Some files reside in a fixed location for a given Oracle install. You can determine the
Oracle inventory by viewing the /etc/oraInst.loc file. The /etc/oratab file identifies
databases (and other Oracle programs) installed on the server.
get_inv_location.sh. This script is a bit less intuitive than the previous examples. By
breaking down the script into its component commands, you will get a better
understanding of what is being accomplished.
To determine inventory location, you are going to pipe the results of the cat command
(which displays the contents of the file) to grep (a utility that prints lines that match a
given pattern). You are searching for lines that contain the literal inventory_loc .
inventory_loc=/u01/oraInventory
You can redirect standard out to a file, using the > redirection. If the file does not exist, it
is created. If it does exist, it is overwritten.
cat /etc/oraInst.loc|grep -v "#"|grep inventory_loc > tmp
Once you have the record that indicates the inventory location, you want to remove the
portion of the record prior to the equal sign. This time you pipe the results of the cat
command to awk (a pattern-scanning and processing language often used to split up
variable-length fields), which essentially tokenizes the string. The –F option directs awk
to use the equal sign as the delimiter. You then print the second token of this string ($2 ),
which is everything to the right of the equal sign. The result is our inventory location
(/u01/oraInventory).
cat tmp | awk -F= '{print $2}'
There is no particular reason to allow the temporary file (tmp) to remain, so it can be
removed.
rm tmp
list_oracle_homes.sh. If you would like to determine the ORACLE_HOME for a given
database, you have several options. You can log on as the database user and echo the
$ORACLE_HOME variable. You can also search through the /etc/oratab file and select
the name associated with a given instance. Database entries in this file are of the form
$ORACLE_SID:$ORACLE_HOME:<N|Y>:
The following one-liner prints out the ORACLE_HOME of an entry with an
ORACLE_SID of TESTDB:
cat /etc/oratab | awk -F: '{if ($1=="TESTDB") print $2 }'
search_log.sh. A variety of logs are generated by Oracle products, and you might be
interested in monitoring them. The database alert log contains messages that are critical
to database operations. Log files are also generated when products are installed or
deinstalled and when patches are applied. The following script iterates over a file passed
to it as an argument. If any lines are found that contain ORA-, an e-mail message is sent
to a designated recipient.
Based on the principles introduced in this script, a larger one can be written and
scheduled to execute at periodic intervals that will search the contents of the alert log (or
another file of interest) and send an e-mail if any errors exist. Then the contents of the log
can be moved to another file, so that only the most recent error messages will be sent via
e-mail.
Oracle Recovery Manager Scripts. Oracle Recovery Manager (RMAN) is a utility that
can be used to administer database backup and recovery. This greatly simplifies
administration on multiple platforms, because all backup scripts can be written to be run
by RMAN, reducing the amount of platform-specific code. RMAN can be called by the
underlying operating system and passed a script. For example, a cold (cold.sh) backup
might consist of the following script:
#!/bin/bash
rman target / <<EOF
shutdown immediate;
startup mount;
backup spfile;
backup database;
alter database open;
delete noprompt obsolete;
quit;
EOF
Line 1 indicates that you are using the bash shell. Line 2 invokes Oracle Recovery
Manager and specifies the OS user login to the target database (specified in the
environmental variable $ORACLE_SID). The <<EOF following this indicates that the
subsequent commands will be passed into RMAN to be processed. The EOF on the last
line indicates that you have reached the end of the series of commands to be passed into
RMAN. RMAN is then used to shut down the database, start up and mount the database,
and proceed to back up the server parameter file and the contents of the database. The
database is then opened. Any backups that are older than those specified in the retention
policy are then deleted. See the RMAN documentation to construct a backup that is
relevant for your situation.
Nightly backups are commonly scheduled and run automatically. The script above could
be called and the contents of standard out sent to an e-mail address with the following
command:
sh cold.sh | mail -s"Backup ‘date‘" administrator@yourcompany.com
Other Oracle utilities can be run from within shell scripts as well. The tnsping utility can
be used to see if a given Oracle connection identifier can contact the listener. You might
run this utility to check for connection problems:
Database Installation. Many of the steps involved in the setup of a database can be
automated. Before you install Oracle 10g on Linux, you need to run various tests to
verify the minimum required version of packages and the settings of kernel parameters.
You can query the version of a package by using the rpm command with the –q option.
rpm -q compat-libstdc++
You can determine various aspects of the system by looking at the /proc “virtual” or
“pseudo” file system. It contains not real files but, rather, runtime system information that
can be viewed as if it resided in files. For instance, /proc/meminfo contains memory
information for the system, and grep MemTotal /proc/meminfo displays the total
memory of the system. By using awk as you did earlier, you could isolate the amount of
memory in kilobytes, by using:
Database Monitoring. The ps command can be used to report process status and to
check to see if a database, listener, script, or any other process of interest is running. If
you want to list all the databases that are currently running on a server, you can run the
following command:
echo "‘ps -ef | grep smon|grep -v grep|awk '{print $8}'| awk -F \"_\"
'{print$3}'‘"
Although this is functional, it is a bit difficult to understand at first glance. The first
command, ps (using the -ef options for a full listing of all processes), finds all of the
processes running on the server. The next, grep, searches for SMON (the Oracle System
Monitor background process), which indicates that the database is running. You want to
remove entries that refer to the grep command itself, which is being run. You then use
awk to locate the eighth column in the listing, which contains the system monitor process
name in the form ora_smon_<oracle_sid>. The last instance of awk then uses the
underscore character as the delimiter to search for and print the database name that owns
the SMON process. The underscore character needs to appear in quotes, and the
backslash is used before each of the quotes to escape those quotes (because the entire
string appears within a set of double quotes).
exec_sql.sh. As mentioned earlier, it is possible to query the database from a shell script
provided the user has access to sqlplus. The following example returns a (space-
delimited) list of machines that are currently maintaining sessions in the database:
#!/bin/bash
echo $output
This script is similar to the previous RMAN script in that you are inputting commands
into another program. A local OS authenticated connection is made to the database as
sysdba. To prevent extraneous messages from being returned, this script turns off the
heading, feedback, and verify options of SQL*Plus. The query is executed and SQL*Plus
is exited.
Note the double-backslash before the dollar sign in the view name. These are required
escape sequences within the string: The first slash escapes the second slash, which
escapes the dollar sign. Again, not pretty, but functional.
As mentioned previously, if you are going to be writing something that requires extensive
database access, shell scripting is not the best option. It might be better to rewrite the
script in PL/SQL, Perl (which uses syntax similar to that used in shell scripting), Python,
Java, or another language of your choice.
Conclusion
Shell scripting can be an effective tool for quickly automating repetitive and error-prone
administration tasks. The examples in this article provide an introduction to the
possibilities available but are far from comprehensive. Every system has distinct quirks
and foibles and a unique configuration. An administrator will develop unique solutions to
meet the needs of the particular system.