RE: ONTAPE as cron
Posted in 1997
Dan
Since more than 1 person was interested in the script, I decided
to also post to the newsgroup.
Heres a copy of my crontab:
---------------------8<----- ~informix/crontab ----8<------------
#
# Informix Crontab
#
# Format:
# Minute Hour Monthday Month Weekday command
# 0-59 0-23 1-31 1-12 0-6 (0=Sunday)
#
0 18 * * 1-6 /dbdump/IDBB.sh
---------------------8<----- end of ~informix/crontab ----8<-----
which says that at 6:00 pm every day of the week except Sunday,
the command /dbdump/IDBB.sh must run. I activate this once using
crontab crontab from ~informix.
Heres a copy of my script. There are 2 configurable parameters
here. They are DUMPDIR which is the cooked FS where you decide
to dump your data and KPERPAGE which is the number of KBytes
your Informix page is.
---------------------8<----- IDBB.sh ----8<-----------------------
#!/bin/ksh
#
# IDBB - Automatic Informix Data Base Backup
#
# Automated backup process. Will read the /infxbkup/IDBB.LST file
# to determine the database instances to back up and the backup
# schedule that needs to be followed. Based on the day of week
# the backup level will be either level 0, 1 or 2. This is a batch
# process which needs to be run by cron.
#
# Author: Sujit Pal
# Date: 12/08/1996
#
#
# Common function to do a level 0, 1 or 2 backup depending on $bklvl
#
backit()
{
#
# Check if enough space available on disk. Heuristic based on 1st run:
# Level 0 gzipped is 20% of used database space.
# Level 1 gzipped is 5% of used database space.
# Level 2 gzipped is 5% of used database space.
#
case $rbklvl in
0) reqspace=$(($dataspace/5))
;;
1) reqspace=$(($dataspace/20))
;;
2) reqspace=$(($dataspace/20))
;;
esac
if [ $reqspace -gt $freespace ]
then
echo "IDBB Error: Level $rbklvl backup for ($onconfig_file) failed. No space." \\
| tee -a $DUMPDIR/IDBB.log
else
echo "IDBB Log: Level $rbklvl Backup will be done for ($onconfig_file)" \\
| tee -a $DUMPDIR/IDBB.log
echo "IDBB Log: Level $rbklvl Backup started: `date`" | tee -a IDBB.log
#
# Make a copy of the $INFORMIXDIR/etc/$ONCONFIG to a backup file.
# sed the TAPEDEV to $instname.l$bklvl.
# Create a FIFO called $instname.l$bklvl with permissions 0777. Set
# up the file to write to "|gzip - > $instname.l$bklvl.gz". Preserve
# the pid of the process. Run the ontape. At the end of the ontape
# remove the pipe and kill the process. Copy the original ONCONFIG
# file to the temporary one.
#
mkfifo $DUMPDIR/$bkdev
chmod 777 $DUMPDIR/$bkdev
gzip - <$DUMPDIR/$bkdev >$DUMPDIR/$bkdev.gz &
bgpid=$!
cp $INFORMIXDIR/etc/$ONCONFIG $INFORMIXDIR/etc/$ONCONFIG.old
sed -e "s%/dev/null%$DUMPDIR/$bkdev%" $INFORMIXDIR/etc/$ONCONFIG > \\
$INFORMIXDIR/etc/$ONCONFIG.new
mv $INFORMIXDIR/etc/$ONCONFIG.new $INFORMIXDIR/etc/$ONCONFIG
if [ `echo $onconfig_file | cut -c1-2` = "tb" ]
then
tbtape -s -L $rbklvl <<EOF
EOF
else
ontape -s -L $rbklvl <<EOF
EOF
fi
ontape_failed=$?
if [ $ontape_failed -ne 0 ]
then
echo "IDBB Error: Level $rbklvl backup failed on Instance ($onconfig_file)" | \\
tee -a $DUMPDIR/IDBB.log
fi
kill -9 $bgpid
mv $INFORMIXDIR/etc/$ONCONFIG.old $INFORMIXDIR/etc/$ONCONFIG
rm -f $DUMPDIR/$bkdev
chmod 0444 $DUMPDIR/$bkdev.gz
echo "IDBB Log: Level $rbklvl Backup ended: `date`" | tee -a IDBB.log
fi
}
#
# Main
#
#
# Include constants. DUMPDIR is the directory that is set up for a
# given host and KPERPAGE is the number of KBytes a DB Page has on
# this host. On HP-UX it is 2 and on AIX it is 4.
#
DUMPDIR=/dbdump
KPERPAGE=2
#
PATH=$PATH:/bin:/usr/bin:/usr/local/binexport PATH
rm -f $DUMPDIR/IDBB.log
#
# Loop through the $DUMPDIR/IDBB.lst file to determine the
# database instance to backup.
#
cat $DUMPDIR/IDBB.lst | while read INST_LIST
do
#
# Set the environment to access the database. Check to
# see if instance is online, if not write to error log
# and loop to the next instance.
#
if [ `echo $INST_LIST | cut -c1-1` = '#' ]
then
continue
fi
onconfig_file=`echo $INST_LIST | awk '{print $1}'`
onconfig_dir=`du -a /usr/informix* | awk '{print $2}' \\
| grep $onconfig_file | uniq`
INFORMIXDIR=`echo $onconfig_dir | awk -F"/" '{print $2,$3}' | \\ sed -e 's/^/\\//' -e 's/ /\\//'`
export INFORMIXDIR
PATH=$INFORMIXDIR/bin:$PATH export INFORMIXDIR
if [ `echo $onconfig_file | cut -c1-2` = "tb" ]
then # This is version 5, use tb commands
TBCONFIG=$onconfig_file
export TBCONFIG
else # This is version 7, use on commands
ONCONFIG=$onconfig_file export ONCONFIG
INFORMIXSERVER=`cat $onconfig_dir | grep "^DBSERVERNAME" | \\ uniq | awk '{print $2}`
export INFORMIXSERVER
fi
is_online=`onstat - | grep "On\\-Line" | wc -l`
if [ $is_online -eq 0 ]
then
echo "IDBB Error: Instance ($onconfig_file) not online" | \\
tee -a $DUMPDIR/IDBB.log
continue
fi
#
# Determine the backup level to be performed today. The
# level is indicated for the instance by a 7-char string
# that gives the backup levels for the week SMTWTFS. 0,
# 1 and 2 represent level 0, 1 and 2 and - indicates no
# backup needed.
#
day=`date | cut -f1 -d" "`
case $day in
"Sun") dow=1;;
"Mon") dow=2;;
"Tue") dow=3;;
"Wed") dow=4;;
"Thu") dow=5;;
"Fri") dow=6;;
"Sat") dow=7;;
esac
bklvl=`echo $INST_LIST | awk '{print $2}' | cut -c$dow-$dow`
if [ $bklvl = "-" ]
then
echo "IDBB Log: No backup needed for ($onconfig_file)" | \\
tee -a $DUMPDIR/IDBB.log
continue
fi
#
# Calculate the used database space against the available
# disk space to determine if the backup should continue.
# Both dataspace and freespace are in 512 K blocks.
#
dataspace=`onstat -d | sed -e '1,/offset/d' -e '/active/,$d' | \\
awk '{dataspace += $5-$6} END {printf("%d", dataspace*2*KPERPAGE)}'`
freespace=`df $DUMPDIR | cut -f2 -d":" | awk '{print $1}'`
#
# If $bklvl = 0, then delete all the backup files. If $bklvl > 0
# then look for previous levels. If they dont exist, create them.
#
instname=`echo $onconfig_file | cut -f2 -d"."`
case $bklvl in
0) if [ -f $DUMPDIR/$instname.l0.gz ]
then
if [ -f $DUMPDIR/$instname.l[12].gz ]
then
rm -f $DUMPDIR/$instname.l[012].gz
else
echo "IDBB Error: Level 0 backup for $onc