Solved

Modify SQL query to delete .bak older than 7 days

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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