Royal_C Royal_C - 1 year ago 90
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 ]
echo "Error while compressing the db backup file"
exit 1

Error Message: Error while compressing the db backup file

TAR Version : 1.23

Please do the needful.

Answer Source

I belive the proper syntax for integer comparison is:

if [ $? -ne 0 ]

The != operator is used for pattern matching.

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download