Download Attune CE

Perform Backup File Rotation on Windows | Grandfather-Father-son Scheme

Exported on 20-Jan-2022 16:46:18

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.

Parameters

Name Type Script Reference Default Value Comment
Backup Directory Location Text backupDirectoryLocation The location of the backup directory. Example: 'C:\backup\'
Day of the Month Text dayOfTheMonth The day of the month 1 to 28. Example: "1"
Day of the week Text dayOfTheWeek The day of the week 1 to 7 where 1 is Sunday. Example: "7"
Days Rotation Text daysRotation The number of days in rotation. Example: "3"
Months Rotation Text monthsRotation The number of months in rotation. Example: "3"
Weeks Rotation Text weeksRotation The number of weeks in rotation. Example: "3"
Windows Node Windows Server windowsNode
Windows User Windows OS Credential windowsUser

1 - Verify Rotation Directories - Windows

This step has the following parameters

Name Script Reference Default Value
Backup Directory Location {backupDirectoryLocation} None

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Windows Node
  2. Login as user {Windows User}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
$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

This step has the following parameters

Name Script Reference Default Value
Weeks Rotation {weeksRotation} None
Day of the Month {dayOfTheMonth} None
Backup Directory Location {backupDirectoryLocation} None
Days Rotation {daysRotation} None
Months Rotation {monthsRotation} None
Day of the week {dayOfTheWeek} None

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Windows Node
  2. Login as user {Windows User}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
$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

This step has the following parameters

Name Script Reference Default Value
Backup Directory Location {backupDirectoryLocation} None
Days Rotation {daysRotation} None

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Windows Node
  2. Login as user {Windows User}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
$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

This step has the following parameters

Name Script Reference Default Value
Weeks Rotation {weeksRotation} None
Backup Directory Location {backupDirectoryLocation} None

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Windows Node
  2. Login as user {Windows User}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
$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

This step has the following parameters

Name Script Reference Default Value
Backup Directory Location {backupDirectoryLocation} None
Months Rotation {monthsRotation} None

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Windows Node
  2. Login as user {Windows User}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
$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."
}