Sunday, December 18, 2011

Delete files based on date – ksh bash

Referring to an earlier blog post on how to delete log files or DRPlans using PowerShell I have another TSM server that is running on AIX. It’s new so I don’t have a bunch of DRM plans in the directory currently. Also this is a server I use for my internal testing and development so it’s not too heavily used by others.

Here is the directory listing before I wrote and ran the script:

image

After I wrote and ran the script:

image

I set it to only keep 7 days worth of files and now all I need to do is put it to run in my crontab every day…

Here’s the script – Cheers!


#!/usr/bin/ksh

DRM_Directory=/opt/tivoli/storage/drm/drplan
DaysToKeep=7

find $DRM_Directory -type f -mtime +$DaysToKeep -exec rm -f {} \;

Friday, December 16, 2011

Email Files with PowerShell

I have a need when dealing with customers and their disaster recovery plans provided by Tivoli Storage Manager (TSM) to get these files offsite on a regular basis. Normally every day at about the same time. It’s a great idea to email them to yourself, however not such a great idea if the email is on the server you may need to recover. I recommend in most cases that people get an external email account (Gmail, Live, Yahoo, etc.) and have the disaster recovery plans sent to them there. That way they are more likely to be able to retrieve them then if they were on the Exchange or Lotus Notes (Yes, people still use Notes for email) server that was in the datacenter that just imploded.
You need to update a few things and to make this work:
  • $SMTPServer – Make it your SMTP server
  • $DistributionList – I did it this way so you (or someone else) don’t need to edit the script when the recipients change
  • $SendingAddress – Who is this email going to be coming from?
  • $DataDirectory – What directory are the files kept in that need to be sent?
  • $RequiredFiles – The file names that need to be sent
In this instance the DR Plan itself is the last file to be created and has a different name everyday. I’m using the time difference to add it to the list of files that are needed.

# Author: Thomas Wimprine
# Creation Date: Dec 14, 2011
# Filename: SendDRPlan.ps1
# Description: Collect files needed for TSM Dr Recovery and email them to a distibution list
 
Function SendEmail {
    param (
        $FilesArray
    )
    $SMTPServer = "mail.domain.com"
    $DistributionList = "DRPlanRecipiants@domain.com"
    $SendingAddress = "TSM@domain.com"
    
    # Create our mail message objects
    $ToAddress = New-Object System.Net.Mail.MailAddress $DistributionList
    $FromAddress = New-object System.Net.Mail.MailAddress $SendingAddress
    $Message = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress
    
    $Date = Get-Date
    $Date = $Date.ToShortDateString()
    $Message.Subject = "TSM DR Plan for $Date"
    $Message.Body = @("This is the daily DR plan as created by TSM with the required files to recover. Retain this message with attachments until it is no longer needed")
    
    # Add the attachments we need to the message
    foreach ($File in $FilesArray) {
        $Attachment = New-Object System.Net.Mail.Attachment($File,'text/plain')
        $Message.Attachments.Add($Attachment)
    }
    
    $Client = New-Object System.Net.Mail.SMTPClient $SMTPServer
    
    $Client.Send($Message)
    
}
 
Function GetLatestDRPlan {
    param ($Directory)
    foreach ($File in Get-ChildItem $Directory) {
        if ($NewestFile.CreationTime -lt $File.CreationTime) {
            $NewestFile = $File
        }
    }
    $NewestFile
}
 
$DataDirectory = "D:\DRPlanDir"
$RequiredFiles = "devconfig","volhist"
$RequiredFiles += GetLatestDRPlan($DataDirectory)
 
$AttachFiles = @()
foreach ($File in $RequiredFiles) {
    $AttachFiles += "$DataDirectory\$File"
}
 
SendEmail($AttachFiles)

Thursday, December 15, 2011

PowerShell–Remove old log files

Have you ever had an application running on a server and was completely happy until you realized your drive space was slowly getting chewed up? You do a little investigation and realize that this application has been writing log files since creation and never does any cleanup.  This may not be a huge problem as before with our 3Tb hard drives, however if this has been a system that’s been in production for a few years and forgotten about this could become an issue.

This isn’t completely limited to log files, some applications actually create files for you to use and leave them on the drive assuming (correctly in some cases) that you will deal with them. One application that does this and I support is IBM’s Tivoli Storage Manager (TSM). This is a data protection/backup/archive/management software and it creates disaster recovery plans on a daily basis, when configured properly. These plans get created and place in a per-determined location for you to do what you need to get them offsite so you can recover your systems and data in the event of a disaster.

I recently went into a customer’s location and they have had this system running for many years. This server was sitting in the rack just happy as could be, however they had data on the drives that went back… a long time… Unfortunately this was their history files and the disaster recovery plans from forever ago. They didn’t need to keep it beyond its useful life, which in this case is really only a few days. This is one of the scripts I wrote for them using PowerShell to clean out all the log files and DRM plans that are no longer needed.

This gets put in a file “C:\Scripts\RemoveExpiredLogs.ps1” that is scheduled to run daily.

# Author: Thomas Wimprine
# Creation Date: Dec 14, 2011
# FileName: RemoveExpiredLogs.ps1
# Description: Delete logfiles from a specified directory based on age

$RetainDays = 10 # Number of days you want to keep logs
$LogDirectory = "D:\LogDir\" # In this instance I need the trailing slash

$DeleteDate = [DateTime]::Today.AddDays(-$RetainDays)

foreach ($file in Get-ChildItem $LogDirectory) {
if ($file.CreationTime -lt $DeleteDate) {
Remove-Item $LogDirectory$file
}
}




 



Simple & Effective – I find myself re-writing this more often than I thought I would so I hope you enjoy…

Friday, December 9, 2011

You need Watson in your business!!!

Honestly – I just want someone to need this so I can work on it. This is one of those projects that would be amazing to be involved in and I want to be involved!

Quoting from the YouTube comments:

“Watson is designed to further the science of natural language processing through advances in question and answer technology. This "first of its kind" workload optimized system has applicability in your day-to-day business analytics challenges as well.
Solving these challenges requires many of the same architectural elements as Watson. Power Systems with POWER7 processor technology is uniquely positioned to deliver these capabilities..“