0

I want to save error if error comes, into a file. But error not redirecting into file.

I get below error and its not redirecting into file.

mysqldump: Couldn't execute 'SHOW FIELDS FROM `v_comloc`': View 'nextgendev_new.v_comloc' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them (1356)

My attempt

HOST="localhost"
PASS="aaaaa"
LOCAL_DIR="/userBackupDrive/backupDatabases/"           #Modified local directory
DAY="`date +%a`"
DATE="`date +%F`"
USER="root"

for db in `mysql -h $HOST -u $USER -p$PASS -e "show databases"`
     do
         mysqldump --opt --routines --single-transaction -p$PASS -u $USER -h $HOST --databases $db | gzip -9 > $LOCAL_DIR$db.$DAY.sql.gz  2>>/var/log/failedJobs/mysqlCronJob.log  

                RESULT=$?
                if [ $RESULT -eq 0 ] ; then
                        echo
                else
                        echo "$DATE__  $db backup not successful" >> /var/log/failedJobs/dbError.log

                fi
     done
addi jeo
  • 3
  • 2

2 Answers2

1

You were redirecting errors from gzip, not from mysqldupmp.

Put redirection before pipe, like this:

mysqldump --opt --routines --single-transaction -p$PASS -u $USER -h $HOST \
          --databases $db 2>>/var/log/failedJobs/mysqlCronJob.log \
         | gzip -9 > $LOCAL_DIR$db.$DAY.sql.gz
Eddie C.
  • 535
  • 1
  • 3
  • 12
tbielaszewski
  • 441
  • 2
  • 5
0

You seem to be trying to redirect standard output (">") twice, which doesn't work.

Perhaps you want this:

     mysqldump --opt --routines --single-transaction -p$PASS -u $USER -h $HOST --databases $db 2>/var/log/failedJobs/mysqlCronJob.log | gzip -9 > $LOCAL_DIR$db.$DAY.sql.gz 
Gerard H. Pille
  • 2,569
  • 1
  • 13
  • 11
  • Thank you, it working for output redirect as guided by you. But why exit code giving `zero` while there is error in db backup for few databases. ? – addi jeo Aug 20 '20 at 11:33
  • That is probably the exit code of gzip, gzip has no problem. In stead of testing the exit code, you could test if mysqlCronJob.log is not empty. – Gerard H. Pille Aug 20 '20 at 14:23