A sample C shell maintenance script

Running dbcc checks and performing database backups protect the integrity and recoverability of your Adaptive Server databases. The following sample C shell script calls several isql scripts to help you do this:

#!/bin/csh -f
 if ( -e dbcc_mail.out) then
 	rm dbcc_mail.out
 foreach i (*.dbcc)
 isql -Usa -Ppassword < $i > dbcc_out
 if ( 'grep -c 'Msg 25[0-9][0-9]' dbcc_out' ) then
 	echo "There are errors in" $i >> dbcc_mail.out
 	cat dbcc_out >> dbcc_mail.out
 	echo "Backing up " $i:r >> dbcc_mail.out
 	isql -Usa -Ppassword < $i:r.backup
 mail -s "Backup Report" jjones < dbcc_mail.out

The first set of scripts (one for each database with a file name appended with .dbcc) runs dbcc checkalloc and dbcc checkdb for each database and sends the messages to an output file called dbcc_out.

For example, the script master.dbcc runs dbcc to check the master database:

dbcc checkalloc (master)
dbcc checkdb (master)

The C shell script then runs the grep command to find 2500-level error messages in the dbcc output. The results of the grep command go into an output file called dbcc_mail.out.

Next, the script invokes an isql backup script for each database for which no 2500-level errors occurred and adds the “Backing up database_name” line to dbcc_mail.out. For example, the script master.backup backs up the master database:

use master
dump database master to master_dump

You may want to add appropriate dump transaction commands to your scripts.

If there are 2500-level error messages, the script does not back up the database. At the end of the script, dbcc_mail.out is mailed to the System Administrator “jjones,” who then has a record of fatal dbcc errors and successful backups.

You can tailor the sample shell and isql scripts to suit the needs of your installation.

To have the scripts execute automatically, edit the crontab file, and add an entry similar to this:

00 02 * * * /usr/u/sybase/dbcc_ck 2>&1

This example executes a C shell script called dbcc_ck every morning at 2:00 a.m.