Results 1 to 24 of 24
-
07-22-2009, 12:11 PM #1Junior Guru
- 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.
-
07-22-2009, 02:05 PM #2Web Hosting Master
- 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.
-
07-22-2009, 02:18 PM #3WHT Addict
- Join Date
- Nov 2007
- Posts
- 116
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!
-
07-22-2009, 02:21 PM #4Web Hosting Master
- 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;
-
07-22-2009, 10:55 PM #5Junior Guru
- 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;
-
07-22-2009, 11:31 PM #6Web Hosting Master
- 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.
-
07-22-2009, 11:36 PM #7Junior Guru
- 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.
-
07-23-2009, 12:24 AM #8Retired Moderator
- 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' \
Code:'/home/username/public_html/temp home' \
- A warning in the comments around that section
- A check for sane values in the code before "rm -rf"
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
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
-
07-23-2009, 12:33 AM #9Junior Guru
- 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.
-
07-23-2009, 10:23 AM #10Web Hosting Master
- 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.
-
07-23-2009, 12:47 PM #11Web Hosting Master
- 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? = )
-
07-23-2009, 01:03 PM #12Junior Guru
- 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.
-
07-23-2009, 03:04 PM #13WHT Addict
- Join Date
- Nov 2007
- Posts
- 116
Keep up the great work guys
-
08-09-2009, 04:31 AM #14Junior Guru Wannabe
- Join Date
- Aug 2004
- Posts
- 69
nice idea,
can we take the final version of it in the case it is complete and tested.
Best Regards
Dimi
-
08-09-2009, 07:49 AM #15Junior Guru
- 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.
-
08-21-2009, 06:40 AM #16Web Hosting Evangelist
- 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 .
-
08-21-2009, 11:13 AM #17Junior Guru
- 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.
-
08-21-2009, 02:34 PM #18Web Hosting Master
- 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?
-
08-21-2009, 06:44 PM #19Newbie
- Join Date
- Jan 2008
- Posts
- 10
Hi,
Can I use this script to cleanup lxadmin (kloxo)?
Thanks.
-
08-21-2009, 06:58 PM #20Junior Guru
- 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.
-
08-21-2009, 07:25 PM #21WHT Addict
- Join Date
- Dec 2002
- Posts
- 160
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.
-
08-21-2009, 07:55 PM #22Newbie
- Join Date
- Jan 2008
- Posts
- 10
-
08-22-2009, 06:31 PM #23Web Hosting Evangelist
- Join Date
- Feb 2003
- Location
- Seattle, WA
- Posts
- 541
-
08-23-2009, 05:52 AM #24Norwegian polar bear hunter
- Join Date
- Feb 2005
- Location
- Norway
- Posts
- 1,651
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
-
JUHOST.COM - Dedicated Servers, Fast Servers, Free 13GB Template/Script Bonus
By punjabipredator in forum Dedicated Hosting OffersReplies: 4Last Post: 01-13-2007, 05:22 AM -
/tmp Cleanup Script
By bheka in forum Hosting Security and TechnologyReplies: 6Last Post: 11-06-2006, 04:47 PM -
Who offers more disk space on dedicated servers?
By jondolar in forum Dedicated ServerReplies: 14Last Post: 09-13-2006, 07:44 PM -
disk cleanup is being an @ss
By daylate in forum Web Hosting LoungeReplies: 0Last Post: 03-19-2004, 03:37 AM -
Problem with disk quota used after transfer between cpanel servers
By nogi in forum Hosting Security and TechnologyReplies: 2Last Post: 05-25-2003, 06:51 PM