201908 – SQL – backup file housekeeping

#Readme.txt
#**** backup files in Backup folder will be cleaned daily 1 AM
#*** 4 copies of daily full backup is kept
#** for backup need to store for a short time, longer than 4 days, put to F:\Temp folder
#* for backup need to store for a long time, put to copy_only_backup folder
# Get-executionpolicy
# Delete backup 
$Path = "F:\BACKUP"
#.bak file keep for 4 days
$FilePattern1 = "*.bak"
$Daysback = "-4" 
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
#.trn files kept for 24hrs
$FilePattern2 = "*.trn"
$LogtoDelete = $CurrentDate.AddDays(-1) 

Get-ChildItem $Path -Recurse | Where-Object {($_.LastWriteTime -lt $DatetoDelete -and $_.Name -like $FilePattern1) -or ($_.LastWriteTime -lt $LogtoDelete -and $_.Name -like $FilePattern2) } | Remove-Item

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s