Re: Bash Script help...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On Fri, May 8, 2009 at 1:12 AM, Jason Todd Slack-Moehrle
<mailinglists@xxxxxxxxxxxxxxx> 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@xxxxxxxx:/.
> etc...etc....
>
> My questions:
> 1. How do I execute each statement and make sure subsequent statements
> are not executed until the previous is done?
>
> 2. 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.


> 3. 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@xxxxxxxx"
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

> 4. 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.
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos


[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux