Results 1 to 24 of 24
  1. #1
    Join Date
    May 2009
    Location
    Kandy, Sri Lanka
    Posts
    205

    A disk cleanup script for cPanel VPS and Dedicated Servers

    Hey everyone,

    2 days back i was starting to get the inode soft limit warning in vituozzo. So last night i did some searching and found a cool script that cleans up the disk space, and removes all the leftovers of cpanel updates/upgrades/php rebuilds etc. This helped me in removing approx. 50,000 inodes and released about 1GB of disk space. Since this was very helpful to me i though of sharing it with you all.

    @MODS: Please put this thread in an appropriate section, cos i couldn't find one

    BTW, the people using other control panels, please be kind enough to modify this script accordingly and repost it so that others may find it more useful.
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2006
    Location
    New York, NY
    Posts
    706
    Hey man, this is a pretty good idea. I'm going to modify this a bit by making the code a tad cleaner by using a 'for' loop, instead of repeating the same commmand. I'm also going to include functions for error checking the process.

    I'll post up the revised code in a bit.

  3. #3
    Would really be great to have something automated and most importantly SECURE that doesn't erase any important files...
    A great time saver instead of the usual "please clean my server from useless files because I am running out of space" ticket!

  4. #4
    Join Date
    Feb 2006
    Location
    New York, NY
    Posts
    706
    Here's the new code.

    Code:
    #!/bin/bash
    ###################
    # Disk Cleanup Script
    # Originally by gunemalli / Revised by Joseph Fasone (r00ter)
    ###################
    
    ## Defining locations
    home=/root/
    log=/root/diskcleanup
    
    ## Defining functions
    
    function check {
    
    if [ $? = "0" ]
    then
    
    echo -n "..." ;
    
    sleep 1;
    continue;
    
    else
    
    echo -n "...FAILED!"
    sleep 5;
    exit;
    
    fi
    
    }
    
    function fcheck {
    
    if [ $? = "0" ]
    then
    
    echo -n "...Done!" ;
    
    else
    
    echo -n "...FAILED!"
    sleep 5;
    exit;
    
    fi
    
    }
    
    
    ## Show the before Disk Usage.
    
    df -h
    sleep 3
    
    
    
    
    ## Yum works best when cleaned often I have noticed :)
    
    echo -n "Cleaning YUM......."
    yum clean all &>$log
    fcheck
    echo
    
    #
    ## Remove all Compressed logs / remove any variations / incrementals
    #
    
    echo -n "Now removing files...."
    sleep 1
    
    for delete in \
    /var/log/*.gz \
    '/var/log/*.?' \
    '/home/core.*' \
    '/home/cpeasyapache' \
    '/home/MySQL-install' \
    '/home/latest' \
    '/home/cprubygemsbuild' \
    '/home/cprubybuild' \
    '/home/cprestore' \
    '/usr/local/cpanel/src/3rdparty/*' \
    
    do
    
    rm -rf $delete &>$log
    check
    continue
    
    done
    fcheck
    echo
    
    ## Final commands..
    cd /tmp &>log
    
    for files in `ls`; do rm -f $files &>log; done;
    
    echo -n "Now restarting MySQL..."
    /etc/init.d/mysql restart &> log
    fcheck
    
    echo
    echo
    echo "Disk Cleanup Complete!"
    sleep 2
    
    exit 0;

  5. #5
    Join Date
    May 2009
    Location
    Kandy, Sri Lanka
    Posts
    205
    A tad longer than the original one but much more efficient than it and better. I cleaned up a little bit more of the white spaces and added a bit more code.

    You DirectAdmin guys, please help us to improve this script.

    Code:
    #!/bin/bash
    ###################
    # Disk Cleanup Script
    # Originally by Gayan Gunasekara (gunemalli) / Revised by Joseph Fasone (r00ter)
    # Adapted from http://neverthelost.net/site/2009/04/quick-and-easy-clean-up-for-cpanel-servers/
    ###################
    
    ## Defining locations
    home=/root/
    log=/root/diskcleanup
    
    ## Defining functions
    
    function check {
    
    if [ $? = "0" ]
    then
    
    echo -n "..." ;
    
    sleep 1;
    continue;
    
    else
    
    echo -n "...FAILED!"
    sleep 5;
    exit;
    
    fi
    
    }
    
    function fcheck {
    
    if [ $? = "0" ]
    then
    
    echo -n "...Done!" ;
    
    else
    
    echo -n "...FAILED!"
    sleep 5;
    exit;
    
    fi
    
    }
    
    ## Show the before Disk Usage.
    
    df -h
    sleep 3
    
    ## Yum works best when cleaned often I have noticed :)
    
    echo -n "Cleaning YUM......."
    yum clean all &>$log
    fcheck
    echo
    
    #
    ## Remove all Compressed logs / remove any variations / incrementals
    #
    
    echo -n "Now removing files...."
    sleep 1
    
    for delete in \
    '/var/log/*.gz' \
    '/var/log/*.?' \
    '/home/core.*' \
    '/home/cpeasyapache' \
    '/home/MySQL-install' \
    '/home/latest' \
    '/home/cprubygemsbuild' \
    '/home/cprubybuild' \
    '/home/cprestore' \
    '/usr/local/cpanel/src/3rdparty/*' \
    
    do
    
    rm -rf $delete &>$log
    check
    continue
    
    done
    fcheck
    echo
    
    ## Final commands..
    cd /tmp &>log
    
    for files in `ls`; do rm -f $files &>log; done;
    
    echo -n "Now restarting MySQL..."
    /etc/init.d/mysql restart &> log
    fcheck
    
    echo
    echo
    echo "New disk space"
    df -h
    echo
    echo
    echo "Disk Cleanup Complete!"
    sleep 2
    
    exit 0;

  6. #6
    Join Date
    Feb 2006
    Location
    New York, NY
    Posts
    706
    Thanks for the support, gunemalli. I made it so all errors get exported to a file called 'diskcleanup' in the /root or ~/ directory. It also makes sure every command exits cleanly before proceeding onto the next one. I also added in a simple progress bar.

  7. #7
    Join Date
    May 2009
    Location
    Kandy, Sri Lanka
    Posts
    205
    I checked the script. Its really nice, and very informative. I'm not a shell scripting guru nor a linux one. But it's nice to learn things new.

    I just posted this in the DA forums, so some one could look into this add things which are unique to DA. then just a matter of combining the two scripts and releasing.

  8. #8
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,849
    Hmm, a bash script that runs as root and does "rm -rf $variable" repeatedly. What could possibly go wrong...

    Just in case anyone doubts the danger here, how about a tiny mistake in setting up one of those variables, eg.
    Code:
    '/ home/cprubybuild' \
    or
    Code:
    '/home/username/public_html/temp home' \
    At the very least you need:
    1. A warning in the comments around that section
    2. A check for sane values in the code before "rm -rf"
    (and even then, no matter how idiot-proof you try to make it, you can always find a better idiot. )

    Here's a possible test - not guaranteed in any way but it might give you a shade more protection:
    Code:
    containspace=`expr match "$delete" '.*[[:space:]]'`
    if [ "$delete" == "/" -o "$containspace" != "0" ]; then
            echo "DANGER! REQUESTED rm / OR PATH CONTAINING SPACES - NOT DELETING"
    else
            rm -rf $delete &>$log
    fi
    Edit: It's a good idea also to use "set -eu", causing the script to bomb-out on errors or undefined variables
    Last edited by foobic; 07-23-2009 at 12:32 AM.
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  9. #9
    Join Date
    May 2009
    Location
    Kandy, Sri Lanka
    Posts
    205
    Nice catch

    Thanks for the heads up, i'll try to look into modifying this, but as i said earlier, my knowledge in shell scripting is very low.

    Thanks.

  10. #10
    Join Date
    Feb 2006
    Location
    New York, NY
    Posts
    706
    Chris,

    I'll input your work in a bit. However, regarding your last comment - I use two checking functions causing the script to exit on error. Every command is run through an exit status checking command. If the exit status is not 0, the script will not complete. For example, if you tried to rm -rf / - CentOS 5 will give you the run of the mill "Are you crazy?" message, and exit uncleanly, causing the script to fail.

  11. #11
    Join Date
    Feb 2004
    Location
    Sacramento CA
    Posts
    3,513
    This is one of those instances of members of this community coming together to work on something that can help everyone. Kudos to you guys.

    @Chris How's it going? = )

  12. #12
    Join Date
    May 2009
    Location
    Kandy, Sri Lanka
    Posts
    205
    Thanks for the compliment Nnyan. I thought of sharing my find with everyone cos i know many of you out there would benefit from little things like these.

    And i'm really greatful for Chris taking this further along to improve it. much credits of this script goes to Loren @ http://neverthelost.net/ he is the original author of the script. I only did 2 lines of code to rectify mysql.sock issue. and to Chris for modifying to a better state.

  13. #13
    Keep up the great work guys

  14. #14
    nice idea,
    can we take the final version of it in the case it is complete and tested.

    Best Regards
    Dimi

  15. #15
    Join Date
    May 2009
    Location
    Kandy, Sri Lanka
    Posts
    205
    Well, i think Chris is busy. I'm no ssh scripting guru. But i'm using the script i posted at post #5. It's working flawlessly. and erases a lot of junk.

    The final one will come out once Chris finishes of with it.

  16. #16
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    541
    The version of the script that I have posted on my site is rather old to be honest with you. I have been testing some newer variables to remove very old versions of items such old Apache configurations and such but rathar than just remove them I want to archive them for later use.

    I appreciate giving credit to where credit is due though .

  17. #17
    Join Date
    May 2009
    Location
    Kandy, Sri Lanka
    Posts
    205
    Hello there,

    It's so nice to see you here. Thanks for the info. Even tho it's old i must say, it's a very effective script. It cleans like hell and doesn't cause any problems. If you can please do modify the new versions on what you found out.

    I'll be out of luck tho, cos in a few weeks time I'm moving to DirectAdmin. I'll try to modify this script to their specs too, once i get more experience on DA. Then it would be beneficial to many users out there.

    Thanks for posting here, and i really like your blog. A very informative one.

  18. #18
    Join Date
    Feb 2004
    Location
    Sacramento CA
    Posts
    3,513
    looking foward to the finalized version but may I ask what are the differences (if any) between the #4 and #5 versions?

  19. #19
    Hi,

    Can I use this script to cleanup lxadmin (kloxo)?

    Thanks.

  20. #20
    Join Date
    May 2009
    Location
    Kandy, Sri Lanka
    Posts
    205
    the main difference is there are some additional information displaying, and cleaned up code from #4. Also proper credits have been added.

    For now this is cpanel only. i'll try to dev a version for DA and kloxo later.

  21. #21
    so you free'd up 1gb of space by basically cleaning yum, /var/log, /tmp and some cpanel files? wow that cpanel must be a right mess - or you don't have logrotate setup properly, my entire vps is only about 2gb, even on my desktop machine, /var/log and /tmp make up about 4mb!

    i'd have thought cleaning /var/tmp would be better than /tmp, which is generally in-memory not filesystem-based.

    and yes, i agree, rm -rf is very risky, especially from a script and as root.

  22. #22
    Quote Originally Posted by gunemalli View Post
    the main difference is there are some additional information displaying, and cleaned up code from #4. Also proper credits have been added.

    For now this is cpanel only. i'll try to dev a version for DA and kloxo later.
    Thanks. You're great. Really appreciate the work and effort put in by everyone.

    Looking forward to your kloxo version.

  23. #23
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    541
    Quote Originally Posted by sej7278 View Post
    so you free'd up 1gb of space by basically cleaning yum, /var/log, /tmp and some cpanel files? wow that cpanel must be a right mess - or you don't have logrotate setup properly, my entire vps is only about 2gb, even on my desktop machine, /var/log and /tmp make up about 4mb!

    i'd have thought cleaning /var/tmp would be better than /tmp, which is generally in-memory not filesystem-based.

    and yes, i agree, rm -rf is very risky, especially from a script and as root.
    Generally when you are using repetitive tasks and the same commands over and over again you tend to know what is acceptable to use at what time.

  24. #24
    Join Date
    Feb 2005
    Location
    Norway
    Posts
    1,651
    Quote Originally Posted by r00ter View Post
    Chris,

    I'll input your work in a bit. However, regarding your last comment - I use two checking functions causing the script to exit on error. Every command is run through an exit status checking command. If the exit status is not 0, the script will not complete. For example, if you tried to rm -rf / - CentOS 5 will give you the run of the mill "Are you crazy?" message, and exit uncleanly, causing the script to fail.
    Hello, I just tried to type in rm -rf / on my CentOs 5.2 test pc from root, and it deleted all my files, and I can not reboot or start the server anymore. I did not get any "Are you crazy" message, only some directories that was busy.

    So using rm -rf / on a CentOs computer without any warning is not very good.
    My Top 20 benchmark list (and review site)
    Powered by: Kimsufi, backed up by: Hetzner, DigitalOcean and Vultr.com
    Also using
    SolaDrive.com (56+ months), KnownHost.com (56+ months)

Similar Threads

  1. JUHOST.COM - Dedicated Servers, Fast Servers, Free 13GB Template/Script Bonus
    By punjabipredator in forum Dedicated Hosting Offers
    Replies: 4
    Last Post: 01-13-2007, 05:22 AM
  2. /tmp Cleanup Script
    By bheka in forum Hosting Security and Technology
    Replies: 6
    Last Post: 11-06-2006, 04:47 PM
  3. Who offers more disk space on dedicated servers?
    By jondolar in forum Dedicated Server
    Replies: 14
    Last Post: 09-13-2006, 07:44 PM
  4. disk cleanup is being an @ss
    By daylate in forum Web Hosting Lounge
    Replies: 0
    Last Post: 03-19-2004, 03:37 AM
  5. Problem with disk quota used after transfer between cpanel servers
    By nogi in forum Hosting Security and Technology
    Replies: 2
    Last Post: 05-25-2003, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •