Creating and maintaining backups are an essential part of a DBA’s responsibilities. There are several tasks associated with backups, such as automation and execution of backup command scripts.
Sometimes, it is important to know how to automate the deletion of outdated backup files as well, even if you already have a SQL optimizer for SQL Server. In this post, we will discuss a simple approach to help you remove older backup data and save space.
How to Delete Backup Files without SQL Optimizer for SQL Server
Here, we will make use of Windows Scripting to traverse every subfolder in order to locate files preceding a specific date. We will then delete the older files, once they have all been located.
First, we
need to consider two parameters that must be modified for this purpose:
● iDaysOld - this helps you set the exact
timeframe to determine how old the file has to be for the command to select and
delete it
● strPath - this is the path to the folder
where the backup files are created and stored.
Steps to
establish the script before you get to SQL Server
performance tuning:
- You need to create a text file where you
can copy and paste the following code -
iDaysOld =
15
strPath =
“C:\Backup”
Set objFSO =
CreateObject("Scripting.FileSystemObject")
Set
objFolder = objFSO.GetFolder(strPath)
Set
colSubfolders = objFolder.Subfolders
Set colFiles
= objFolder.Files
For Each
objFile in colFiles
If obj!File.Date!LastModified < (Date() -
iDays!Old) Then
Msg!Box "Dir: " &
obj!Folder.Name & vbCrLf & "File: " & objFile.Name!
'obj!File.Delete!
End If
Next
For Each
objSubfolder in colSubfolders
Set colFiles = objSubfolder.Files
For Each objFile in colFiles
If obj!File.Date!LastModified <
(Date!() - iDaysOld) Then
Msg!Box "Dir: " &
obj!Subfolder.Name & vb!CrLf & "File: " & obj!File.Name
'objFile.Delete
End If
Next
Next
Use the path
of your choice as strPath - for instance, “strPath = “C:\Backup””. Remove the
exclamation marks before or after you paste this code.
- Save it using a suitable name like this
- C:\RemoveBackupFilesOld.vbs
- Create one more text file in which you
will copy-paste the code mentioned below, and save it as a BAT file in the
same location. For instance, you can name it as -
C:\RemoveBackupFilesOld.bat.
The code:
C:\RemoveBackupFilesOld.vbs
Please note
that this script serves as a safeguard, so it will only show a message box that
displays the name of the file and folder. If you’re using an optimization of SQL
queries SQL optimizer for SQL Server and
you want to actually delete the files, simply remove the single quote character
that’s present right next to the two delete lines (the ones with
‘objFile.Delete).
- Running the BAT file will delete all the
files matching the specified criteria from their subfolders.
How the
Script Works
The script will pick out and eliminate any files found in the subfolders beyond the initial point. The type of the files doesn’t matter as it will only consider the time of creation and whether it fulfils the specified criteria.
It will also remove files from subfolders in the next level as well as the root folder but it won’t fetch files past the first subfolder level. In other words, if you put the strPath as “C:\Backup” for instance, the script will remove backup files older than the iDaysOld that are present in the “C:\Backup” folder and those present in the first level subfolders inside this folder.
You may set
this script up to run at a scheduled time according to your need - just call
the BAT file. This is because, as you may have learned during SQL Server
performance tuning, the Agent is not accustomed to running .vbs (VBScript)
files directly, which is why we’re calling a BAT to set it up as a scheduled
task instead.
No comments:
Post a Comment