Simple Email Script

Hi All,

Im completely new to scripting but I was wondering can somebody show me how to write a bash script so that if any file enters a specific directory that an email gets sent?

I am running a few backups on a few different machines and rysncing them to a remote device, For each server I have a directory name of each server so when a new backup file is received by the backup device I want it to send me a simple mail saying that the backup device got it.

The backup device is a readynas 1100 and runs linux so theres no hassle running cron jobs and scripts etc

I have the backups named by date so the file names are different each time for instance

When this file is received i need a mail sent
/ns2/named-2013-03-13_0940.tar

The File name the next day will be
/ns2/named-2013-03-14_0940.tar

Thanks in advance

James

You can try with the following bash script but make sure you test it. There will be many more smart ways to do it.

#!/bin/bash
NOW=$(date +"%Y-%m-%e")
if ls *$NOW*
then
echo 'BACKUP DONE ' | mail -s backupreport ithelpdesk@enovatemedia.co.in
else 'file not found' | mail -s backupreport ithelpdesk@enovatemedia.co.in
fi

Thanks Kirfee

Is there somewhere i should enter where the files are in this script?

The output i am getting from this is :

ls: *2013-03-15*: No such file or directory
./rtapp1.sh: line 6: file not found: command not found
Null message body; hope that's ok

Im presuming that its because I didnt specify the directory

the directory is /RTAPP1/Dailybkup/

Sorry im completely new at this so i know I should add it but havent a clue where

replace line as follows;

ls /RTAPP1/Dailybkup/*$NOW*

Thanks Kirfee Im getting the hang of messing around with that script you gave me on other servers much appreciated. Think I will have to buy a book and teach myself,

I am having one issue on one server however is it possible to run yesterdays date in the script instead of todays date?

The issue I am having on one server is the backup runs close to midnight and sometimes it doesnt finish till after midnight so i was thinking if I can just see that the date before ran okay I can troubleshoot myself from there,

#!/bin/bash
NOW=$(date +"%e%m%y")
if ls /RTAPP1/Dailybkup/Dailybkup.$NOW.bz2
then
echo 'BACKUP Successfull RTAPP1' | mail -s BACKUP.Successfull.RTAPP1 support@xxx.com
else 'file not found' | mail -s Backup_Failed_RTAPP1_MOVE_TO_UNIX_QUEUE support@xxx.com
fi

SC-NAS1100:/emailscripts# cd /RTAPP!
-bash: cd: /RTAPP!: No such file or directory
SC-NAS1100:/emailscripts# cd /RTAPP1
SC-NAS1100:/RTAPP1# ls
Dailybkup
SC-NAS1100:/RTAPP1# cd Dailybkup/
SC-NAS1100:/RTAPP1/Dailybkup# ls
Dailybkup010113.bz2 Dailybkup051212.bz2 Dailybkup100313.bz2 Dailybkup150213.bz2 Dailybkup200113.bz2 Dailybkup251212.bz2
Dailybkup010213.bz2 Dailybkup060113.bz2 Dailybkup101212.bz2 Dailybkup150313.bz2 Dailybkup200213.bz2 Dailybkup260113.bz2
Dailybkup010313.bz2 Dailybkup060213.bz2 Dailybkup110113.bz2 Dailybkup151212.bz2 Dailybkup200313.bz2 Dailybkup260213.bz2
Dailybkup011212.bz2 Dailybkup060313.bz2 Dailybkup110213.bz2 Dailybkup160113.bz2 Dailybkup201212.bz2 Dailybkup261212.bz2
Dailybkup020113.bz2 Dailybkup061212.bz2 Dailybkup110313.bz2 Dailybkup160213.bz2 Dailybkup210113.bz2 Dailybkup270113.bz2
Dailybkup020213.bz2 Dailybkup070113.bz2 Dailybkup111212.bz2 Dailybkup160313.bz2 Dailybkup210213.bz2 Dailybkup270213.bz2
Dailybkup020313.bz2 Dailybkup070213.bz2 Dailybkup120113.bz2 Dailybkup161212.bz2 Dailybkup210313.bz2 Dailybkup271212.bz2
Dailybkup021212.bz2 Dailybkup070313.bz2 Dailybkup120213.bz2 Dailybkup170113.bz2 Dailybkup211212.bz2 Dailybkup280113.bz2
Dailybkup030113.bz2 Dailybkup071212.bz2 Dailybkup120313.bz2 Dailybkup170213.bz2 Dailybkup220113.bz2 Dailybkup280213.bz2
Dailybkup030213.bz2 Dailybkup080113.bz2 Dailybkup121212.bz2 Dailybkup170313.bz2 Dailybkup220213.bz2 Dailybkup281212.bz2
Dailybkup030313.bz2 Dailybkup080213.bz2 Dailybkup130113.bz2 Dailybkup171212.bz2 Dailybkup221212.bz2 Dailybkup290113.bz2
Dailybkup031212.bz2 Dailybkup080313.bz2 Dailybkup130213.bz2 Dailybkup180113.bz2 Dailybkup230113.bz2 Dailybkup291212.bz2
Dailybkup040113.bz2 Dailybkup081212.bz2 Dailybkup130313.bz2 Dailybkup180213.bz2 Dailybkup230213.bz2 Dailybkup300113.bz2
Dailybkup040213.bz2 Dailybkup090113.bz2 Dailybkup131212.bz2 Dailybkup180313.bz2 Dailybkup231212.bz2 Dailybkup301112.bz2
Dailybkup040313.bz2 Dailybkup090213.bz2 Dailybkup140113.bz2 Dailybkup181212.bz2 Dailybkup240113.bz2 Dailybkup301212.bz2
Dailybkup041212.bz2 Dailybkup090313.bz2 Dailybkup140213.bz2 Dailybkup190113.bz2 Dailybkup240213.bz2 Dailybkup310113.bz2
Dailybkup050113.bz2 Dailybkup091212.bz2 Dailybkup140313.bz2 Dailybkup190213.bz2 Dailybkup241212.bz2 Dailybkup311212.bz2
Dailybkup050213.bz2 Dailybkup100113.bz2 Dailybkup141212.bz2 Dailybkup190313.bz2 Dailybkup250113.bz2
Dailybkup050313.bz2 Dailybkup100213.bz2 Dailybkup150113.bz2 Dailybkup191212.bz2 Dailybkup250213.bz2
SC-NAS1100:/RTAPP1/Dailybkup# cd /emailscripts/
SC-NAS1100:/emailscripts# ls
notfound.txt rtapp1.sh rtapp1test.sh support@xxx.com
SC-NAS1100:/emailscripts# ./rtapp1.sh
ls: /RTAPP1/Dailybkup/Dailybkup.220313.bz2: No such file or directory
./rtapp1.sh: line 6: file not found: command not found
Null message body; hope that's ok
SC-NAS1100:/emailscripts#

---------- Post updated at 11:05 PM ---------- Previous update was at 10:52 PM ----------

Never mind some googling around helped me come up with

#!/bin/bash
#NOW=$(date +"%e%m%y")
yest=$(date --date="yesterday")
echo "$yest"
yest=$(date --date="yesterday" +"%e%m%y")

if ls /RTAPP1/Dailybkup/*$yest*
then
echo 'BACKUP Successfull RTAPP1' | mail -s BACKUP.Successfull.RTAPP1 support@xxx.com
else 'file not found' | mail -s Backup_Failed_RTAPP1_MOVE_TO_UNIX_QUEUE support@xxx.com
fi

thanks for your help

Good to see....your issues resolved.