Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Delete SQL Server Backup Files Older than 7 days

$
0
0

Looking to create a vbscript to be run from SQL Server Agent on SQL Server 2008.

Script will

1) determine day of the week.

2) traverse through a fixed unc path \\storage\blah\backups\ then day of the week. So, today is Wednesday, 11/27/2013; it'll traverse to\\storage\blah\backups\Wednesday.

3) delete the backup files for last Wednesday 11/20/2013.

I am trying to avoid using XP_CMDSHELL (this is currently disabled for security measures).

I also cannot use master..xp_delete_file because some of the backups uses RedGate Backup tool and as far as I understood it, MASTER..XP_DELETE_FILE looks at header files and only deletes certain files.

This is all I have so far

Option Explicit
Dim bkupDirectory
Dim bkupFolder
Dim bkupDaysOld
Dim dtToday
Dim dtDayOfWeek
Dim objFSO 
Dim objFolder
Dim colFiles 

bkupDirectory = "\\storage\blah\backups\"
bkupDaysOld = 7
dtToday = Date()
dtDayOfWeek = WeekdayName(dtToday)

Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFolder = objFSO.GetFolder(bkupDirectory)  
Set colFiles = objFolder.Files  

For Each objFile in colFiles  
   If objFile.DateLastModified < (Date() - bkupDaysOld) Then  
   objFile.Delete(True)  
   End If  
Next

Set objFSO = Nothing
Set objFolder = Nothing
Set colFiles = Nothing
Set objFile = Nothing

Obviously adopted http://www.mssqltips.com/sqlservertip/1324/maintenance-task-to-delete-old-sql-server-backup-files/ ; however, not sure how I'll incorporatedtDayOfWeek  to the path name

Any sample / help is appreciated.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>