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?