Shell send error message

4

I have a cron job running that calls a shell script. This sh file backs up the database and saves it to a folder on the server. That's working.

I know that at the time of generating the backup and / or save an error may occur. I would like to send this error to an email or generate some error log, something like that. Does anyone know how to do this?

Follow the script:

#!/bin/bash
# bkpDBData.sh
#
# gera um dump da base de dados, somente dos dados para um zip no servidor
#

# echo 'Iniciando backup SGI!';

local_save_backup=/local/salvar/backups
ano='date +%Y'
path_complete=$local_save_backup'/'$ano

# verifica se existe a pasta do local_save_backup+ano
# se nao existe, entao cria a pasta
if [ ! -d $path_complete ]; then
    # echo "criar pasta";
    mkdir $path_complete
    # echo "pasta criada";
fi

mysqldump -h localhost -u user -psenha -x -e -t nome_da_base | gzip > $path_complete/'date +%Y%m%d_%H%M'_mysql.gz;

# echo 'Fim do backup SGI!';
    
asked by anonymous 26.02.2014 / 19:50

1 answer

3

Any error messages will be sent to stderr , which is the standard error output of the shell. The strerr is identified by the number 2 when you want to do some action with these errors.

So, suppose your cron makes a call of the type:

bkpDBData.sh

Let's change this call to write errors to a log:

bkpDBData.sh 2>/var/log/bkdbdata.log

Next, you can schedule a cron to take the log messages every 1 minute and send them to an email:

mail -s "bkpdbdata" [email protected] < /var/log/bkpdata.log
    
26.02.2014 / 19:55