Backup script using Korn Flow Control

I am wiping off the dust to my shell scipting days and had this question: I have this script that I have created, and cannot figure out where my flow control issue is within this script.

#!/bin/ksh
#Basic script to backup server
#Home directories to the external 
#############################################################
## Functions using Expect##

ssh_mysql_bak()
{
expect <<EOD
set timeout 30
exp_internal 1

spawn ssh -p 3202 testser@192.168.0.32 "mysqldump --opt -u root -ppassword mysql > mysql.bak.dump"
expect "testuser@192.168.0.32's password:"
send "password\r"
wait
EOD
}

scp_mysql_bak ()
{
expect <<EOD
set timeout 30
exp_internal 1
spawn scp -P 3202 testuser@192.168.0.32:/home/testuser/mysql.bak.dump $home
expect "testuser@192.168.0.32's password:"
send  "password\r"
wait
EOD
}

############################################################### MAIN ################################################################################

results=$1
home=/home/testuser
backup_dir=/media/caca/extract
date=$(date +%m%d%y)
reg_date=$(date)
id_check=$(whoami)
search=find
archive="cpio -oavc"
mail_to=me@testemail.com   
sendto=/usr/bin/mutt
pinger=`ping -c 2 192.168.0.32`

        if [[ -z $pinger && $id_check = "root" && -d $backup_dir &&  $results = "full" ]] ; then
        cd $home
        # Excludes copying of hidden folders to conserve space while testing
        echo " $date Full Backup Log                                            " > $backup_dir/full_status$date.log
        $search . -depth -ipath './.*' -prune -o -ipath './Downloads*' -prune -o -print | $archive > $backup_dir/full$date.cpio 2>> $backup_dir/full_status$date.log
        ssh_mysql_bak && scp_mysql_bak;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2>> $backup_dir/mysql_status$date.log 
        $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to < $backup_dir/full_status$date.log
        else
                echo ""                                                                                  > $backup_dir/fullerror$date.log
                echo " $reg_date  ----------Full Backup Error Log----------                           " >> $backup_dir/fullerror$date.log
                echo ""                                                                                 >> $backup_dir/fullerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/fullerror$date.log 
                echo " The "Full Backup" did not execute for one of the Following reasons:      "       >> $backup_dir/fullerror$date.log
                echo ""                                                                                 >> $backup_dir/fullerror$date.log
                echo " 1 - Ran as non-root user                                                 "       >> $backup_dir/fullerror$date.log
                echo " 3 - Share not mounted                                                    "       >> $backup_dir/fullerror$date.log       
                echo " 3 - Incorrect Syntax                                                     "       >> $backup_dir/fullerror$date.log
                $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/incerror$date.log
fi

if      [[ -z $pinger && $id_check = "root" && -d $backup_dir && $results = "incremental" ]] ; then
        cd $home
# Searches for only files that have been modified with 24 hours excluding hidden directories to conserve space while testing
        $search . -depth -mtime 0 ! -ipath './.*' ! -ipath './Downloads*' -print | $archive > $backup_dir/inc$date.cpio 2> $backup_dir/inc$date.log
        ssh_mysql_bak 2>&1 /dev/null && scp_mysql_bak 2>&1 /dev/null ;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2> $backup_dir/mysql_status$date.log
        $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to  < $backup_dir/inc$date.log
        else
                echo ""                                                                                 > $backup_dir/error$date.log
                echo " $reg_date  ----------Incremental Backup Error Log----------                           "     >> $backup_dir/incerror$date.log
                echo ""                                                                                         >> $backup_dir/incerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/incerror$date.log 
                echo ""                                                                                 >> $backup_dir/incerror$date.log
      $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/incerror$date.log
fi

case "$results"
in

        full) echo full;;

        incremental) echo incremental;;

        *)echo "Incorrect syntax usage. Use <backup.ksh> {full|incremental} or target directory does not exist. Please verify share exist";;

esac

when my condition finds the host 192.168.0.32 down and unpingable, the test will fail and then go to the else clause and invoke the else section and should perform whats is in it an exit. For example:

        else
                echo ""                                                                                  > $backup_dir/fullerror$date.log
                echo " $reg_date  ----------Full Backup Error Log----------                           " >> $backup_dir/fullerror$date.log
                echo ""                                                                                 >> $backup_dir/fullerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/fullerror$date.log 
                echo " The "Full Backup" did not execute for one of the Following reasons:      "       >> $backup_dir/fullerror$date.log
                echo ""                                                                                 >> $backup_dir/fullerror$date.log
                echo " 1 - Ran as non-root user                                                 "       >> $backup_dir/fullerror$date.log
                echo " 3 - Share not mounted                                                    "       >> $backup_dir/fullerror$date.log       
                echo " 3 - Incorrect Syntax                                                     "       >> $backup_dir/fullerror$date.log
                $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/incerror$date.log

but what happens is, it does not exit and prints both else clauses for the full backup and incremental backup. It is not exiting when the first else clause completes, it will move on to the next else and print it also. So it is printing both Full Backup Error Log and the Incremental Error Log. Where is my issue with flow control?

Simple fix: add an exit 1 after the sendto command so that the script will exit at that point. You'll also want to add one at the bottom of your other else block. It also never hurts to add an exit 0 at the end of the script.

Other things....
First, when testing for equality within [[ and ]] use a double equal and not a single equal. The single equal sign will work, but it's use is discouraged as Kshell might not support it in future releases.

I am also leery that your test for remote host availability will actually work. Ping, at least on my Linux and FreeBSD hosts, will always generate output to stdout and thus your pinger variable will never be empty and thus the -z $pinger will never be true. I would suggest this change:

ping -c 1 $remote-host >/dev/null 2>&1
ping_stat=$?      # 0 for success; non-zero for failure

if (( $ping_stat == 0 )) && [[ $id_check == "root" && -d $backup_dir &&  $results == "full" ]]

This will test the success of the ping based on the return code and not the presence of any text written to stdout.

awesome,

I will try the exit 1. I tried that before but with just an exit in both else statements and I still had issues but will give it another shot. I will also add what you had suggested with the ping stuff, you made it clear that what I was doing wouldnt have worked as designed. Will let you know how it goes.

Regards

---------- Post updated 10-24-11 at 10:39 AM ---------- Previous update was 10-23-11 at 10:36 PM ----------

It appears to have worked except now when I do not specify "full or incremental, it proceeds to execute the "Full Backup" else statement instead of flagging incorrect syntax usage from my case area:

sudo ksh -x ./backup.ksh 
+ results=''
+ home=/home/testuser
+ backup_dir=/media/caca/extract
+ date +%m%d%y
+ date=102411
+ date
+ reg_date='Mon Oct 24 10:27:13 EDT 2011'
+ whoami
+ id_check=root
+ search=find
+ archive='cpio -oavc'
+ mail_to=me@testemail.com
+ sendto=/usr/bin/mutt
+ remote_host=192.168.0.32
+ ping -c 2 192.168.0.32
+ 1> /dev/null 2>& 1
+ ping_stat=1
+ (( 1 == 0 ))
+ echo ''
+ 1> /media/caca/extract/fullerror102411.log
+ echo ' Mon Oct 24 10:27:13 EDT 2011  ----------Full Backup Error Log----------                           '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ''
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ' Make sure you are root and backup share is mounted on server **/media/backup_drive** '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ' The Full' 'Backup did not execute for one of the Following reasons:      '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ''
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ' 1 - Ran as non-root user                                                 '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo $' 2 - Share not mounted\t\t\t\t\t\t\t'
+ 1>> /media/caca/extract/fullerror102411.log
+ echo $' 3 - Incorrect Syntax\t\t\t\t\t\t\t'
+ 1>> /media/caca/extract/fullerror102411.log
+ /usr/bin/mutt -s 'Daily MYSQL Log' me@testemail.com
+ 0< /media/caca/extract/fullerror102411.log
+ exit 1

What should have been executed:

*)echo "Incorrect syntax usage. Use <backup.ksh> {full|incremental} or target directory does not exist. Please verify share exist";;

I no it the smallest little thing. I am missing something somewhere ??

---------- Post updated at 12:13 PM ---------- Previous update was at 10:39 AM ----------

It appears to have worked except now when I do not specify "full or incremental, it proceeds to execute the "Full Backup" else statement instead of flagging incorrect syntax usage from my case area:


sudo ksh -x ./backup.ksh 
+ results=''
+ home=/home/testuser
+ backup_dir=/media/caca/extract
+ date +%m%d%y
+ date=102411
+ date
+ reg_date='Mon Oct 24 10:27:13 EDT 2011'
+ whoami
+ id_check=root
+ search=find
+ archive='cpio -oavc'
+ mail_to=me@testemail.com
+ sendto=/usr/bin/mutt
+ remote_host=192.168.0.32
+ ping -c 2 192.168.0.32
+ 1> /dev/null 2>& 1
+ ping_stat=1
+ (( 1 == 0 ))
+ echo ''
+ 1> /media/caca/extract/fullerror102411.log
+ echo ' Mon Oct 24 10:27:13 EDT 2011  ----------Full Backup Error Log----------                           '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ''
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ' Make sure you are root and backup share is mounted on server **/media/backup_drive** '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ' The Full' 'Backup did not execute for one of the Following reasons:      '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ''
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ' 1 - Ran as non-root user                                                 '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo $' 2 - Share not mounted\t\t\t\t\t\t\t'
+ 1>> /media/caca/extract/fullerror102411.log
+ echo $' 3 - Incorrect Syntax\t\t\t\t\t\t\t'
+ 1>> /media/caca/extract/fullerror102411.log
+ /usr/bin/mutt -s 'Daily MYSQL Log' me@testemail.com
+ 0< /media/caca/extract/fullerror102411.log
+ exit 1

What should have been executed:


*)echo "Incorrect syntax usage. Use <backup.ksh> {full|incremental} or target directory does not exist. Please verify share exist";;

I no it the smallest little thing. I am missing something somewhere ??

---------- Post updated at 05:16 PM ---------- Previous update was at 12:13 PM ----------

I made another adjustment,fixed some of the logic and it fixed my issues. I created a pinger function and just called it when I needed it as opposed to using a variable constant:


pinger ()
{
remote_host=192.168.0.32
ping -c 1 $remote_host >/dev/null 2>&1
ping_stat=$?
}
##############

if [[ $id_check == "root" && -d $backup_dir &&  $results == "full" ]] ; then

                pinger

                if [[ $ping_stat -eq 0 ]] ; then
        cd $home
        ......

I also added exit 1's at the end of conditions


$sendto -s "Daily Backup Log" $mail_to  < $backup_dir/inc$date.log
exit 1
...

I will paste the finish product in a few.