Solved

Modify SQL query to delete .bak older than 7 days

Posted on 2008-10-07
3
642 Views
Last Modified: 2012-05-05
I have a SQL query file and am not to familiar with SQL.  I need to add "delete bak files older than 7 days" into the below script:

DECLARE @BackupFile varchar(255), @DB varchar(30), @Description varchar(255), @LogFile varchar(50)
DECLARE @Name varchar(30), @MediaName varchar(30), @BackupDirectory nvarchar(200)
SET @BackupDirectory = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\'
--Add a list of all databases you don't want to backup to this.
DECLARE Database_CURSOR CURSOR FOR SELECT name FROM sysdatabases WHERE name <> 'Northwind'
OPEN Database_Cursor
FETCH next FROM Database_CURSOR INTO @DB
WHILE @@fetch_status = 0

    BEGIN
          SET @Name = @DB + '( Daily BACKUP )'
          SET @MediaName = @DB + '_Dump' + CONVERT(varchar, CURRENT_TIMESTAMP , 112)
          SET @BackupFile = @BackupDirectory + + @DB + '_' + 'Full' + '_' +
                CONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'
          SET @Description = 'Normal' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.'

          IF (SELECT COUNT(*) FROM msdb.dbo.backupset WHERE database_name = @DB) > 0 OR @DB = 'master'
                BEGIN
                      SET @BackupFile = @BackupDirectory + @DB + '_' + 'Full' + '_' +
                            CONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'
                      --SET some more pretty stuff for sql server.
                      SET @Description = 'Full' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.'
                END      
          ELSE
                BEGIN
                      SET @BackupFile = @BackupDirectory + @DB + '_' + 'Full' + '_' +
                            CONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'
                      --SET some more pretty stuff for sql server.
                      SET @Description = 'Full' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.'
                END
                BACKUP DATABASE @DB TO DISK = @BackupFile
                WITH NAME = @Name, DESCRIPTION = @Description ,
                MEDIANAME = @MediaName, MEDIADESCRIPTION = @Description ,
                STATS = 10
          FETCH next FROM Database_CURSOR INTO @DB
END
CLOSE Database_Cursor
DEALLOCATE Database_Cursor
0
Comment
Question by:d_ww
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 41

Expert Comment

by:graye
ID: 22664748
Just curious.... why not use the built-in Maintenance Plan feature to create a "Clean up" Task?
If for some reason, you don't like using Maintenance Plans, you could at least let it create the T-SQL for you, then do a cut-n-paste
0
 

Author Comment

by:d_ww
ID: 22669201
Sorry the version I am using is SQL 2008 studio express.  There is no maintenance feature.  Have to manually created the scripts.
0
 
LVL 41

Accepted Solution

by:
graye earned 50 total points
ID: 22713781
Here is what get's automatically generated by the Maintenance Plan....
EXECUTE master.dbo.xp_delete_file 0,N'C:\Microsoft SQL Server\MSSQL\Backup',N'bak',N'2008-09-16T12:40:57'
Does the Express Edition have the xp_delete_file stored procedure?
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question