1

I have made a simple backup script

Code:

function backups_remove_file
{
    echo ""
    echo "2. Make Backups Of Files(s)"
    echo ""
    echo "Which File You Wanna To Make Backups?"
    read back
    echo "What's The File's Name Should Be After Backups? And Where?"
    read after
    read -p "Continue (y/n)?" CONT1
    if [ "$CONT1" == "y" ]; then
        tar -cvpzf "$after.tar.gz" "$back"
        echo "Backups File Successful !" ;
    else
        echo "Backups File Unsuccessful !" ;
    fi
}

But there is one small problem I could not fix. When it shows

Which File You Want To Make Backups?

if you write for example "test" after that, and for the next question

What's The File's Name Should Be After Backups? And Where?

if you write for example "test1", then there will be a safety issue if they want to continue or not if type y it will output:

taking: test: can not state: No such file or directory
tar: Exiting with failure status due to previous errors
 Backups File Successful!

The output in that case should be Backups File Unsuccessful!. What should I do to get the expected output in this case?

3
  • well I tried your function in a script and example, and it worked..
    – rusty
    Jan 10, 2014 at 17:58
  • "..No such file or directory..." yes for such cases with non existent targets you'd need a proper catch in your script..
    – rusty
    Jan 10, 2014 at 18:41
  • You should instead just write an alias to tar in the form of bktar='tar -cvpzf "$after.tar.gz" "$back"' or something like that instead of pointlessly creating a function.
    – Braiam
    Jan 10, 2014 at 19:17

3 Answers 3

2

I think a better script would read command line input - something like:

#/bin/bash
echo "Shall back up $1 as $2 - press Ctrl-C to exit if this is not what you want" 
sleep 3
echo "Backing Up..."
tar -cvpzf $2.tar.gz $1
echo "Backing up of $1 Complete"
notify-send -t 8000 "Backing up of $1 complete"
exit

You could then enter the command like this:

COMMAND "/path/to/input" "/path/to/output"

The notify-send will also send graphical notification, so it will notify you when it is done.

This is just my suggestion - I will take a look at yours in a second...


The problem with your script is this. It will say successful, as $CONT1 = y, so it will execute the backup tar...command, and then echo Success. It will only echo unsuccessful if you enter the continue parameter not as y. So for this particular script, a better end to it would be (this only changes what is echoed in the terminal output):

if [ "$CONT1" == "y" ]; then
        tar -cvpzf "$after.tar.gz" "$back"
        echo "Backing up File finished!" ;
else
        echo "Did not backup" ;
fi
exit

This seems to work:

#!/bin/bash
echo ""
echo "Which File you want to backup?";
read input
echo "Where do you you want it to be backed up? (include .tar.gz extension)";
read output
read -p "Continue (y/n)? " CONT
if [ "$CONT" == "y" ]; then
        tar -cpf "$input" "$output" || 'echo "Backing Up Failed" && exit;'
    echo "Backing Up Successful!"
else
        echo "Did not backup" ;
fi
exit

The || after the command means that it will only execute 'echo "Backing Up Failed" && exit;' if the command produces errors, which will cause it to echo that it has failed and then exit. If the command does not produce errors, it will ignore that, echo Successful, and exit.

This may have problems with path's to some files, so you may want to use the other script I suggested...

Hope this helps.

0
2

I am adding another if condition in your script. It will work as you wish. See the modified script below.

#!/bin/bash

function backups_remove_file()
{
    echo "Make Backups Of Files(s)"
    echo "Which File You Wanna To Make Backups?"
    read back
    echo "What's The File's Name Should Be After Backups? And Where?"
    read after
    read -p "Continue (y/n)?" CONT1
    if [ "$CONT1" == "y" ]; then
        tar -cvpzf "$after.tar.gz" "$back"
        if [ $? == 0 ]; then
            echo "Backups File Successful !"
        else
            echo "Backups File Unsuccessful !"
            exit 1
        fi
    else
        echo "Backups File Unsuccessful !" 
    fi
}

backups_remove_file

$? stores the exit status of last command on shell. on success it would be 0

0
1

This works:

function backups_remove_file
{
    echo ""
    echo "2. Make Backups Of Files(s)"
    echo ""
    echo "Which File You Wanna To Make Backups?"
    read back
    echo "What's The File's Name Should Be After Backups? And Where?"
    read after
    read -p "Continue (y/n)?" CONT1
    if [ "$CONT1" == "y" ]; then
        if tar -cvpzf "$after.tar.gz" "$back"; then
            echo "Backups File Successful !"
        else
            echo "Backups File Unuccessful !"
        fi
    else
        echo "User selected no"
    fi
}

The reason is that the line after tar ... is run regardless of the exit code. This means that it will always echo 'successful'. Bash does not automatically raise an exception/error if something went wrong.

This is fixed by using the tar ... in an if statement so its exit code influences which path it takes form the if statement.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .