Click here to Skip to main content
15,917,875 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I'm trying to make this bash script dispatch and email when it detects that a certain type of file (files ending in ".gz"), which was last modified 24 hours ago, is below a certain size in a directory, I currently have it running on my entire computer. (I have it set to 10 kilobytes).

It just sends an email regardless, I expect it only to send one if it actually detects that the file is under the threshold, and if someone could please go through it and try to tell me where I messed it up that would be great! This is one of my first serious scripts so go easy on the vocabulary!

1) The script will find all of the files in a directory ending with ".gz" that were modified in the last 24 hours.

2) It will check these files and make sure they are above a certain threshold in size (say 10 kilobytes for example)

3) If they are it will do nothing, but if they are under that threshold, an email will be dispatched to the said email address

Bash
#!/bin/bash
for file in /*; do
stat $file
    FAILURE= find . -name "*.gz" -size -10k -mtime -1 -printf 'Failure %p\n'
done
if $FAILURE
then
    echo "The backup test has failed!" | mail -s "BACKUP FAILURE" myemail@gmail.com
fi
Posted
Updated 23-Jun-15 7:00am
v5
Comments
Richard MacCutchan 23-Jun-15 3:55am    
"It isn't working correctly"

You need to explain what that means.
Aiden.JP 23-Jun-15 12:51pm    
It just sends an email regardless, I expect it only to send one if it actually detects that the file is under the threshold.
phil.o 23-Jun-15 4:08am    
Please explain the problem correctly. What do you expect from your script? In what does it differ from what you actually get?
Aiden.JP 23-Jun-15 12:51pm    
It just sends an email regardless, I expect it only to send one if it actually detects that the file is under the threshold.

1 solution

It is some time since I wrote any bash scripts but I think you may need something like:
Bash
#!/bin/bash
# find all .gz files of 10k since yesterday, and count them
cd <directory where the files are stored>
COUNT = `find . -name "*.gz" -size -10k -mtime -1 -print | wc -l`
if [ $COUNT -eq 0 ]
then
    echo "The backup test has failed!" | mail -s "BACKUP FAILURE" myemail@gmail.com
fi

I'm not sure of the complete syntax but it should give you an idea. The for loop and stat command in your script do nothing so are not required.
 
Share this answer
 
v2
Comments
Aiden.JP 23-Jun-15 15:16pm    
0: command not found
[: -eq: unary operator expected

There is an error on the "if" line.
Richard MacCutchan 23-Jun-15 15:26pm    
Like I said, it is some time since I have written bash scripts. You will need to consult the man pages to check the correct syntax.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900