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?

  • You could just check that the file was created and the last line contains something like -- Dump completed on ... - If there was a problem then this won't be the case. – Ewan Heming Mar 21 '11 at 14:00
  • I am creating dump through shell script so i want to get the idea dump is properly created or not through some status. – krunal shah Mar 21 '11 at 14:10
up vote 11 down vote accepted

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 ]
    echo "Success"
    echo "Mysqldump encountered a problem look in database.err for information"
  • i am not able to return the Success of Failure error message to my email ? Please help sir... – sqlchild Jan 5 '12 at 14:39

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.