Backup script using Korn Flow Control


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Backup script using Korn Flow Control
# 1  
Old 10-23-2011
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.

Code:
#!/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:

Code:
        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?
# 2  
Old 10-23-2011
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:

Code:
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.
# 3  
Old 10-24-2011
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:

Code:
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:

Code:
*)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:


Code:
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:


Code:
*)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:

Code:
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

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

I will paste the finish product in a few.

Last edited by metallica1973; 10-24-2011 at 11:45 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Flow control state changed in server logs

Hi, We observe below logs from switch - the database servers rebooted becaause they couldn't do I/O on vfiler -Any pointers looking at below logs please? Switch logs: 2016 Apr 30 07:41:16.729 EAG-ECOM-POD111GPU-SWF1 %ETHPORT-5-IF_DOWN_LINK_FAILURE: Interface Ethernet152/1/8 is down (Link... (0 Replies)
Discussion started by: admin_db
0 Replies

2. Shell Programming and Scripting

Help with control flow in a Bash script

In my bash script I want to say "if argument 2 is anything except x, y or z, than echo this" (x y and z being words). So my script looks like this: if ] then echo "unrecognized input: $2" fi This usually works but than I also want to say "if argument 2 IS x, y, or z, but argument 4 is... (4 Replies)
Discussion started by: Jrodicon
4 Replies

3. Shell Programming and Scripting

Ampersand not giving back the control (Korn shell)

OS version : AIX 6.1 Shell : Korn When you 'postfix' a command with ampersand (&) , it is supposed to run in the background and give you back the control. I tested this with ping command (by default it pings every 1 second ) After I ran the below ping command with ampersand, I pressed... (3 Replies)
Discussion started by: polavan
3 Replies

4. Shell Programming and Scripting

Flow Control in CSH

hi , I am new to scripting, i have a doubt can any one pls solve it for me the code is not working set users = (user1 user2 user3) echo The users are echo $users echo Enter the USER NAME set USER_NAME = $< set i = 1; for ( i = 1; i <= $#users; i++ ) if ( $USER_NAME == $users )... (1 Reply)
Discussion started by: Manju87
1 Replies

5. Shell Programming and Scripting

Python script - control flow statements

Hi guys.I'm just beginner of python. I'm just trying to do some analysis on simple input file. it has 6 columns and i want to consider k,l and m,n if i and j are + after that checking which value is greater or lower in k,l and m,n I have included logic header just to explain what I was... (4 Replies)
Discussion started by: repinementer
4 Replies

6. UNIX for Advanced & Expert Users

Script should flow after ftp --Urgent

Hi everyone, The script actually does the ftp and gets the file to the local system. I want to do some manipulations for that file , But after doing ftp , script is not proceding and just a prompt is displayed . .... ftp code here...... .................... ............... echo "FTP... (4 Replies)
Discussion started by: kaaakrishna
4 Replies

7. IP Networking

Disabling 802.3x flow control

I have a server I would like to disable 802.3x flow control on. The host is Linux (CentOS 4.4 x86_64 w/ 2.6.9-42.0.3.EL kernel,) and I'm using the ns83820 driver for the ethernet interface in question. I've tried looking at the driver parameters (modinfo ns83820) and using ethtool (ethtool -a... (0 Replies)
Discussion started by: LivinFree
0 Replies

8. Programming

dilemma in control flow

hello im facing a queer problem when i execute the foll code in unix # include <stdio.h> # include <unistd.h> main(int argc,char *argv) { FILE *fp = fopen("/ras/chirag/fifotest/file.fifo","a"); int i=1; fprintf(fp,argv); printf("I SLEEP"); system("date"); for (i=0;i<50;i++)... (2 Replies)
Discussion started by: tej.buch
2 Replies

9. Shell Programming and Scripting

Tape backup control

Hi all I have a requirement to ensure I do not overwrite a tape that is current. The way in which I propose doind this is by placing a header file at the beginning of the tape containing the creation date of the backup. Before each backup, I will read this file and validate. My question is as... (4 Replies)
Discussion started by: jhansrod
4 Replies

10. UNIX for Dummies Questions & Answers

how to assign correct control flow?

how can i create a script with a correct control flow/loop(?) to provide output from 3 files? file1 one two three file2 yellow red orange file3 banana apples cantaloupes output file: one (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question