Cleanup

PowerShell Cleanup Script

Cleanup logfiles when file change date is older than 30 days…

<#
    Description    : Cleanup logfiles when file change date is older than 30 days...
    Keywords       : Files, Change, Cleanup
    Majorversion   : 1
    Author         : Tim van Kooten Niekerk
    Date           : 2011-11-10
#>

<#      CHANGELOG:
        2013-12-12 => Add Headers (Tim van Kooten Niekerk)
#>

# Global config settings...
[string]$sDirName = "D:\LogFiles\W3SVC705438624"
[string]$sFileNameMask = "*.log"
[string]$sGCIMask = $sDirName + "\" + $sFileNameMask
[string]$sEventLogMessage = "### Logfile cleanup script started ###`r`n"

# Start Main script...
if (Test-Path $sDirName) {
get-childitem $sGCIMask | 
	% { 
		if ($_.LastWriteTime -lt (Get-date).AddDays(-30)) {
		remove-item $_.fullname
		$sEventLogMessage = $sEventLogMessage + "File " + $_.fullname + " (" + $dFileModTime.DateTime + ") removed...`r`n"
		}
	}
}

# Cleanup...
[string]$sEventLogMessage = $sEventLogMessage + "### Logfile cleanup script finished ###"
write-eventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventID 30001 -Message $sEventLogMessage -EntryType Information

PowerShell Remove Old Files From a Directory

Script for removal of old backup or logfiles from a specific directory. The example below removes all files from a directory that are older than 90 days. A log of all files that are removed is written to the windows eventlog.

# Global config settings...
[string]$sDirName = "D:\LogFiles"
[string]$sFileNameMask = "*.log"
[string]$sGCIMask = $sDirName + "\" + $sFileNameMask
[string]$sEventLogMessage = "### Cleanup Script Started... ###`r`n"

# Functions and Procedures...
function fGetFileModTime([string]$sFileName) {
  $vFileInfo = get-childitem $sFileName
  return $vFileInfo.lastwritetime 
}

# Start Main script...
if (Test-Path $sDirName) {
get-childitem $sGCIMask | 
    % { 
        # Get File Last Write Time...
        [datetime]$dFileModTime = fGetFileModTime($_.fullname)
        if ($dFileModTime -lt (Get-date).AddDays(-90)) {
        remove-item $_.fullname
        $sEventLogMessage = $sEventLogMessage + "File " + $_.fullname + " (" + $dFileModTime.DateTime + ") removed...`r`n"
        }
    }
}

# Finish and Cleanup...
[string]$sEventLogMessage = $sEventLogMessage + "### Cleanup Script Finished... ###"
write-eventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventID 30001 -Message $sEventLogMessage -EntryType Information

MySQL Table Cleanup Procedure

With the following procedure, it is possible to move data from a primary table to a secondary table for archival purposes. This specific example moves data on the basis of a date column in the primary table. The number of days (related to  the current date/time) to remain in the primary table is passed to the procedure. Start procedure with ‘CALL pCleanUPTable (35)’.

DELIMITER //

CREATE PROCEDURE pCleanUpTable(IN iDays INT)

  BEGIN
    START TRANSACTION;
    SELECT (now() - interval iDays day) INTO @dtDeleteFrom;
    SAVEPOINT PreCleanUP;
    -- Copy old records to new archive/secundary  table...
    INSERT INTO sectab (primtab_id, value1, value2, value3, datum)
    SELECT id, value1, value2, value3, datum from primtab where datum < @dtDeleteFrom;
    SELECT ROW_COUNT() INTO @iRowsCopy;

    -- Remove records from primary table...
    DELETE FROM primtab WHERE datum < @dtDeleteFrom;
    SELECT ROW_COUNT() INTO @iRowsDelete;

    -- Check if rows copied -eq rows deleted...
    IF (@iRowsCopy = @iRowsDelete) THEN
      COMMIT;
      SELECT 'Success - Committed!' AS Result, @iRowsCopy AS RowsCopy, @iRowsDelete AS RowsDelete;
    ELSE
      ROLLBACK TO PreCleanUp;
      SELECT 'Failure - ROLLBACK!' AS Result, @iRowsCopy AS RowsCopy, @iRowsDelete AS RowsDelete;
    END IF;
  END //

DELIMITER ;

In bovenstaande voorbeeld is gebruik gemaakt van de volgende InnoDB tabellen.

