On Fri, May 8, 2009 at 1:12 AM, Jason Todd Slack-Moehrle mailinglists@mailnewsrss.com wrote:
I need to write a script that I will manually start (or a cron job in future) but I need it to do a number of things in order one after another. How do i do that so everything gets dont as the steps depend on each other.
Example:
cd /system_backups/
tar cvf apache-conf.tar /etc/httpd/conf/* gzip -v9 apache-conf.tar
tar cvf apache-data.tar /var/www/* gzip -v9 apache-data.tar
then last step... tar cvf <current_date>-system_backup.tar <all> the gzip files above gzip -v9 <current_date>-system_backup.tar
scp <current_date>-system_backup.tar.gz user@10.0.0.1:/. etc...etc....
My questions:
- How do I execute each statement and make sure subsequent statements
are not executed until the previous is done?
- How do I error check so if a step fails the script stops?
After each command you can check the return code. For example:
touch /tmp/foo RESULT_CODE=$?
A non-zero error code will signify that some sort of error has occurred, so your script can check for these. You can then use the error code in a case statement and either print an error message then halt, or do something to resolve.
- Since I run an SMTP Server on this box can I e-mail myself from
bash the nightly results?
Yes, you can use the mail/mailx commands to send yourself an email. Here's somethign that I use:
#!/bin/sh # # $ID: $
LOG_RECIPIENTS="klowe@rccl.com" LOG_PATH=/usr/local/logs
TODAY=`${DATE} -u +%Y%m%d` SCRIPT_NAME=`basename $0|sed -e 's/.sh//'` SCRIPT_DESCRIPTION="Replace Me With Description" LOG_FILE=${LOG_PATH}/${SCRIPT_NAME}_${TODAY}.log MACHINE_NAME=`hostname`
log_header(){ echo ----------------------------------------------------- echo ${SCRIPT_DESCRIPTION} on ${MACHINE_NAME} echo SCRIPT: $0 echo ----------------------------------------------------- echo echo Script started on `date` echo }
log_footer(){ echo echo Script completed on `date` echo echo ----------------------------------------------------- echo ${SCRIPT_DESCRIPTION} on ${MACHINE_NAME} echo ----------------------------------------------------- }
mail_logs(){ for recipient in ${LOG_RECIPIENTS}; do cat ${LOG_FILE} | ${MAILX} -s "${SCRIPT_DESCRIPTION} for ${TODAY}" ${recipient} done }
exec 1>${LOG_FILE} 2>&1 log_header log_footer mail_logs
- when I want to run the scp to send over the file to another machine
for safety, how can I have it know the password to the machine I am scp'ing to?
Look up "ssh passwordless authentication" on how to generate a public/private key and install.