17

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?

krunal shah
  • 335
  • 1
  • 3
  • 13
  • 1
    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

3 Answers3

20

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
user9517
  • 114,104
  • 20
  • 206
  • 289
3

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
Dmytro Leonenko
  • 454
  • 1
  • 7
  • 24
0

Warn for people reading this question: Sometimes mysqldump gives exit code 0 even if it has error.

For example, this is what I see today:

I have no name!@backup-mysql-manual-20hvd-pp6m2:/$     mysqldump       --host="${MYSQL_HOST}"       --port="${MYSQL_PORT}"       --user="${MYSQL_USER}"       --password="${MYSQL_PASSWORD}"       --all-databases > /tmp/a.sql 
mysqldump: Error: 'Access denied; you need (at least one of) the PROCESS privilege(s) for this operation' when trying to dump tablespaces
I have no name!@backup-mysql-manual-20hvd-pp6m2:/$ echo $?
0
ch271828n
  • 133
  • 5