Monday, June 30, 2014

Script Clean Up Data

Script Clean Up Data

#!/bin/bash

# Variables
DATE=`date +%Y-%m-%d`
YEAR=`date +%Y`
THISMONTH=`date +%m`
LASTMONTH=`date +'%m' -d 'last month'`
DAYTH=`date +%d`
ERROR=/opt/scripts/error.log
ERROR1=/opt/scripts/error1.log
LOG=/opt/scripts/log.log
error=0

# Clean up Netflow Data last month older then 7 days
DATA_LASTMONTH=/data/nfsen/profiles-data/live/r4/$YEAR/$LASTMONTH/
find $NETFLOW_DATA_LASTMONTH -type d -mtime +7 -exec rm -rf {} 2>> $ERROR \;

# Clean up Netflow Data this month older then 7 days
DATA_THISMONTH=/data/nfsen/profiles-data/live/r4/$YEAR/$THISMONTH/
find $NETFLOW_DATA_THISMONTH -type d -mtime +7 -exec rm -rf {} 2>> $ERROR \;

# Clean up other file-*
find /data/all -name "file.*" -mtime +7 -exec rm -f {} 2>> $ERROR \;
find /data/dns -name "file.*" -mtime +7 -exec rm -f {} 2>> $ERROR \;
find /hoststats/data/ssh -name "file.*" -mtime +7 -exec rm -f {} 2>> $ERROR \;
find /data/telnet -name "file.*" -mtime +7 -exec rm -f {} 2>> $ERROR \;

#Check Error
/bin/cat $ERROR >> $ERROR1
        if test -s $ERROR;
  then
                error=1
        fi
sleep 2
if [ "$error" = 1 ];
then
        # Create email
        echo "From: Netflow " >> /var/tmp/mail.txt
        echo "Subject: Clean Up Netflow Files" >> /var/tmp/mail.txt
        echo " " >> /var/tmp/mail.txt
        echo "CHECK ERROR IN 12.13.14.15 in /opt/scripts/log.log"  >> /var/tmp/mail.txt
        echo "###################################################" >> /var/tmp/mail.txt
        echo "Kind of error: "  >> /var/tmp/mail.txt
        echo "---------------------------------------------------" >> /var/tmp/mail.txt
        `/bin/cat $ERROR1 >> /var/tmp/mail.txt`
        echo "###################################################" >> /var/tmp/mail.txt


        echo "CHECK THE SERVER IP 12.13.14.15" >> /var/tmp/mail.txt

        mail -s "ERROR Cleanup Netflow Data"  ken.vannakk@gmail.com < /var/tmp/mail.txt
        echo "ERROR!!!!" >> $LOG
        /bin/cat $ERROR >> $LOG
        #Delete Log
        rm /var/tmp/mail.txt
        rm $ERROR
        rm $ERROR1
fi
echo "FINISH Cleanup Netflow Data:" $DATE >> $LOG

No comments:

Post a Comment