In my previous post we looked at a simple bash backup script example, which would sync data between servers using “rsync” and then send us an email.
The only problem with this script is if “rsync” fails, we would still get a successful email.
Now I am sure you don’t want to find out on the day you really need them that you don’t have them 🙂 .
To avoid such a catastrophe, we need our bash script to do the following
1. Test if “rsync” was successful.
2. If it was successful then send us a successful email.
3. Else send us an email informing us that the backup failed.
Lets add these conditions in our code.
[user@linux-vps ~]$vim backup #!/bin/bash #Author Leo G rsync -avz -e "ssh " /path/to/yourfile email@example.com:/backup/ if test $? -eq 0 then echo "backup for $(date) "| mail -s "backup complete" firstname.lastname@example.org else echo "backup for $(date) "| mail -s "backup failed" email@example.com fi
“if” bash keyword
Bash has a few built-in keywords to help you run commands based on conditions. “if” is one of them and it’s syntax is as follows.
if condition then do something else do something else fi
“test” is a bash built-in command which will test our expression and return True or False
Exit Status of a command
“$?” is a special variable which stores the exit status of a command. If your command ran successfully then it will return “0” and this will be stored in “$?”. The exit status ranges from 0 to 255, you can read more about it here
Bash Comparison Operators.
“-eq” is a comparison operator and compares the value of “$?” to 0.
We can use the following operators for numeric comparison.
-eq Is Equal To
-ne Is Not Equal To
-gt Is Greater Than
-ge Is Greater Than or Equal To
-lt Is Less Than
-le Is Less Than or Equal To
And the following for String or word comparison
== Is Equal To
!= Is Not Equal To
> Is Greater Than if
>= Is Greater Than Or Equal To
BASH double brackets
I used the “test” command in this example along with the “if” statement for explanation purposes. The “if” statement in bash itself will check the exit status, so you do not need to use “test”.
[user@linux-vps ~]$vim backup #!/bin/bash #Author Leo G if rsync -avz -e "ssh " /path/to/yourfile firstname.lastname@example.org:/backup/ then echo "backup for $(date) "| mail -s "backup complete" email@example.com else echo "backup for $(date) "| mail -s "backup failed" firstname.lastname@example.org fi
You can also use “[” instead of test, "[[” will be the extended form which will allow you yo use logical operators like &&. You can read about it in detail here
Now, we may not be around when the backup fails, but we would still like to know why it did. So how do we tell our script to send us the error message?
In order for our bash script to tell us what went wrong, we need to redirect the error to a file and then attach the file or it’s content in our email.
In Linux everything is a file, so INPUT (STDIN), OUTPUT (STDOUT) and ERROR (STDERR) are also files and can be accessed via File Descriptors. File Descriptors are numeric(integer) values of these files.
The numeric value for STDIN is 0, STDOUT is 1 and STDERR is 2.
Let’s add this redirection to our bash script.
[u[user@linux-vps ~]im backup #!/bin/bash #Author Leo G if rsync -avz -e "ssh " /path/to/yourfile email@example.com:/backup/ 2> /tmp/error.txt then echo "backup for $(date) "| mail -s "backup complete" firstname.lastname@example.org else mail -s "backup for $(date) failed" email@example.com
There you go, a much more intelligent script, That will help us debug much faster in case of a failure.
For more advanced bash scripting I recommend your read text printing with the AWK command
Is a Linux Hobbyist and Enthusiast. He Strongly believes in OpenSource Software and would like you to view and download his software at https://github.com/Leo-g
Latest posts by Leo G (see all)
- Getting started with Machine learning on Linux with Python 3 and Scikit-learn - December 31, 2016
- Running Asynchronous background Tasks on Linux with Python 3 Flask and Celery - October 10, 2016
- Installing and Securing Nginx Websites on Linux with a Free SSL/TLS Certificate from Let’s Encrypt - June 8, 2016