cPanel Quota Management during package changes

If you have a program or script that you'd like to give away or sell, this is the place to do it. All offers should include contact info.

Moderators: BBear, theunknownhost, flaguy

Post Reply
User avatar
Arf
Official Test Penquin
Posts: 9103
Joined: Tue Apr 09, 2002 12:00 am
Location: IDAHO, USA
Contact:

cPanel Quota Management during package changes

Post by Arf » Tue Jun 08, 2010 9:37 am

I've been hunkered down writing code again. It's been a while since I've been able to contribute because I've been learning how cPanel works or in this case, doesn't work and needs a fix. So, here's one of my first contributions to the cause. It's good to be coding again.

Problem: When you edit a hosting package in cPanel, WHM resets all quotas to their default even if the clients using that package have purchased disk space ala-cart.

Solution: I've developed two programs QuotaRecord and QuotaReset that automate the process of resetting all the quotas for each client.

QuotaRecord creates a database of all the quotas for all the clients on the server. You run this program prior to making package changes.

QuotaReset resets all the quotas back to their custom levels after you've made changes in a specific package.

Caveat: Um, not sure if this needs to be said, but if the change you're making in the WHM alters the disk space quota for the package, you wouldn't want to use QuotaReset because it would reset the quota back to the level prior to you making the desired change.

Disclaimer: There are not warranties or guarantees that the code below will do or fail to do anything. No support is provided. No assurance that they won't do damage or bring down the fire of wrath. Use at your own risk. And of course, bla bla bla.
Last edited by Arf on Tue Jun 08, 2010 9:41 am, edited 1 time in total.

User avatar
Arf
Official Test Penquin
Posts: 9103
Joined: Tue Apr 09, 2002 12:00 am
Location: IDAHO, USA
Contact:

Post by Arf » Tue Jun 08, 2010 9:38 am

QuotaRecord

Code: Select all

#!/bin/bash 
# quotarecord by Thomas Leo 6/2010. Version 1.0
# stores a record of the user name and his quota in megabytes info /root/tmp/quotarecord.txt
# This program is designed to be used prior to editing a hosting package in cPanel where
# all the user quotas will be set back to their orignal default and any extra disk space
# on those accounts is removed. This program makes a  record of the current quotas prior
# to the resetting of the hosting package so that another program, quotarepair, can be run
# to reset all account quotas back to their individual custom quotas. 

# format of output file: username MEGS BYTES
# example: smith 100 102400

# DISKPARTITION - the specific disk partition to check. Hint: use the "df" command, find
# the /home directory on right columnm, partition is on left. Example: "/dev/sda8"
DISKPARTITION="/dev/sda8" 


# Just chmod 700 and run. 
# Data collected it put into a file /root/tmp/quotarecord.txt
##########################################################################################

# Make /root/tmp if doesn't exist.
if [ ! -d /root/tmp ]; then mkdir /root/tmp; fi

# clear /root/tmp/quotarecord.txt
>  /root/tmp/quotarecord.txt

# filter out all the extra stuff from the repquota command.
repquota  $DISKPARTITION | awk '{print $1"\t "$4}' | grep -v "^#" | grep "0$" | grep -v " 0$" > /root/tmp/rawquotalist

# digest the info.
while read LINE; do 
        USERNAME=`echo $LINE | awk '{print $1}'`
        BYTES=`echo $LINE | awk '{print $2}'`
  MEGS=`echo $((BYTES/1024))`

echo "$USERNAME $MEGS $BYTES" | tee -a /root/tmp/quotarecord.txt


done < /root/tmp/rawquotalist

# rm /root/tmp/quotarecord.txt DO NOT REMOVE as this is used by quoterepair program. 

rm /root/tmp/rawquotalist

echo "Thank you for using $0 output is at /root/tmp/quotarecord.txt -- Thomas"

User avatar
Arf
Official Test Penquin
Posts: 9103
Joined: Tue Apr 09, 2002 12:00 am
Location: IDAHO, USA
Contact:

Post by Arf » Tue Jun 08, 2010 9:38 am

QuotaReset
I didn't mention it because I'd rather people realize after the first run that unless you put -L in the command line the program will just do a test run and made no changes. You would discover that upon first run.

Code: Select all

#!/bin/bash
# quotareset by Thomas Leo 6/2010. Version 1.0
# Restores quotas for all account according to information in /root/tmp/quotarecord.txt
# as created by companion program quotarecord. 

# This program is designed to be used after editing a hosting package in cPanel where
# all the user quotas will be set back to their orignal default and any extra disk space
# on those accounts is removed. This program resets all the current quotas to what they 
# were prior to the resetting of the hosting package

# DISKPARTITION - the specific disk partition to check. Hint: use the "df" command, find
# the /home directory on right columnm, partition is on left. Example: "/dev/sda8"
# NOTE: THIS SHOULD MATCH $DISKPARTITION in quotarecord
DISKPARTITION="/dev/sda8"

# Just chmod 700 and run. No variables need to be set. This program requires data collected 
# by it's companion program quotarecord to be put into a file
# /root/tmp/quotarecord.txt
##########################################################################################

# idiot check
if [ ! -f /root/tmp/quotarecord.txt ]; then 
        echo "Required file does not exist: /root/tmp/quotarecord.txt. That file is created by quotarecord."
        echo "I'm sorry, I cannot reset quotas without that file... exiting"
        exit
fi

while read LINE; do
          USERNAME=`echo $LINE | awk '{print $1}'`
        OLDMEGS=`echo $LINE | awk '{print $2}'`
          OLDBYTES=`echo $LINE | awk '{print $3}'`

                CURRENTBYTES=`quota $USERNAME | grep $DISKPARTITION | awk '{print $3}'`
                CURRENTMEGS=`echo $(($CURRENTBYTES/1024))`

        # Don't bother changing if old and current quota is the same.
        if [ $CURRENTMEGS != $OLDMEGS ]; then
                        echo -e "USER NAME: $USERNAME\n Old Quota: $OLDMEGS  -  Current Quota: $CURRENTMEGS "
                        echo "Resetting quota for $USERNAME to $OLDMEGS"
                if [ "$1" = "-L" ]; then
                        M="M" #Required by the use of the following variablesa
                        /scripts/editquota $USERNAME $OLDMEGS$M
                else
                        echo " ::: TEST MODE ON: To turn test mode off you must use -L in the command line. No changes"
                fi

        fi

done < /root/tmp/quotarecord.txt

echo "Thank you for using $0  -- Thomas"

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest