# Test dump status when ran from script



## hiatek (May 24, 2010)

Hi Guys,
Just going through setting up a script using dump.
The backup appears to work fine.

I have text output echoing to a log file just regarding times and dates and where its up to in the script.

Question is, is there a way to output something different if the backup fails versus successful?
How do I test if it is successful (in script that is)?

I have been following http://forums.freebsd.org/showthread.php?t=185.


Kind Regards,
Michael Hiatt


----------



## graudeejs (May 24, 2010)

you can 

```
command && echo "success" >&2 || echo "fail" >&2 && exit 1
```

or check $? variable

```
command
if [ $? != 0 ]; then
    echo "fail" >&2
    exit 1
fi
```

or similarly


```
command
[ $? != 0 ] && echo "fail" >&2 && exit 1
```

http://www.grymoire.com/Unix/Sh.html


----------



## hiatek (May 24, 2010)

So going off your fine example from the howto:

dump -0Lauf - /dev/ad0s1d | bzip2 > /path/to/backups/ad0s1d.dump.bz2

To test this within a script would the following suffice?

dump -0Lauf - /dev/ad0s1d | bzip2 > /path/to/backups/ad0s1d.dump.bz2 && echo "success" > &2 || echo "fail" > &2 && exit 1

What are the &2 parts referring to?


Currently I've been echoing to a defined log file like so (without knowing if the job is successful, just that it has reached a point where it should run):
LOGFILE=/path/to/logfile
echo "Backup Job for /usr started" >> $LOGFILE

Would I change the &2's to $LOGFILE?


Sorry this must sound really simple, given I'm writing the script out of necessity and I'm teaching myself as I go, I guess that's where I'm at. Sorry.



Kind Regards and thanks for the help so far though.
Michael Hiatt

EDIT: I found http://steve-parker.org/sh/variables2.shtml
I also took a look at the article you listed.

I made a bit of a test file (had a go):

```
#!/bin/sh

ls /root/a
if [ $? != 0 ]
	then
		echo "FAIL" >> /root/logfile.log
		exit 1
fi

echo "PASS" >> /root/logfile.log
```

If the file /root/a exists, I get a PASS in the logfile.
If the file does not exist, I get a FAIL in the logfile.

So similarly, if I set up a check directly after each call to dump (I'm backing up multiple mount points/device names), I should be golden?
e.g.

```
dump -0Lauf - /dev/ad0s1d | bzip2 > /path/to/backups/ad0s1d.dump.bz2
if [ $? != 0 ]
	then
		echo "FAIL" >> /root/logfile.log
		exit 1
fi
echo "PASS on /dev/ad0s1d" >> /root/logfile.log
```

I guess I get confused what its testing, is it testing the first command dump or the second bzip2? Or is it testing the whole line?

Also I realise this could be made more code friendly, I'm attempting to understand whats going on though before I complicate it more.


Thanks for the help. Appreciated.


----------



## graudeejs (May 24, 2010)

*>&2* means: redirect to stream nr2. Normally it's stderr

about, which one [ $? != 0 ] is testing...frankly I'm not 100% sure... but I think it will work


----------



## hiatek (May 25, 2010)

Sweet,
Gave it a test run and it appears to work as expected.
Thanks Killasmurf86.


----------

