LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [help needed] [mysql dumps/restores] [bash script] [output redirection] (https://www.linuxquestions.org/questions/programming-9/%5Bhelp-needed%5D-%5Bmysql-dumps-restores%5D-%5Bbash-script%5D-%5Boutput-redirection%5D-884844/)

Specter1981 06-06-2011 12:29 PM

[help needed] [mysql dumps/restores] [bash script] [output redirection]
 
Hello and thanks for opening my cry out for help. I have been searching the web for an answer to this problem but have failed to find one up to this point and am getting pretty desperate. Don't even know where to ask this question...

So here it is: I am working on a script that is supposed to automatically import a mysql dump to a temporary database, clear some sensitive data, then do a new dump and then send the cleared database to some developpers. For it I need some pretty detailed logs. Here is where I am having the problem:

Code:

mysql -u $MYSQL_ROOT_USER -p$MYSQL_ROOT_PASS ${TEMP_DB} < $WORK_FILE_1 >> $LOG_FOLDER/${DATE_LOG}-movingDB.log 2>&1
My problem is that I can't seem to be logging anything using this method (I was actually hoping that this would be capting all the stdout and stderr streams to ${DATE_LOG}-movingDB.log but that doesn't happen). If I make a mistake in the name of the work file that needs to be imported, well, I get the error message when running the script from the shell but nothing in the logs.

Anyone has any idea what I could do or where I could ask?

Thank you in advance.

carltm 06-06-2011 07:34 PM

It sounds like you want detailed logging of the import process.
By default that doesn't happen. When all is successful, there
is no output from the mysql command.

Try adding the "-v" flag and see if that gives enough output.
If not, keep adding more and see what happens.

Specter1981 06-07-2011 03:17 AM

thank you very much for taking the time to through some advice my way. Very much appreciated. I tried what you suggested, however the verbose mode is too much. What was important for this script was to record if the command succeeds or if it fails.

So I went around it with the following:

Code:

mysql -u $MYSQL_ROOT_USER -p$MYSQL_ROOT_PASS ${TEMP_DB} < $WORK_FILE_1
RESULT=$?
if [$RESULT -eq 0]
then
  echo "...Done!" >> $LOG_FOLDER/${DATE_LOG}-movingDB.log
else
  echo "...FAIL!" >> $LOG_FOLDER/${DATE_LOG}-movingDB.log
fi

This will at least record if the command worked or not.

HOWEVER, I find it hard to believe no one ever needed this before me. I mean if in the command
Code:

mysql -u $MYSQL_ROOT_USER -p$MYSQL_ROOT_PASS ${TEMP_DB} < $WORK_FILE_1 >> $LOG_FOLDER/${DATE_LOG}-movingDB.log 2>&1
I change the root user to some bogus one, then I get the logging I want. If I change the password to a bogus one I get the logging I want (the standard mysql error messages that would be displayed are redirected to the log file). But if I change something AFTER the < character (i.e. the path to the mysql dump that I want to import), then all I get is an error message onscreen and nothing in the log file. Could anyone explain why that is? (or point me in the right direction please?).

Thank you.

carltm 06-07-2011 06:27 AM

Yes, it's a question of what processes each part of the command line.

To start, the shell will replace any variables and set up any redirections.
It then puts all the arguments into a list and starts the command, giving
it the list of arguments. The command then processes the arguments.

Since the redirection is handled by the shell prior to executing the
command, the errors at this stage are not redirected into the log file.


All times are GMT -5. The time now is 06:19 AM.