Scan your cPanel/Directadmin for excessive files

When dealing with cPanel/Directadmin that contains a lot of users, you need some control of what those users are uploading. Web hosting accounts should be used just for that, hosting websites, and not as data storage – in most cases. Sometimes you even offer packets with unlimited disk capacity but you don’t want that users are storing movies and all kind of other unnecessary files that don’t belong on web hosting account. From web hosting perspective, in most cases, any single file that exceeds 100M is usually not part of website and is just laying there, wasting your precious disk space. In cases like this, account is used as backup service. You’ll be able to find all kind of files. Movies, music, applications, archives …

So you may want to have some reports on regular basis that gives you list of all files that exceed some maximum allowed file size limit – defined by you. Personaly, I scan on interval of 7 days for all files that exceed size of 300M. I created a simple script that will do just that. It will check your /home directory for all files that exceed size that you defined. At the end, report will be sent to your email. You can also scan other directory beside /home if you wish.

Copy script below, create file like /sbin/check_excessive_files.sh and paste its content in it. Of course, you should define variables according to your needs. Don’t forget to give permissions so that script will be able to execute:

chmod +x /sbin/check_excessive_files.sh

Add script to your crontab so that it will run on regular interval. In example below, every 10 days.

crontab -e

Than add this line to your cron:

0 0 */10 * * /sbin/check_excessive_files.sh >/dev/null 2>&1

Here is the script:

#!/bin/bash

# global variables
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin

# define your mail
MAIL=/bin/mail

# set file size limit
limit=300M

# hostname of your server
host=`hostname`

# temp log file
log_file=`mktemp`;
temp_file=`mktemp`;

# add header
echo -e "$host - files larger than $limit" >> $temp_file
echo -e "----------------------------------------------------------------------------------------------------\n" >> $temp_file

# send email to address defined below
mail_address='igor@mydomain.com'

# send email from - hostname will be used
mail_from="monitor@$host"

# define mail subject
mail_subject="Excessive files report - $host"

# directory to scan. You can change if you want. This is default cPanel directory
path=/home/

# search for excessive files
find $path -size +$limit -type f -exec du -h {} \; >> $log_file

if [[ -s "$log_file" ]]; then

       cat $log_file >> $temp_file;
       $MAIL -r $mail_from -s "$mail_subject" $mail_address < $temp_file; else echo "No results" > /dev/null
fi

# remove temp files
rm $log_file
rm $temp_file 

Got Something To Say:

Your email address will not be published. Required fields are marked *

*

I accept the Privacy Policy

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 geegkytuts.net
Hosted by SIEL


About author