Here is a simple one, that keeps 2 copies (one for odd-numbered days and
one for even-numbered days), and emails the admin with the status:

#!/bin/sh

#This script is to back up the production databases
DBLIST="db1 proddb1 proddb2"

PGUSER=dbusername
DUMPDIR=/disk1/database/backups

MAILCMD=/bin/mail
ADMIN_EMAIL='[EMAIL PROTECTED]'
MAILLOG=/tmp/maillog$$
MAIL_SUBJECT="Database Backup Process"
LOGFILE=/disk1/homedir/database/db_backup.log

function MAILERROR {
  $MAILCMD -s "$1" "$ADMIN_EMAIL" <$MAILLOG
  rm $MAILLOG
  exit 1
}


if ! cd $DUMPDIR ; then
  echo "Cannot cd into $DUMPDIR for backup process" >>$LOGFILE
  MAILERROR "$MAIL_SUBJECT - Error during cd into $DUMPDIR"
fi

#=== we are going to create 2 sets of dump files, for odd and even days ===
#get the day of the year
NUM=`date '+%j'`
#if we don't delete the leading 0, the shell thinks it is an octal number
NUM=${NUM##0}
RES=$(( $NUM % 2 ))

for DBNAME in $DBLIST; do
  echo "Backup of $DBNAME started at $(date)" >>$MAILLOG

  DUMPFILE=dump_${DBNAME}_${RES}

  echo -e "\nStarting backup of $DBNAME to $DUMPFILE at $(date)" >>$LOGFILE
  echo -e "\nStarting backup of $DBNAME to $DUMPFILE at $(date)" >>$MAILLOG

  #delete the old file(s)
  if [ -e $DUMPFILE ]; then
    /bin/rm $DUMPFILE* 2>>$LOGFILE
  fi

  #using postgresql custom dump format, with compression; allows selective
restore
  if ! /usr/local/pgsql/bin/pg_dump -Fc -U $PGUSER $DBNAME >${DUMPFILE}
2>>$LOGFILE  ; then
    echo "Error during pg_dump of $DBNAME" >> $LOGFILE
    echo "Error during pg_dump; see $LOGFILE for details" >> $MAILLOG
    MAILERROR "$MAIL_SUBJECT - Error during pg_dump of $DBNAME"
  fi

  #record process success
  echo -e "Backup of $DBNAME complete at $(date)\n" >>$LOGFILE
  echo -e "Backup of $DBNAME succeeded $(date)\n\n" >>$MAILLOG
done

MAIL_SUBJECT="$MAIL_SUBJECT - Backup of databases succeeded"
$MAILCMD -s "$MAIL_SUBJECT" "$ADMIN_EMAIL" <$MAILLOG
rm $MAILLOG

exit 0


Hope this helps.
Susan


                                                                                
                                                          
                           alexandre - aldeia                                   
                                                          
                      digital                        To:       
pgsql-general@postgresql.org                                               
                      <[EMAIL PROTECTED]>         cc:                           
                                                       
                           Sent by:                  Subject:  [GENERAL] 
pg_dump error codes                                              
                                                                                
                                                          
                                                      |-------------------|     
                                                          
                      [EMAIL PROTECTED]         | [ ] Expand Groups |           
                                                    
                      tgresql.org                     |-------------------|     
                                                          
                                                                                
                                                          
                                                                                
                                                          
                           01/02/2006 02:45                                     
                                                          
                      AM                                                        
                                                          
                                                                                
                                                          
                                                                                
                                                          




Hi,

(maybe an idiot question)

In my Linux bash backup scripts, I wish to send an e-mail when an error
occurs in pg_dump proccess. And if possible, I want to send the error
output via e-mail.

Anybody knows how to capture the output and send this to an e-mail ONLY
if an error occurs ?

Thanks

Alexandre

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings





----------------------------------------------------------------------------------------------
Simply protected storage solutions ensure that your information is
automatically safe, readily available and always there, visit us at 
http://www.overlandstorage.com
----------------------------------------------------------------------------------------------


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to [EMAIL PROTECTED] so that your
       message can get through to the mailing list cleanly

Reply via email to