i wrote a script that make a dump of mysql dbs, i need to check if the command succfully executed or not. using $? give me always 0 even if there are a connection problem with the db server. any idea how can i do this thanks
1 Answer
Script
#!/bin/sh
mysqldump -h 192.168.1.10 -u user -pPaSSwOrD dbname > filename.sql
if [ "$?" -eq 0 ]; then
echo "Success"
else
echo "Error"
fi
is successfully showing errors for connection timeouts, wrong dbname, wrong username and password. It only doesn't check integrity of dump taken, but it's another story.
4 Comments
Khaled_Jamel
thanks Shader, the problem was that i am using a pipe after the mysql command to compress the output 'mysqldump -u usr -ppwd db | gzip -c > db.sql.gz' so i think that the result of $? concern the gzip command status not the first mysql one. that's it ?
Shader
Yes, that's correct. If we change the first line to
mysqldump -h 192.168.1.10 -u user -pPaSSwOrD dbname | gzip > filename.sql.gz, then $? is referencing to gzip operation. To make it easier, split operation in two: make a dump (and check if it's successful), compress it to .gz (and check if necessary).Shader
Other solution is to put
set -o pipefail before mysqldump ... | gzip > .... In this case pipe will return an error if any of command fails.Khaled_Jamel
thanks again, this last one resolve the problem to me.
mysqldoes provide a valid exit status... stackoverflow.com/questions/21369396/…