0

As an update to my question earlier I will post the script I have up to now, the first two tasks I have managed to script perfectly, leaving me with backups younger than 30 days and, when older, only backups created on sundays. However, I am unable to get the 3rd task out of the way. Could anyone please provide me with some pointer as to how I can accomplish the 3rd task? Any help will be greatly appreciated.

Here is my code sofar:

Function FirstCheck{

$Today = GET-Date

#Check for *.BAK files to see if older than 30 days.
$BackupsOlderThan30 = GET-CHILDITEM C:\Temp\OnTracBak\ –filter *.BAK | WHERE { (($Today - $_.LastWriteTime).Days –gt 30 ) }

#Check for files older than 30 days and that are not Sunday
$NotWeeklyBackups=$BackupsOlderThan30 | where { ($_.LastWriteTime ).DayOfWeek –ne ’Sunday’ }

$NotWeeklyBackups | REMOVE-ITEM

$NotWeeklyBackups.Count

} 

I might have a question/little challenge for you. I'll try to explain it as clear as possible.

Situation: Our company uses an SLA for our customers.

One of the items in this SLA is a backup clause... In here we defined a couple of rules:

  1. for the first 30 days from today a backup should be made and kept daily.
  2. for the the second and third month a backup should be kept every week
  3. for the fourth till the twentyfourth month a backup should be kept monthly.

Now, the backups are made on a daily basis. As a result, our backupfolder currently has a size of around 4TB. (Way to large.)

Of course we could track and delete or save our backupfiles manually but of course, I would like to automate this using Powershell scripting.

The script should do the following: Check the folder and subfolder for .bak and .trn files. Get the date of these files and apply the following rules:

  1. If files are between 1 and 30 days old, keep all the files.
  2. If files are between the second and third month old, keep a full backup every week (sunday) (delete other backup files to which 2. applies)
  3. If files are between the fourth and the twentyfourth month old, keep a full backup from evey 4th week of the month... (delete other backupfiles to which 3. applies)

Currently I have no idea as to how or where to begin scripting or obviously how to perform these extended searches ... :-(

RoelofW
  • 55
  • 7

1 Answers1

1

Use Get-ChildItem cmdlet to list the files. Each file has got a LastWriteTime property, by whic you can filter the collection. Business rules for filtering are left as an exercise to the reader. Simple filtering example is like so,

$files = @(gci path-to-files) # Force gci to return an array
$cutPoint = (get-date).addDays(-30) # Get date 30 days ago
$filesToPrune = $files | ? { $_.LastWriteTime -lt $cutPoint } # Files for archive process
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • Thanks for the partial answer on this one.. As of now I've found a way to make the first two tasks work, however, I am not getting the last one to work, I have will update my question with the current solution I have, but would like to ask your or anyone's help here to get task #3 to work... thanks in advance! – RoelofW Aug 26 '13 at 09:07
  • @RoelofW What you need is to formalize the SLA rules. A verbal explanation is just the starting point. Write out a pseudocode implementation, and then start to work on it with Powershell. Calculating "n-th week of month" is locale specific. For example, in the US, Sunday is the 1st day of a week whlist Monday is the 1st day in many other locales. – vonPryz Aug 26 '13 at 09:50
  • @RoleofW How about re-defining the SLA a bit? For the elder backup sets, just keep the last full backup for every month. No need to calculate the n-th week. – vonPryz Aug 26 '13 at 09:52
  • @vonPrynx What needs to be done is; I need to figure out, from the files that are left after my "firstcheck" function (which works like a charm), which of the files are older than 4 months and delete everything that is not equal to the fourth (or last) week of the month... indeed, I need to keep the last full backup for every month, but... I am stuck on how to determine the last backup of every month... – RoelofW Aug 26 '13 at 12:52
  • @RoelofW Please post separate a question for the nth week of a month calculation. The issue is not simple. There are some [C# solutions](http://stackoverflow.com/questions/2136487/calculate-week-of-month-in-net), that can be converted into PSH with little effort. – vonPryz Aug 26 '13 at 18:14