Find, backup and delete.

Is there any error while executing this script..

#!/bin/bash

FINDPATH=/home/ftpcdr/cdr/192.168.3.91
BACKPATH=/home/ftpcdr/backup
STATUS=$?

cd $FINDPATH
find -type d -mtime +30  -print > $BACKPATH/list.txt # FIND FILES THAT CREATED BEFORE 30 DAYS.

FIRST=$(ls $BACKPATH/list.txt | grep "[0-9]" | head -n 1)
LAST=$(ls $BACKPATH/list.txt | grep "[0-9]" | tail -n 1)
LISTFILE=/home/ftpcdr/backup/list.txt

if  [ -s "$LISTFILE" ] ;then # IF NO AVAILABLE OUTPUT IN list.txt THEN EXIT THE SCRIPT. OTHERWISE, GET BACKUP FOR THEM.( I ALWAYS GET HERE THE NEXT ECHO MESSAGES). 
echo "No data to backup!" && exit
else
cd $FINDPATH && tar -czf $BACKPATH/$FIRST\_$LAST.tar.gz `cat $BACKPATH/list.txt`
fi

if [[ $STATUS -eq 0 ]];then #IF THE LAST STEP OK, THEN DELETE THE SAME COMPRESSED FILES AS OF THE LAST STEP.
cd $FINDPATH
find -type d -mtime +30 -delete
fi

I don't know if there are any error in my script. Please your advice. Thanks

You're grepping for a number in a filename path that doesn't contain any numbers. (given that the file exists, ls $BACKPATH/list.txt is a less efficient way of saying echo $BACKPATH/list.txt)

Perhaps you are meaning to grep the contents of the file?

i.e. This...

grep "[0-9]" $BACKPATH/list.txt

Instead of this...

ls $BACKPATH/list.txt | grep "[0-9]"

I forgot to mention that $FINDPATH contains a many directories named by dates (e.g. 20130809).

But you're grepping on BACKPATH, not on FINDPATH - and on the name of the file, not its contents. And, BTW, if you ls a fully-qualified filename, the output will be a fully qualified filename.

I'm grepping (because I want to give the TAR file a name like FIRSTDAT_LASTFATE.tar) list.txt wich stored in $BACKPATH for the directories that compressed)for the directories that stored. However, if I'm wrong, how can I fix this?

Hey guys, I have fixed and tested it like this..

#!/bin/bash

# Environment Variables
FINDPATH=/home/ftpcdr/cdr/192.168.3.91
BACKPATH=/home/ftpcdr/backup

# Change into CDRs directory, find the CDRs which created before 30 days and create a list for them in list.txt

cd $FINDPATH && find -type d -mtime +30 | grep -o '[0-9]*' | sort > $BACKPATH/list.txt

# Environment Variables

FIRST=$(cat $BACKPATH/list.txt | grep "[0-9]" |  head -n 1)
LAST=$(cat $BACKPATH/list.txt | grep "[0-9]" | tail -n 1)
LISTFILE=/home/ftpcdr/backup/list.txt

# If we dont have a valid CDRs within this time, then exit the script Else, backup (and compress)
# these files into our backup directory and name it as of the period of CDR creation.

if  [ ! -s "$LISTFILE" ]
then
echo "No data to backup!" && exit
else
cd $FINDPATH && tar -czf $BACKPATH/$FIRST\_$LAST.tar.gz `cat $BACKPATH/list.txt`
fi

#If this operation completed successfully, then delete the original CDRs. Script job has done!

if [ "$?" = "0" ]; then
cd $FINDPATH && find -type d -mtime +30 -delete && echo "CDRs have been successfully archived from $FIRST to $LAST on $BACKPATH"
else
echo "CDRs BACKUP ERROR!"
fi

#End of script, EOT