Created
December 31, 2015 11:40
-
-
Save beancurd1/9c9e1688b625a891a4b7 to your computer and use it in GitHub Desktop.
A useful script to manage GFS retention in multiple folders which contain backup files such as "backup_ddMMyyy.7z". It looks for backups done within a predefined year, then group them into Monthly, Weekly & Daily
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Created by beancurd1 (please distribute the code with this session, thanks) | |
# https://gist.github.com/beancurd1 | |
# A useful script to manage GFS retention in a single folder which contains backup files | |
# such as "backup_ddMMyyy.7z". It looks for backups done within a year, then put them | |
# into Monthly, Weekly & Daily.groups | |
Clear | |
# Create a String array to store end of month dates for X months (e.g. 31DEC15 to 31JAN15) | |
# define how many years of data to retain (it will search files created in that year, split them into Monthly, Weekly and Daily) | |
$year=1 | |
for($i=0; $i -ge (-11*$year); $i--) { | |
$date = (Get-Date).AddMonths($i) | |
$startofmonth = Get-Date $date -Day 1 -hour 0 -minute 0 -second 0 | |
$endofmonth = (($startofmonth).AddMonths(1).AddSeconds(-1)) | |
[array]$eomArray += @($endofmonth.ToString("dd MMM yyyy")) | |
} | |
# Find Monthly (bakups done on the last day of a month for the last 12 months), | |
# Weekly (bakups done on Sat & Sun in last 12 months), | |
# Daily (backups done in last 12 months except above) | |
# It loops through the folders (e.g. Databases, Reports, Invoices, etc) in Bakcup folder and identify files as M/W/D backups | |
# Note: It groups backups by creation date, you need to include you actions (e.g. move, del, rename, etc.) | |
$backupPath = "X:\Backup\*" | |
$dirs = Get-Item -Path "$backupPath" | ?{$_.PSIsContainer} | |
ForEach ($dir in $dirs){ | |
Get-ChildItem "$($dir.FullName)\*" -include @("*.7z","*.zip","*.bak") | | |
Where-Object { $_.CreationTime -ge (Get-Date).AddYears(-1*$year) -and $_.CreationTime -le (Get-Date) }| | |
Sort-Object CreationTime -Descending | | |
Foreach-Object{ | |
if ($eomArray -contains $_.CreationTime.ToString("dd MMM yyyy")) { | |
# Write-Host $_ $_.CreationTime $_.CreationTime.DayOfWeek "Monthly" | |
# Your Action HERE | |
} elseif ($_.CreationTime.DayOfWeek -eq 'Saturday' -or $_.CreationTime.DayOfWeek -eq 'Sunday') { | |
# Write-Host $_ $_.CreationTime $_.CreationTime.DayOfWeek ”Weekly" | |
# Your Action HERE | |
} else { | |
# Write-Host $_ $_.CreationTime $_.CreationTime.DayOfWeek "Daily" | |
# Your Action HERE | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment