Re: Bash Script help...

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



> 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/

# timestamp in YYYY-MM-DD format
TIME_STAMP="$(date +%Y-%m-%d)"

# ip address of remote host to scp files to
REMOTE_IP="10.0.0.X"

# comma separated list of users to receive notification alerts
$AUDIENCE="user1@xxxxxxxxxxx,user2@xxxxxxxxxxx"
cd /system_backups


> tar cvf apache-conf.tar /etc/httpd/conf/*
> gzip -v9 apache-conf.tar

tar cvfz apache-conf.tar.gz /etc/httpd/conf
if [[ $? -ne 0 ]]
then
    echo "Failed" | /bin/mail -s "Apache configuration directory backup failed"
    # no more execution since previous step did not succeed
    exit -1
fi

> tar cvf apache-data.tar /var/www/*
> gzip -v9 apache-data.tar

tar cvfz apache-data.tar.gz /var/www
if [[ $? -ne 0 ]]
then
    echo "Failed" | /bin/mail -s "Apache data directory backup failed"
    exit -1
fi

> then last step...
> tar cvf <current_date>-system_backup.tar <all> the gzip files above
> gzip -v9 <current_date>-system_backup.tar

tar cvfz ${TIME_STAMP}-system-backup.tar.gz apache-conf.tar.gz
apache-data.tar.gz
if [[ $? -ne 0 ]]
then
    echo "Failed" | /bin/mail -s "Apache system backup failed"
    exit -1
fi

>
> scp <current_date>-system_backup.tar.gz user@xxxxxxxx:/.

scp ${TIME_STAMP}-system-backup.tar.gz user@xxxxxxxx:/
if [[ $? -ne 0 ]]
then
    echo "Failed" | /bin/mail -s "Apache scp transfer failed"
    exit -1
else
    echo "Success" | /bin/mail -s "Apache system backup success" $AUDIENCE
    exit 0
fi


> My questions:
> 1. How do I execute each statement and make sure subsequent statements
> are not executed until the previous is done?

if [[ $? -ne 0 ]] statement does that for you

> 2. How do I error check so if a step fails the script stops?

exit X

> 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?

You need to setup password-less login first
Try
http://blogs.translucentcode.org/mick/archives/000230.html

> Can anyone provide insight for me?

This is a very basic script.  You can use functions for error checking
and make the code smaller etc.

> -Jason

Hope this helps.
_______________________________________________
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