Windows SBS 2008 server out of disk space

I’ve been seeing a lot of Windows 2008 SBS servers running out of disk space lately on the C drive.

This causes Exchange to stop working due to the “back pressure” feature, which has been the first clue to the problem. After a bit of searching I found several area’s where log files can grow.

The 4 main area’s I found are :

  1. Active Directory Certificate Services
  2. IIS Log Files
  3. SBS Data Collector log (DataserviceComponents.log)
  4. Sharepoint SQL logs

I found a great script at SBSfaqs (http://www.sbsfaq.com/?p=1598) and updated the script to include all 4 points above and be able to select which options to run.

You will need to save the 2 SQL files and the batch file to a folder called “SBS Cleaner” on the root of the E drive. Run the batch file as administrator.

Please find the code for each file below.

Other Sources:

http://support.microsoft.com/kb/2000544
http://blogs.technet.com/b/sbs/archive/2010/03/02/recovering-disk-space-on-the-c-drive-in-small-business-server-2008.aspx
http://oxfordsbsguy.com/2012/10/31/sbs-2008-disk-space-and-the-dataservicecomponents-log-file/

Copy the following code and save it as logshrink.sql

 declare @ConfigDB varchar(255);
   declare @ConfigDBLog varchar(255);
   declare @ConfigDBCmd varchar(255);
   select @ConfigDB = name from sys.databases where name like 'SharePoint_Config_%';
   set @ConfigDBCmd = 'BACKUP database [' + RTRIM(@ConfigDB) + '] to disk=''before.bkf''';
   execute(@ConfigDBCmd);
   set @ConfigDBCmd = 'use [' + RTRIM(@COnfigDB) + ']';
   execute(@ConfigDBCmd);
   set @ConfigDBCmd = 'BACKUP LOG [' + RTRIM(@ConfigDB) + '] WITH TRUNCATE_ONLY';
   execute(@ConfigDBCmd);
   set @ConfigDBCmd = 'use [' + RTRIM(@COnfigDB) + ']';
   execute(@ConfigDBCmd);
   select @ConfigDBLog = name from sys.database_files where name like 'SharePoint_Config%_log';
   set @ConfigDBCmd = 'use [' + RTRIM(@ConfigDB) + '] DBCC SHRINKFILE([' + RTRIM(@ConfigDB) + '_log],1)';
   execute(@ConfigDBCmd);
   set @ConfigDBCmd = 'BACKUP database [' + RTRIM(@ConfigDB) + '] to disk=''after.bkf''';
   execute(@ConfigDBCmd);
   go

 Copy the following code and save it as logshrinkshareweb.sql

declare @ConfigDB varchar(255);
   declare @ConfigDBLog varchar(255);
   declare @ConfigDBCmd varchar(255);
   select @ConfigDB = name from sys.databases where name like 'ShareWebDb%';
   set @ConfigDBCmd = 'BACKUP database [' + RTRIM(@ConfigDB) + '] to disk=''beforeWeb.bkf''';
   execute(@ConfigDBCmd);
   set @ConfigDBCmd = 'use [' + RTRIM(@COnfigDB) + ']';
   execute(@ConfigDBCmd);
   set @ConfigDBCmd = 'BACKUP LOG [' + RTRIM(@ConfigDB) + '] WITH TRUNCATE_ONLY';
   execute(@ConfigDBCmd);
   set @ConfigDBCmd = 'use [' + RTRIM(@COnfigDB) + ']';
   execute(@ConfigDBCmd);
   select @ConfigDBLog = name from sys.database_files where name like 'ShareWebDb%_log';
   set @ConfigDBCmd = 'use [' + RTRIM(@ConfigDB) + '] DBCC SHRINKFILE([' + RTRIM(@ConfigDB) + '_log],1)';
   execute(@ConfigDBCmd);
   set @ConfigDBCmd = 'BACKUP database [' + RTRIM(@ConfigDB) + '] to disk=''afterWeb.bkf''';
   execute(@ConfigDBCmd);
   go

 Copy the following code into a SBS Cleaner.bat file

@echo off

rem Script to clean up disk space on SBS 2008 servers
  rem Downloaded from SBSfaq.com rem V1.0 - March 28th, 2010
  rem v1.1 - Feb 23rd, 2012 - fixed typo in certlog path

rem v2.0 updated by Kettlewell.IT 17 Oct 2013
  rem add choices and SBS Data Collection Monitoring
  rem updated IIS files to be over 30 days before deleting (thanks to comment from JVR at SBSfaq.com)
  rem added logging file.

rem PLEASE READ
  rem  It creates a log file SBSCleaner.log in the folder it is run from.
  rem  It must be Run As Administrator
  rem  You need 2 files logshrink.sql and logshrinkshareweb.sql in the same folder for the SQL log shrinking.

:START

ECHO C Drive cleaner for SBS 2008
  ECHO Please Make sure you have a full backup before running this program. No one is responsible for any data loss that may occur.

ECHO C Drive cleaner for SBS 2008 was run at : >> "SBSCleaner.log"
  ECHO %date% %time% >> "SBSCleaner.log"
  ECHO DISK SPACE BEFORE: >> "SBSCleaner.log" wmic logicaldisk where "deviceid='C:'" get freespace >> "SBSCleaner.log"
  ECHO . >> "SBSCleaner.log"

CHOICE /M "Do you want to clean up the Active Directory Certificate Services ?"
  IF ERRORLEVEL 2 GOTO IIS
  IF ERRORLEVEL 1 GOTO CERT

rem Certificate Services Logs

:CERT

net stop "Active Directory Certificate Services" >> "SBSCleaner.log"forfiles /d -30 /p "%windir%\system32\certlog" /m *.log /c "cmd /c del @file" >> "SBSCleaner.log" 
  forfiles /d -30 /p "%windir%\system32\certlog" /m *.chk /c "cmd /c del @file" >> "SBSCleaner.log" 
  forfiles /d -30 /p "%windir%\system32\certlog" /m *.jrs /c "cmd /c del @file" >> "SBSCleaner.log"   
  net start "Active Directory Certificate Services" >> "SBSCleaner.log"

ECHO Active Directory Certificate Services Cleaned ! ECHO Active Directory Certificate Services Cleaned ! >> "SBSCleaner.log"

rem IIS Log Files

:IIS

CHOICE /M "Do you want to clean up the IIS logs ?" 
  IF ERRORLEVEL 2 GOTO SBS 
  IF ERRORLEVEL 1 GOTO IIS2

:IIS2

forfiles /d -30 /p "C:\inetpub\logs\LogFiles" /m *.log /s /c "cmd /c del @file" >> "SBSCleaner.log"

ECHO IIS Logs Cleaned ! 
  ECHO IIS Logs Cleaned ! >> "SBSCleaner.log"

REM SBS Monitoring Logs 

:SBS

CHOICE /M "Do you want to clean up the SBS Data Collector log ?" 
  IF ERRORLEVEL 2 GOTO :SQL 
  IF ERRORLEVEL 1 GOTO :SBS2

:SBS2

Net stop "DataCollectorSvc" >> "SBSCleaner.log" 
  del "C:\program files\Windows Small business Server\logs\monitoringServiceLogs\DataserviceComponents.log" Net start "DataCollectorSvc" >> "SBSCleaner.log"

ECHO SBS Data Collector Log Cleaned! 
  ECHO SBS Data Collector Log Cleaned! >> "SBSCleaner.log"

:SQL

CHOICE /M "Do you want to clean up the Sharepoint SQL logs ?" 
  IF ERRORLEVEL 2 GOTO :END 
  IF ERRORLEVEL 1 GOTO :SQL1

:SQL1

sqlcmd -S \\.\pipe\mssql$microsoft##ssee\sql\query -E -i "E:\SBS cleaner\logshrink.sql"

sqlcmd -S \\.\pipe\mssql$microsoft##ssee\sql\query -E -i "E:\SBS cleaner\logshrinkshareweb.sql"

ECHO SQL Log files Cleaned! ECHO SQL Log files Cleaned! >> "SBSCleaner.log"

:END

ECHO DISK SPACE AFTER: >> "SBSCleaner.log" wmic logicaldisk where "deviceid='C:'" get freespace /format:value >> "SBSCleaner.log"

ECHO . 
  ECHO . 
  ECHO Script Finished 

PAUSE

Do you have an improvement on this or has this helped you out ? Please leave a comment to let me know, thanks.

Leave a comment

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

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

One thought on “Windows SBS 2008 server out of disk space”