1830045 Members
16872 Online
109998 Solutions
New Discussion

shell script!

 
SOLVED
Go to solution
ajay_25
Occasional Contributor

shell script!

Hi Everybody,

In my backup directory (Linux Os) everyday one file will be created automatically by cron. I want to check the size of the file which is created today with yesterdays file. if todays file is smaller than y'days file then delete todays file and send mail to specific user to report the problem.

please guide me how to write a shell script with above specification.

Your help will be highly appreciated.

Thanks in advance.

ajay.
6 REPLIES 6
Rajesh SB
Esteemed Contributor

Re: shell script!

Hi,

############################
yest_file=/home/temp/fil1
todays_file=/home/temp/file2

if [ -f ]
then
fsize_td=`/usr/sbin/du -sk |awk -F '{ print $1 }'`;
fi

if [ -f ]
then
fsize_yest=`/usr/sbin/du -sk |awk -F '{ print $1 }'`;
fi

if [ $fsize_td -lt $fsize_yest ]
then
/bin/mail -s "`date` Today's file having problem....."
fi
##########

Hope this hint will help.

Cheers,

Rajesh




Raj D.
Honored Contributor

Re: shell script!

Hi Ajay ,

You can do this task like that ,

suppose your backup directory is /home/backup

#########################################
#!/usr/bin/bash
cd /home/backup
ls -l file.today | awk '{print $5}' > today
ls -l file.yest | awk '{print $5}' > yest
TODAY=`cat today` ; YEST=`cat yest`
if [ $TODAY < $YEST ]
then
echo " Todays file is smaller in size than yesterdays "
echo "Deleting todays file ..."
rm -i file.today
# Sending email..
echo "Sending email to the Administrator.."
echo " Alert! Todays backup file is smaller than yesterdays !" | mailx -s "Alert! Backup file smaller than yesterday" ajay@domain.com

echo "Email sent .."
# Removing temp fiel..
rm today ; rm yest

else
echo " Todays backup file file.today is not smaller than yesterdays file"
fi
############################################


Note: file.today and file.yest has to be replace with your file name in backup directory , that generates with yesterday and today.

Hope this will help you ,

Cheers,
Raj.D.


" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: shell script!

Ajay,

Also you can add [ -f filename ] , will check for the exisitance of the file.

So the script will look like this:
----------------------------------------

#########################################
#!/bin/bash
cd /home/backup
# Checking file existance.
if [ -f file.today ]
then
echo " [OK]: file(todays) exists."
else
echo " todays file doesnot exists "
fi
# #
if [ -f file.yest ]
then
echo " [OK]: file(yesterdays) exists."
else
echo " Yesterdays file doesnot exists "
fi
########File check done..################

#########################################
# Comparing file size .......
#########################################

ls -l file.today | awk '{print $5}' > today
ls -l file.yest | awk '{print $5}' > yest
TODAY=`cat today` ; YEST=`cat yest`
if [ $TODAY < $YEST ]
then
echo " Todays file is smaller in size than yesterdays "
echo "Deleting todays file ..."
rm -i file.today
# Sending email..
echo "Sending email to the Administrator.."
echo " Alert! Todays backup file is smaller than yesterdays !" | mailx -s "Alert! Backup file smaller than yesterday" ajay@domain.com

echo "Email sent .."
# Removing temp fiel..
rm today ; rm yest

else
echo " Todays backup file file.today is not smaller than yesterdays file"
fi
############################################

you can try it and let us know ..

Cheers,
Raj.D
" If u think u can , If u think u cannot , - You are always Right . "
Muthukumar_5
Honored Contributor
Solution

Re: shell script!

You can try as,

#!/bin/bash

TFILE="file_$(date +'%d_%m_%y')"
YFILE="file_$(date -d "yesterday" +'%d_%m_%y')"

TSIZE=$(ls -l $file | cut -d" " -f5)
YSIZE=$(ls -l $file | cut -d" " -f5)
USER=$(ls -l $file | cut -d" " -f2)

if [[ ${TSIZE} -lt ${YSIZE} ]]
then
rm -f ${TFILE}
echo "Problem with today's mail. ${TFILE}" | mail $USER@domain

fi

hth.
Easy to suggest when don't know about the problem!
Raj D.
Honored Contributor

Re: shell script!

Hi Ajay ,

Any update on the script problem. Does the above scripts working. pls update.

Cheers ,
Raj.D
" If u think u can , If u think u cannot , - You are always Right . "
ajay_25
Occasional Contributor

Re: shell script!

hi guys,

thank you very much that you all helped me. i taken small part in every script you provided and customised as per my requirement.

file names what i am using in this script are dynamic(everyday changes during the date in its name).those files created by another backup script.sorry i dint mension it in my question.

so i feel Muttukumar script a bit easy. but (cut -d" " -f5) that is pointing to diff field. (cut -d" " -f13) is getting the size of the file field.

this is my script which i costomized with the help of you all.

#!/bin/bash
TFILE="dbname_$(date +20'%y-%m-%d.%A').sql.gz";
YFILE="dbname_$(date -d "yesterday" +'20%y-%m-%d.%A').sql.gz";
if [ -f $TFILE ] && [ -f $YFILE ]
then
TSIZE=$(ls -l $TFILE | awk '{print $5}');
YSIZE=$(ls -l $YFILE | awk '{print $5}');
if [[ ${TSIZE} -lt ${YSIZE} ]]
then
rm -rf ${TFILE};
echo "Problem with todays backedup file name ${TFILE} with ${TSIZE} byte size. YDay file size is $YSIZE" byte | mail -s "Alert" ajay@domain.com;
fi
else
echo "file/files missing in $HOSTNAME" | mail -s "Red Alert" ajay@domain.com;
fi

Once again thank you very much. and expecting the same in future.

Regards,
ajay