Wednesday, January 23, 2013

House Keeping Script

In this post , we will create a shell script to do the housekeeping activities in your application servers,database servers,web servers,cache servers,etc. …!!


Housekeeping-image
You can use this script for almost all housekeeping activities like,
1]  Server logs
2]  Application Logs
3]  Archived Logs
4]  Archived Deployments

and much more ….
Pre-requisites:                                                                                                                         
/optional/Oracle/Middleware/domains/base_domain/servers/Ms1/logs/     | log*
/optional/Oracle/Middleware/domains/base_domain/servers/Ms2/logs/     | log*
/optional/Oracle/Middleware/domains/base_domain/servers/Ms3/logs/     | log*
/optional/Oracle/Middleware/deployments | gz  
We need to mention the list of locations(directory path) and extensions(.out,.log) which requires frequent cleanup at regular interval in a property file.
To do the housekeeping activity we need the following property file.

Save this in a property file and save name it as cleanup_folders.properties.
Core Part of Script:                                                                                                                  
#Program Name:HouseKeeping_Activity.sh                                                         
#A script that performs HouseKeeping activities

CLEAN_UP_PROPERTY_FILE="cleanup_folders.properties"
mount_path="/optional"
number_of_days_old=$1
bold=`tput bold`

PreClean_Event()
{
rm cleanup_task.csv
}

Get_FileSystemSize_BeforeCleanup()
{
fs=`df -h|grep $mount_path`
echo -e "\nFile System Size Before Cleanup:-" >> before.txt
echo $fs >> before.txt
}

Get_FileSystemSize_AfterCleanup()
{
fs=`df -h|grep $mount_path `
echo -e "\n File System Size After Cleanup:-" >> after.txt
echo $fs >> after.txt
}

Read_Property_File()
{
while read line
  do
           path_location=`echo $line | awk '{print $1}'`
           file_format=`echo $line | awk '{print $3}'`
           Find_Old_Files
  done < $CLEAN_UP_PROPERTY_FILE
}

Find_Old_Files()
{
echo -e "\033[32mCleaning up the older files in Progress!!"
echo -e "\033[33mPath Location:$path_location"
echo -e "\033[34mFile Format:$file_format"
find $path_location\/*.$file_format -mtime +$number_of_days_old >> files.txt
total_no_of_files=`find $path_location/*.$file_format -mtime +$number_of_days_old | wc -l`
echo -e "$path_location \t $total_no_of_files" >> files_count.txt
find $path_location\/*.$file_format -mtime +$number_of_days_old| xargs rm
}

HouseKeeping_Report()
{
echo -e "\n CleanUp Task Document!!" >> report.txt
cat before.txt >> report.txt
echo -e "\n" >> report.txt
cat after.txt >> report.txt
echo -e "\n" >> report.txt
echo -e "\n Path Location" "\t 
| Total No of files deleted under each category" >> report.txt
cat files_count.txt >> report.txt
echo -e "\n" >> report.txt
echo -e "\n List of Files that Deleted:" >> report.txt
cat files.txt >> report.txt
}

PostClean_Event()
{
echo "Cleaning unnecessary Files!!"
rm files.txt
rm before.txt after.txt
rm files_count.txt
}

Help_Function()
{
echo "Enter the number of days as input!!"
echo "Usage:./clean_up_activity.sh 30"
}

#Main Function Begins Here
if [ $# -eq 1 ]
then
    PreClean_Event
    Get_FileSystemSize_BeforeCleanup
    Read_Property_File
    Get_FileSystemSize_AfterCleanup
    HouseKeeping_Report
    PostClean_Event
else
    Help_Function
fi

                                                                                                                                                        
The above script requires one input from user as a command line argument, that is the number of days old logs to keep.For example, if you want to keep only logs and deployed archives for the last one month , then pass 30 as an argument like following.
To keep one month old logs and archives,
./clean_up_activity.sh 30
To keep 45 days old logs and archives,
./clean_up_activity.sh 45
Additionally this script also prepares a short report about ,
1] File System size before cleanup
2] File System size after cleanup
3] Number of deleted files under each directory
4] And finally the list of deleted files
I hope that this script made some of the administrators life easier.If you have better housekeeping script please share it with us, we are eager to know Smile

No comments:

Post a Comment