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?
I think a better script would read command line input - something like:
You could then enter the command like this:
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 backuptar...
command, and then echoSuccess
. It will only echo unsuccessful if you enter the continue parameter not asy
. So for this particular script, a better end to it would be (this only changes what isecho
ed in the terminal output):This seems to work:
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.
I am adding another
if
condition in your script. It will work as you wish. See the modified script below.$?
stores the exit status of last command on shell. on success it would be0
This works:
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 anif
statement so its exit code influences which path it takes form theif
statement.