Exported on 20-Jan-2022 16:46:18
Parameters
1 - Verify Rotation Directories - Windows
Login as user {Windows User} on node {Windows Node}
$backupLocation = "{backupDirectoryLocation}"
Write-Host "Verify that the Backup directory: $backupLocation exists."
if (Test-Path $backupLocation) {
Write-Host "The Backup directory: $backupLocation exists!"
} else {
Write-Error "The Backup directory: $backupLocation does not exist!"
}
"daily", "weekly", "monthly" | ForEach-Object -Process {
Write-Host "========================"
$directoryName = $_
Write-Host "Verify that the $directoryName directory: `
$backupLocation$directoryName exists."
if (Test-Path $backupLocation$directoryName) {
Write-Host "The $directoryName directory: `
$backupLocation$directoryName exists!"
} else {
Write-Host "The $directoryName directory does not exist!"
New-Item -Path $backupLocation -Name $directoryName -ItemType `
"directory"
if (Test-Path $backupLocation$directoryName) {
Write-Host "The $directoryName directory: `
$backupLocation$directoryName has been successfully created!"
} else {
Write-Error "Creating directory $backupLocation$directoryName was `
unsuccessful."
}
}
}
2 - Cleanup and Rotate Root Files - Windows
Login as user {Windows User} on node {Windows Node}
$backupLocation = "{backupDirectoryLocation}"
$daysRotation = {daysRotation}
$weeksRotation = {weeksRotation}
$monthsRotation = {monthsRotation}
$dayOfTheWeek = {dayOfTheWeek}
$dayOfTheMonth = {dayOfTheMonth}
$date = Get-Date
Write-Host "Rotating the root files: "$backupLocation
Write-Host "========================"
$files = Get-ChildItem "$backupLocation" | `
Where-Object {$_.Attributes -ne "directory"} | `
Sort-Object LastWriteTime -Descending
if ($files) {
$files | ForEach-Object -Process {
$fileDate = Get-Date $_.LastWriteTime
if ($fileDate -ge $date.AddDays(-$daysRotation)) {
Write-Host "Rotating to daily: $_"
Copy-Item "$backupLocation$_" `
-Destination "$backupLocationdaily"
}
if ($fileDate -ge $date.AddDays(-$weeksRotation*7) `
-and [int]$fileDate.DayOfWeek -eq $dayOfWeek) {
Write-Host "Rotating to weekly: $_"
Copy-Item "$backupLocation$_" `
-Destination "$backupLocationweekly"
}
if ($fileDate -ge $date.AddDays(-$monthsRotation*30) `
-and [int]$fileDate.DayOfWeek -eq $dayOfMonth) {
Write-Host "Rotating to monthly: $_"
Copy-Item "$backupLocation$_" `
-Destination "$backupLocationmonthly"
}
Remove-Item "$backupLocation$_"
}
} else {
Write-Host "There are no files in " $backupLocation " to rotate."
}
3 - Cleanup and Rotate Archived Files - Windows
The following steps in this group to rotate the archived files can be run in parallel and are configured to do so in Attune.
3.1 - Cleanup and Rotate Daily Files - Windows
Login as user {Windows User} on node {Windows Node}
$backupLocation = "{backupDirectoryLocation}daily"
$daysRotation = {daysRotation}
$date = Get-Date
Write-Host "Rotating the daily files: "$backupLocation
Write-Host "========================"
$files = Get-ChildItem "$backupLocation" | `
Where-Object {$_.Attributes -ne "directory"} | `
Sort-Object LastWriteTime -Descending
if ($files) {
$files | ForEach-Object -Process {
$fileDate = Get-Date $_.LastWriteTime
$backupCount = (Get-ChildItem -Path $backupLocation | `
Group {$_.LastWriteTime.ToString("yyyy-MM-dd")}).count
if ($fileDate -le $date.AddDays(-$daysRotation)) {
if ($backupCount -le $daysRotation) {
Write-Host "Removing: "$_
Remove-Item "$backupLocation\$_"
} else {
Write-Host "File: " $_ " is within the " $daysRotation `
" days rotation."
}
} else {
Write-Host "File: " $_ " is within the " $daysRotation `
" days rotation."
}
}
} else {
Write-Host "There are no files in " $backupLocation " to rotate."
}
3.2 - Cleanup and Rotate Weekly Files - Windows
Login as user {Windows User} on node {Windows Node}
$backupLocation = "{backupDirectoryLocation}weekly"
$weeksRotation = {weeksRotation}
$date = Get-Date
Write-Host "Rotating the weekly files: "$backupLocation
Write-Host "========================"
$files = Get-ChildItem "$backupLocation" | `
Where-Object {$_.Attributes -ne "directory"} | `
Sort-Object LastWriteTime -Descending
if ($files) {
$files | ForEach-Object -Process {
$fileDate = Get-Date $_.LastWriteTime
$backupCount = (Get-ChildItem -Path $backupLocation | `
Group {$_.LastWriteTime.ToString("yyyy-MM-dd")}).count
if ($fileDate -le $date.AddDays(-$weeksRotation)) {
if ($backupCount -le $weeksRotation) {
Write-Host "Removing: "$_
Remove-Item "$backupLocation\$_"
} else {
Write-Host "File: " $_ " is within the " $weeksRotation `
" weeks rotation."
}
} else {
Write-Host "File: " $_ " is within the " $weeksRotation `
" weeks rotation."
}
}
} else {
Write-Host "There are no files in " $backupLocation " to rotate."
}
3.3 - Cleanup and Rotate Monthly Files - Windows
Login as user {Windows User} on node {Windows Node}
$backupLocation = "{backupDirectoryLocation}monthly"
$monthsRotation = {monthsRotation}
$date = Get-Date
Write-Host "Rotating the monthly files: "$backupLocation
Write-Host "========================"
$files = Get-ChildItem "$backupLocation" | `
Where-Object {$_.Attributes -ne "directory"} | `
Sort-Object LastWriteTime -Descending
if ($files) {
$files | ForEach-Object -Process {
$fileDate = Get-Date $_.LastWriteTime
$backupCount = (Get-ChildItem -Path $backupLocation | `
Group {$_.LastWriteTime.ToString("yyyy-MM-dd")}).count
if ($fileDate -le $date.AddDays(-$monthsRotation)) {
if ($backupCount -le $monthsRotation) {
Write-Host "Removing: "$_
Remove-Item "$backupLocation\$_"
} else {
Write-Host "File: " $_ " is within the " $monthsRotation `
" months rotation."
}
} else {
Write-Host "File: " $_ " is within the " $monthsRotation `
" months rotation."
}
}
} else {
Write-Host "There are no files in " $backupLocation " to rotate."
}
Grab this Attune Blueprint to perform the Grandfather-father-son file rotation scheme. The scripts are written to perform daily, weekly, and monthly rotation cycles.
The rotation directories will be verified each time the job is run and create any directories as required. All files in the backup directory will be rotated based on the files 'Last Write Time'.
Job Configuration
The number of daily, weekly, and monthly backups to keep are configurable.
The day of the week and day of the month for the weekly and monthly backups are configurable. For example the day of the week 1 to 7 where 1 is Sunday. If Sunday were configured then the Sunday backups would be retained in the weekly rotation cycle.
Grandfather-father-son File Rotation
The Grandfather-father-son is a common backup rotation scheme that rotates with a FIFO (First in, first out) system in daily, weekly, and monthly cycles. Other cycles time cycles could be implemented based on your requirements.
Testing
This Blueprint has been tested on Windows 10 PowerShell 5.1.