2

I have the following commands in a shell script where I do a mysql dump, then I load that SQL file over ssh into a remote database, and then I update the timestamp.

1. mysqldump -u root files path | gzip -9 > $SQL_FILE
2. cat $SQL_FILE | ssh -i ~/metadata.pem [email protected] 
    "zcat | mysql -u 'root' -h 1.2.3.4 metadata"
3. TIMESTAMP=`date "+%Y-%m-%d-%T"`
4. mysql -u 'root' -h 1.2.3.4 metadata -e "UPDATE path_last_updated SET timestamp=DEFAULT"

Is there any way to improve the above commands. For example, what happens if line 2 fails (for example, due to a connectivity issue), but line 4 succeeds?

How would I make line 4 running conditional on the success of line 2?

1
  • $? contains the int return code of the last run command. 0 is success. Commented Feb 3, 2013 at 22:22

2 Answers 2

2

You could chain all in one block:

mysqldump -u root files path |
    gzip -9 |
     ssh -i ~/metadata.pem [email protected] "zcat |\
                   mysql -u 'root' -h 1.2.3.4 metadata"  &&
    mysql -u 'root' -h 1.2.3.4 metadata -e "
        UPDATE path_last_updated SET timestamp=DEFAULT"

So last mysql command won't be executed if something fail before.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use $? for get return code of last command, if it's not 0, it failed.

Or you can use && for example : cmd1 && cmd2.

Or you can use set -e to stop script if occur an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.