I am working on a bash script to dump a database to a file to be uploaded via FTP. It will be launched by a cron job and the sql server isn't localhost.
I have the script working but am running into a road block with error checking. If the script cant connect to the sql server (in the unlikely event that it goes down) I want it to stop and output whatever the problem was to an error log. The outputting to error log works fine, but the script still runs and creates a blank file. I am trying to use the $? to check if the mysql connection failed and even when I am using a testing server that is off, a 0 is returned instead of a 1. I'm wearing a hole in the wall that I am bashing my head against, what am I doing wrong?
If this has been asked before, please point me in the right direction but I couldn't find any answers when I searched.
contents of the bash script:
{
mysql -h $HOST -u $USERNAME -p$PASSWORD < dump.sql | sed "s/\t/\",\"/g;s/^/\"/;s/$/\"/" > test.csv
} 2>error.log
if [ $? -ne 0 ]; then { echo "SQL Server connection failed. Aborting" ; exit 1; } fi