LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Starting down the bash road, rsync fail question (https://www.linuxquestions.org/questions/programming-9/starting-down-the-bash-road-rsync-fail-question-840526/)

sir-lancealot 10-26-2010 09:32 AM

Starting down the bash road, rsync fail question
 
Actually I guess I could ask, if I have been using linux for 7+ years and wanting to automate more, do I look to bash or something like perl. A co-worker swears by it, but just a .02 to throw out, if one is really easier or better to learn!

So with that, I have a remote server which I want to backup nightly, and report that things went well. I have a website setup, mysql, and basically after the rsync is complete I will curl the site with the ID which resets the 'last checked in time'. A cron scipt checks all the watchdog's, and if one is in an alert stage, it will email/sms, etc. depending on the function.

So the
rsync -av root@thrasher:/mnt/emc/maintenance/backups/ForecastDB/* /var/share/backup/database/forecast

curl http://mymaintenancepage.php?wid=x (where x is the unique ID for that service). That part works fine, so I can check it in, see the time reset, etc. but if my bash script just has the rsync, then the curl, if rsync fails for some reason, does the bash script just continue to the next line and curl it?

If so, how do you change that, and if I have to do this with 5 servers, is it recommended to have 5 separate jobs or one in succession.

Thanks as I take on this new adventure :)

MensaWater 10-26-2010 09:56 AM

You can test whether the rsync was successful or not by putting it in an if/then conditional:

Code:

#!/bin/bash
if rsync -av root@thrasher:/mnt/emc/maintenance/backups/ForecastDB/* /var/share/backup/database/forecast
then curl http://mymaintenancepage.php?wid=x
else echo "ERROR:  The rsync failed."
fi

That tells it to run the rsync and if successful run the curl. If not it will output an error message (to stdout - you can append >2 to the else line (after the quote) to make it output to stderr instead).

Perl is generally more efficient in execution than bash so many people writing big scripts will do it in perl. However, bash is useful for quick and dirty scripts and is used in many big scripts. Of course there are also things like Python, Ruby, PHP. You'll see people argue that one is better than another but it really comes down to personal opinions and those often are based solely on what the user first learned.


All times are GMT -5. The time now is 02:01 PM.