Обсуждение: pg_dump error codes

Поиск
Список
Период
Сортировка

pg_dump error codes

От
alexandre - aldeia digital
Дата:
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

Re: pg_dump error codes

От
Jon Jensen
Дата:
On Mon, 2 Jan 2006, alexandre - aldeia digital wrote:

> 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 ?

I wrote a generic bash shell script to handle this kind of thing in cron.
See the attachment. As long as the program you're running returns an exit
value of 0 when it succeeds, and a non-zero value when it fails, this will
work. (Thought it's not documented in pg_dump's man page, it does return
sane exit values.)

Just do something like:

/path/to/cron-harness pg_dump your-arguments-here

If pg_dump succeeds, nothing will be output. If it fails, all the normal
output will be returned (and if this is a cron job, mailed to you, by
default).

Check the arguments to mktemp, which may vary on your platform. Any
suggestions or improvements are welcome!

HTH,
Jon


--
Jon Jensen
End Point Corporation
http://www.endpoint.com/
Software development with Interchange, Perl, PostgreSQL, Apache, Linux, ...

Вложения

Re: pg_dump error codes

От
Michael Fuhr
Дата:
On Mon, Jan 02, 2006 at 08:45:28AM -0200, alexandre - aldeia digital wrote:
> 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 ?

This is more of a shell scripting question than a PostgreSQL question.
See your shell's documentation and read about I/O redirection and
control structures like "if".

Here's a simple but only minimally-tested example that might give
you some ideas:

#!/bin/sh

dumpout=/tmp/dump.out.$$
dumperr=/tmp/dump.err.$$
erruser=root

trap "rm -f $dumperr $dumpout; exit" 1 2 15

if ! pg_dump "$@" > $dumpout 2> $dumperr
then
    rm -f $dumpout
    mail -s "Dump errors" $erruser < $dumperr
fi

rm -f $dumperr

--
Michael Fuhr

Re: pg_dump error codes

От
SCassidy@overlandstorage.com
Дата:
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='adminemail@mycompany.com'
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
                  
                      <alexandre@ad2.com.br>         cc:
                  
                           Sent by:                  Subject:  [GENERAL] pg_dump error codes
                  

                  
                                                      |-------------------|
                  
                      pgsql-general-owner@pos         | [ ] 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
----------------------------------------------------------------------------------------------


Re: pg_dump error codes

От
alexandre - aldeia digital
Дата:
Michael Fuhr wrote:
> On Mon, Jan 02, 2006 at 08:45:28AM -0200, alexandre - aldeia digital wrote:
>>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 ?
>
> This is more of a shell scripting question than a PostgreSQL question.
> See your shell's documentation and read about I/O redirection and
> control structures like "if".

I know, but I don't found if the pg_dump returns some error code after a
problem.

>
> Here's a simple but only minimally-tested example that might give
> you some ideas:
>
> #!/bin/sh
>
> dumpout=/tmp/dump.out.$$
> dumperr=/tmp/dump.err.$$
> erruser=root
>
> trap "rm -f $dumperr $dumpout; exit" 1 2 15
>
> if ! pg_dump "$@" > $dumpout 2> $dumperr
> then
>     rm -f $dumpout
>     mail -s "Dump errors" $erruser < $dumperr
> fi
>
> rm -f $dumperr
>

Thank you very much!  :)
This is all I need...


Alexandre