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:
- for the first 30 days from today a backup should be made and kept daily.
- for the the second and third month a backup should be kept every week
- 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:
- If files are between 1 and 30 days old, keep all the files.
- 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)
- 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 ... :-(