Does mysqldump return a status?

I am creating a script that backups a mysql db using the mysqldump utility. I am writing this script in the shell "sh". I would like to capture the output status of mysqldump in the script (i.e. if the mysqldump command failed or succeeded) so I can report if the script was successful or not.

  • Does mysqldump return an output status?

  • Can someone please give me instructions on how to do this?


mysqldump returns

0 for Success
1 for Warning
2 for Not Found

It also prints an extended error message to stderr e.g.

mysqldump: Got error: 1049: Unknown database 'dbname' when selecting the database

You can inspect the returned value like so

mysqldump -u DBuser -pDBpassword database >database.sql 2>database.err 
if [ "$?" -eq 0 ]
then
    echo "Success"
else
    echo "Mysqldump encountered a problem look in database.err for information"
fi

After dump finished check $? shell variable. If it's 0 - all went fine. Else - error.

# mysqldump -u aaa -d msf>/dev/null
mysqldump: Got error: 1045: Access denied for user 'aaa'@'localhost' (using password: YES) when trying to connect
# echo $?
2