Royal_C Royal_C - 4 months ago 8
Linux Question

Tar command failed when compressing the sql file

I am trying to compress one sql file using the below tar command. But it is getting failed without any warnings or exceptions in the error log.


Syntax: tar --warning=no-file-changed -zcvf $bkpdir${hadoopdb}mysqldb$bkpday.sql.tar.gz $bkpdir${hadoopdb}mysqldb$bkpday.sql


Is there any way to catch the error when executing the above command.

My Code snippet looks like this.

tar --warning=no-file-changed -zcvf $bkpdir${hadoopdb}_mysqldb_$bkpday.sql.tar.gz $bkpdir${hadoopdb}_mysqldb_$bkpday.sql



if [ "$?" != 0 ]
then
echo "Error while compressing the db backup file"
errmsg
exit 1
fi


Error Message: Error while compressing the db backup file

TAR Version : 1.23


Please do the needful.

Answer

I belive the proper syntax for integer comparison is:

if [ $? -ne 0 ]

The != operator is used for pattern matching.

http://tldp.org/LDP/abs/html/comparison-ops.html


You can direct the error output to a file as well.

tar --warning=no-file-changed -zcvf $bkpdir${hadoopdb}mysqldb$bkpday.sql.tar.gz $bkpdir${hadoopdb}mysqldb$bkpday.sql 2> tar_sql.err

will write errors to the file tar_sql.err.


You can also turn on -e mode. In this mode any command your script runs which returns a non-zero exitcode will cause your script to itself terminate immediately with an error.

You can do that in your shebang line:

#!/bin/sh -e