From: Sujit Pal Subject: Automatic Informix Data Base Backup Date: Mon, 08 Dec 1997 13:14:36 MST Sometime ago I released a backup script which does backups through cron, based on a user-defined backup schedule. The backups are done to the disk and to preserve space it is piped through a compression utility such as compress or gzip. In case of errors it sends a mail to one or more people whose email-ids are stored in an input file. ------------------------------------8<---------------------------------------- # This is a shell archive. Remove anything before this line, # then unpack it by saving it in a file and typing "sh file". # # Wrapped by Sujit Pal on Mon Dec 8 13:13:10 1997 # # This archive contains: # IDBB.dba IDBB.lst IDBB.sh crontab # # Error checking via sum(1) will be performed. LANG=""; export LANG PATH=/bin:/usr/bin:/usr/sbin:/usr/ccs/bin:$PATH; export PATH if sum -r /dev/null 2>&1 then sumopt='-r' else sumopt='' fi echo x - IDBB.dba cat >IDBB.dba <<'@EOF' your_email_address@your_server_name @EOF set `sum $sumopt IDBB.lst <<'@EOF' # Backup List for Scotch Database Instances # ONCONFIG SCHEDULE # FILE SMTWTFS onconfig_file_name 012012- # This implies Level 0 backup of the instance pointed to by # onconfig_file_name on Sunday, Level 1 on Monday, Level 2 # on Tuesday, ... and so on ..., and no backup (indicated # by -) on Sunday. @EOF set `sum $sumopt IDBB.sh <<'@EOF' #!/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 < 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 $onconfig_file already done" \ | tee -a $DUMPDIR/IDBB.log continue fi fi bkdev=`echo $instname".l0"` rbklvl=0 backit ;; 1) if [ -f $DUMPDIR/$instname.l1.gz ] then echo "IDBB Error: Level 1 backup for $onconfig_file already done" \ | tee -a $DUMPDIR/IDBB.log continue fi if [ -f $DUMPDIR/$instname.l0.gz ] then bkdev=`echo $instname".l1"` rbklvl=1 backit else bkdev=`echo $instname".l0"` rbklvl=0 backit bkdev=`echo $instname".l1"` rbklvl=1 backit fi ;; 2) if [ -f $DUMPDIR/$instname.l2.gz ] then echo "IDBB Error: Level 2 backup for $onconfig_file already done" \ | tee -a $DUMPDIR/IDBB.log continue fi if [ -f $DUMPDIR/$instname.l0.gz ] then if [ -f $DUMPDIR/$instname.l1.gz ] then bkdev=`echo $instname".l2"` rbklvl=2 backit else bkdev=`echo $instname".l1"` rbklvl=1 backit bkdev=`echo $instname".l2"` rbklvl=2 backit fi else bkdev=`echo $instname".l0"` rbklvl=0 backit bkdev=`echo $instname".l1"` rbklvl=1 backit bkdev=`echo $instname".l2"` rbklvl=2 backit fi ;; esac done # # Mail the error log information to the email addresses in the # $DUMPDIR/IDBB.dba files. Better still, grep for "IDBB Error" # and if found mail the contents to DBAs. # if [ `cat $DUMPDIR/IDBB.log | grep "IDBB Error" | wc -l` -gt 0 ] then hostname=`hostname` today=`date +%x` cat $DUMPDIR/IDBB.dba | while read dba_email do cat $DUMPDIR/IDBB.log | mailx -s "Backup log for $hostname on $today" \ $dba_email done fi @EOF set `sum $sumopt crontab <<'@EOF' # # Informix Crontab # # This is a sample crontab file that I used. This says to run the backup # job /dbdump/IDBB.sh at 6:00 pm every day for the days 1-6 (Monday thru # Saturday) every day every month. # # 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 @EOF set `sum $sumopt