Solved

Modify SQL query to delete .bak older than 7 days

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help wiht :CAST AS Double 11 47
Set the max value for a column 7 39
My Query is not giving correct result. Please help 5 51
SQL query with cast 38 53
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
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 …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

861 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