SQL Back up RetainDays Option and deleting older back up files

When Sage 500 creates a back up plan, the Step within the job has the Option RetainDays included with it.  The question came up today if this option deletes the older back files the job created.  The option does not delete any back up files.  The option RETAINDAYS achieves a very different purpose with Backup. It just prevents users from overwriting the backup file if the user is trying to do it with INIT option. Otherwise, it really does not do anything else.  If the user is using the FORMAT option, the backup will be overwritten anyway. This means the use of RETAINDAYS is very much limited.

If you need a way to delete older backup files, you can create a .BAT file or a PowerShell script which can then be scheduled with the Task Scheduler to run automatically.

The code for the .BAT file would be similar to the following:

c:\Windows\System32>ForFiles /p "C:\Junk" /s /d -30 /c "cmd /c del @file"

Substitute the folder path and the amount of days with desired values and you are done

For PowerShell it would be as follows:

# Delete all Files in a folder older than 30 days

$Path = "C:\Junk"
$Daysback = "-30"

$CurrentDate = Get-CurrentDate
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

Parents
  • Lou - I do it like you do.  We have an added twist though.  I bought an 8 terabyte drive and copy everything there as a double back flip emergency if there is an emergency backup.  We delete our backups after 7 days using powershell on the main backup volume but keep the 8 terabyte drive with months of backups.  We also use an onsite backup product called Rapid Recovery and that is mirrored offsite.

    Joe - I don't use Ola's stuff because we are not 24x7 but his scripts are considered the gold standard.

Reply
  • Lou - I do it like you do.  We have an added twist though.  I bought an 8 terabyte drive and copy everything there as a double back flip emergency if there is an emergency backup.  We delete our backups after 7 days using powershell on the main backup volume but keep the 8 terabyte drive with months of backups.  We also use an onsite backup product called Rapid Recovery and that is mirrored offsite.

    Joe - I don't use Ola's stuff because we are not 24x7 but his scripts are considered the gold standard.

Children
No Data