Skip to content

Instantly share code, notes, and snippets.

@malias
Created August 6, 2014 19:51
Show Gist options
  • Save malias/3527ab74ee2a85663281 to your computer and use it in GitHub Desktop.
Save malias/3527ab74ee2a85663281 to your computer and use it in GitHub Desktop.
Find Big Files with LSBInitScripts and exceptions
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: findbigfiles
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start: 2 3 4
# Default-Stop: 0 S 6
# Short-Description: Search big Files
# Description: Search all big files in specific directories
### END INIT INFO
#Global variables
log_file='/var/log/findbigfiles.log'
find=`find /var/www /home /tmp /opt -not -path "*/log/*" -not -path "*/typo3temp/*" -not -path "*/sugarcrm/cache/*" -not -path "*/magento/var/cache/*" -not -path "*/crm/cache/*" -size +19M`
limit=1024000
email=''
# Remove old logfile, if you start this script more than once time a day
test -n $log_file && rm $log_file
# Start function to generate a nice logfile
findbigfiles (){
for search_result in $find
do
if [ -f $search_result ]
then
# Generate log
echo "+----------------------------------------------------------------------" >> $log_file
# Get some information for the founded files and put it in a variable
size=`du -h $search_result | awk '{print $1}'`
modi=`stat -c %z $search_result | sed 's/\..*//'`
# If the size bigger or equal $limit, then put a mark in to the log
if [ `du $search_result | awk '{print $1}'` -ge $limit ]
then
echo "| Highly recommended to check this file !" >> $log_file
fi
# Generate log
echo "| "$search_result >> $log_file
echo "| File size: " $size " | Last modification: " $modi >> $log_file
echo "+----------------------------------------------------------------------" >> $log_file
fi
done
# Start mail function
send_mail
}
# Start mail function with multiple recipients
send_mail (){
# If the log file exist
if [ -f $log_file ]
then
# Store variable addresses and execute follow command with each variable
for all_emails in $email
do
# Send mail with hostname and list form the logfile
mail -s "$(hostname) : Big Files to check" $all_emails < $log_file
done
fi
}
# Start function
findbigfiles
# Stop script with error output
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment