Solved

Modify SQL query to delete .bak older than 7 days

Posted on 2008-10-07
3
653 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

635 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