[primtab]
+--------+------------------+------+-----+---------+----------------+
| Field  | Type             | Null | Key | Default | Extra          |
+--------+------------------+------+-----+---------+----------------+
| id     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| value1 | varchar(75)      | YES  |     | NULL    |                |
| value2 | varchar(75)      | YES  |     | NULL    |                |
| value3 | varchar(75)      | YES  |     | NULL    |                |
| datum  | datetime         | YES  |     | NULL    |                |
+--------+------------------+------+-----+---------+----------------+

[sectab]
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| primtab_id | int(10)          | YES  |     | NULL    |                |
| value1     | varchar(75)      | YES  |     | NULL    |                |
| value2     | varchar(75)      | YES  |     | NULL    |                |
| value3     | varchar(75)      | YES  |     | NULL    |                |
| datum      | datetime         | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

VBS Cleanup Sessionfiles

Article is written in the Dutch language…

Het onderstaande script heb ik geschreven om bestanden in een bepaalde directory verwijderen als ze ouder zijn dan een opgegeven aantal dagen. Het script is ontworpen ‘silent’ te draaien m.b.v. bijvoorbeeld de windows taskscheduler-service. Het resultaat van de actie en eventuele foutmeldingen worden in de windows eventlog geregistreerd.

*** Het gebruik van dit scripts en code van deze site is op eigen risico. Test scripts en code grondig voor ingebruikname en zorg ervoor dat je begrijpt wat er wordt uitgevoerd! | Use published scripts and code at your own risk and test thoroughly before usage. Make sure you understand te code! ***

'*********************************************************************
'bestandsnaam: SESSCLEAN.VBS
'geschreven_door: Tim van Kooten Niekerk
'versie: 11:39 22-2-2010
'info: CleanUp (session) bestanden ouder dan x dagen.
'info: Use at your own risk and test thoroughly before usage.
'*********************************************************************

'Procedure Settings (Settings en Declaraties)
  option explicit
  dim oFSO, oWSH, vEventLogHeader, vEventLogFooter
  set oFSO = CreateObject("Scripting.FileSystemObject")
  Set oWSH = WScript.CreateObject("Wscript.Shell")
  vEventLogHeader = "SESSCLEAN.VBS" & vbCrLf
  vEventLogFooter = vbCrLf & vbCrLf & "Info: Bestanden ouder dan een opgegeven aantal " & _
                    "dagen worden bij het starten van dit script verwijderd uit de " & _
                    "opgegeven directory."

'Functie fCleanUPDIR (Clean Up Session Dir)
function fCleanUPDir(vCleanUPDir,vDaysOld)
  if oFSO.FolderExists(vCleanUPDir) then
    if vDaysOld > 0 and vDaysOld < 100 then
      dim vFolder1, vBestandsNaam, f1, vBestandsDate, vFilesDeleted
      vFilesDeleted = 0
      set vFolder1 = oFSO.GetFolder(vCleanUPDir)
      set vFolder1 = vFolder1.Files
      for each f1 in vFolder1
        vBestandsNaam = f1.name
        vBestandsDate = f1.datelastmodified
        if IsDate(vBestandsDate) then
          vBestandsDate = CDate(vBestandsDate)
          if (now - vBestandsDate) > vDaysOld then
            oFSO.DeleteFile(vCleanUPDir & "" & vBestandsNaam)
            vFilesDeleted = vFilesDeleted + 1
          end if
        end if
      next
      'Schrijf de uitkomst en ingestelde waarden naar de eventlog als mededeling.
      oWSH.LogEvent 0, vEventLogHeader & _
                      vFilesDeleted & " bestand(en) van ouder dan " & vDaysOld & _
                      " dag(en) verwijderd uit " & vCleanUPDir & vEventLogFooter
      else
        'Schrijf foutmelding naar de eventlog als waarschuwing
        oWH.LogEvent 2, vEventLogHeader & _
                       "Opgegeven waarde (" & vDaysOld & " dag(en)) bevindt zich " & _
                       "buiten de scope van 0-100. Controleer de configuratie." & _
                       vEventLogFooter
      end if
  else
    'Schrijf foutmelding naar de eventlog als waarschuwing
    oWSH.LogEvent 2, vEventLogHeader & _
                    "Opgegeven Directory (" & vCleanUPDir & ") kon niet worden gevonden. " & _
                    "Controleer of de directory bestaat of op fouten in de configuratie." & _
                    vEventLogFooter
  end if  
end function

fCleanUPDir "D:Tempphpsession", 1
fCleanUPDir "D:Tempphpupload", 1

' Einde van script