Little Brain
Little Brain

Reputation: 2847

mysqldump gives exit code 0 on failure e.g. because database doesn't exist

I'm writing a simple script to backup a mysql database:

#!/bin/sh

ROOTDIR=/home/dbusername
LOGDIR=$ROOTDIR/logs
DB_BACKUP_PATH=$ROOTDIR/db_backup
LOGFILE=$LOGDIR/db_backup.log
TODAY=`date +"%Y-%m-%d"`

MYSQL_HOST='localhost'
MYSQL_PORT='3306'
DATABASE_NAME='idontexist'

#################################################################

mkdir -p ${DB_BACKUP_PATH}/${TODAY}
echo "Backup started for database - ${DATABASE_NAME}"


mysqldump -h ${MYSQL_HOST} -P ${MYSQL_PORT} ${DATABASE_NAME} > ${DB_BACKUP_PATH}/${TODAY}/${DATABASE_NAME}-${TODAY}.sql 2> $LOGFILE
 echo "exit code:"
 echo $?

When I run the script, the exit code printed on the screen is 0 even though the script failed because the database doesn't exist. The correct verbose message is saved in my logfile.

Any idea why I'm seeing exit code 0 on failure? Thanks!

Upvotes: 1

Views: 742

Answers (1)

Bayou
Bayou

Reputation: 3441

You're echoing the exit code of echo, not the exit code of mysqldump.

Change

echo "exit code:"
echo $?

to

echo "exit code: $?"

Upvotes: 2

Related Questions