Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add lab for powershell retention
- Loading branch information
1 parent
98a9952
commit aedfcff
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#Retention plan for BACKUP TO URL backups in Azure blob storage | ||
#Breaking lease if necessary: https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/deleting-backup-blob-files-with-active-leases | ||
|
||
Clear-Host | ||
|
||
#TODO | ||
$context = New-AzureStorageContext -StorageAccountName "ACCOUNT NAME HERE!" -StorageAccountKey "STORAGE KEY HERE!" | ||
$container = "CONTAINER NAME HERE (subfolder path)!" | ||
$RetentionWeeks = 8 | ||
|
||
[DateTime]$today = (Get-Date) | ||
[Int]$dateofweek = ($today.DayOfWeek) #get the day of the week (0 = Sunday) so that we're always deleting a whole week at a time, in the case of a weekly full schedule | ||
#Delete whole weeks only | ||
$RetentionDays = ($RetentionWeeks * -7) -1 -$dateofweek | ||
$BackupFileExtension = '*.bak' | ||
|
||
write-host (Get-Date) | ||
|
||
Get-AzureStorageBlob -Container $container -Context $context | ` | ||
where-object { $_.PSIsContainer -ne $true -and $_.LastModified -lt (get-date).AddDays($RetentionDays) -and $_.Name -Like $BackupFileExtension ` | ||
} | Remove-AzureStorageBlob -Verbose #-whatif | ||
|
||
$RetentionDays = $RetentionWeeks * -7 -$dateofweek | ||
$BackupFileExtension = '*.dif' | ||
|
||
Get-AzureStorageBlob -Container $container -Context $context | ` | ||
where-object { $_.PSIsContainer -ne $true -and $_.LastModified -lt (get-date).adddays($RetentionDays) -and $_.Name -Like $BackupFileExtension ` | ||
} | Remove-AzureStorageBlob -Verbose #-whatif | ||
|
||
|
||
$RetentionDays = $RetentionWeeks * -7 -$dateofweek | ||
$BackupFileExtension = '*.trn' | ||
|
||
Get-AzureStorageBlob -Container $container -Context $context | ` | ||
where-object { $_.PSIsContainer -ne $true -and $_.LastModified -lt (get-date).adddays($RetentionDays) -and $_.Name -Like $BackupFileExtension ` | ||
} | Remove-AzureStorageBlob -Verbose #-whatif | ||
|
||
write-host (Get-Date) |