Reputation: 110482
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?
Upvotes: 2
Views: 325
Reputation: 70987
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.
Upvotes: 2
Reputation: 9285
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.
Upvotes: 1