dbTalk Databases Forums  

Script to delete backup files which are 7 days old

microsoft.public.sqlserver.tools microsoft.public.sqlserver.tools


Discuss Script to delete backup files which are 7 days old in the microsoft.public.sqlserver.tools forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
opokad06
 
Posts: n/a

Default Script to delete backup files which are 7 days old - 11-20-2008 , 11:06 AM






In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku

Reply With Quote
  #2  
Old   
Russell Fields
 
Posts: n/a

Default Re: Script to delete backup files which are 7 days old - 11-20-2008 , 01:08 PM






KPoku,

One easy way is to use your backup history to drive the process. For
example:

-- SQL Server 2005 data type
DECLARE @DeleteFiles NVARCHAR(MAX)

SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ physical_device_name + '"''' +CHAR(13)+CHAR(10)
FROM msdb.dbo.backupmediafamily ms
JOIN msdb.dbo.backupset us ON ms.media_set_id = us.media_set_id
WHERE backup_finish_date < GETDATE() - 7
AND backup_finish_date > GETDATE() - 9
and database_name = 'Admin2000FCDisaster'

EXEC (@DeleteFiles)

The two dates is just to allow for the job having been missed once or twice.
(Depending on the exact time.) You can make it more complicated, but this
is one way of working.

The other is, if you name your files consistently with date in the name,
such as:
DatabaseName_db_YYYYMMDDHHMM.BAK
DatabaseName_log_YYYYMMDDHHMM.TRN

Then you could run a script to search for existing files with names earlier
than 7 days ago. E.g.

CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))

DECLARE @Command NVARCHAR(256)

SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ '\\BackupServer\BackupPath\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)

DECLARE @DeleteFiles NVARCHAR(MAX)
SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK'
OR FullPathName LIKE '%_%.TRN')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')

EXEC(@DeleteFiles)

Beware of any typos. Test in a test enviroment, etc.

RLF

"opokad06" <opokad06 (AT) discussions (DOT) microsoft.com> wrote

Quote:
In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku


Reply With Quote
  #3  
Old   
Russell Fields
 
Posts: n/a

Default Re: Script to delete backup files which are 7 days old - 11-20-2008 , 01:08 PM



KPoku,

One easy way is to use your backup history to drive the process. For
example:

-- SQL Server 2005 data type
DECLARE @DeleteFiles NVARCHAR(MAX)

SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ physical_device_name + '"''' +CHAR(13)+CHAR(10)
FROM msdb.dbo.backupmediafamily ms
JOIN msdb.dbo.backupset us ON ms.media_set_id = us.media_set_id
WHERE backup_finish_date < GETDATE() - 7
AND backup_finish_date > GETDATE() - 9
and database_name = 'Admin2000FCDisaster'

EXEC (@DeleteFiles)

The two dates is just to allow for the job having been missed once or twice.
(Depending on the exact time.) You can make it more complicated, but this
is one way of working.

The other is, if you name your files consistently with date in the name,
such as:
DatabaseName_db_YYYYMMDDHHMM.BAK
DatabaseName_log_YYYYMMDDHHMM.TRN

Then you could run a script to search for existing files with names earlier
than 7 days ago. E.g.

CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))

DECLARE @Command NVARCHAR(256)

SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ '\\BackupServer\BackupPath\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)

DECLARE @DeleteFiles NVARCHAR(MAX)
SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK'
OR FullPathName LIKE '%_%.TRN')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')

EXEC(@DeleteFiles)

Beware of any typos. Test in a test enviroment, etc.

RLF

"opokad06" <opokad06 (AT) discussions (DOT) microsoft.com> wrote

Quote:
In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku


Reply With Quote
  #4  
Old   
Russell Fields
 
Posts: n/a

Default Re: Script to delete backup files which are 7 days old - 11-20-2008 , 01:08 PM



KPoku,

One easy way is to use your backup history to drive the process. For
example:

-- SQL Server 2005 data type
DECLARE @DeleteFiles NVARCHAR(MAX)

SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ physical_device_name + '"''' +CHAR(13)+CHAR(10)
FROM msdb.dbo.backupmediafamily ms
JOIN msdb.dbo.backupset us ON ms.media_set_id = us.media_set_id
WHERE backup_finish_date < GETDATE() - 7
AND backup_finish_date > GETDATE() - 9
and database_name = 'Admin2000FCDisaster'

EXEC (@DeleteFiles)

The two dates is just to allow for the job having been missed once or twice.
(Depending on the exact time.) You can make it more complicated, but this
is one way of working.

The other is, if you name your files consistently with date in the name,
such as:
DatabaseName_db_YYYYMMDDHHMM.BAK
DatabaseName_log_YYYYMMDDHHMM.TRN

Then you could run a script to search for existing files with names earlier
than 7 days ago. E.g.

CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))

DECLARE @Command NVARCHAR(256)

SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ '\\BackupServer\BackupPath\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)

DECLARE @DeleteFiles NVARCHAR(MAX)
SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK'
OR FullPathName LIKE '%_%.TRN')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')

EXEC(@DeleteFiles)

Beware of any typos. Test in a test enviroment, etc.

RLF

"opokad06" <opokad06 (AT) discussions (DOT) microsoft.com> wrote

Quote:
In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku


Reply With Quote
  #5  
Old   
Russell Fields
 
Posts: n/a

Default Re: Script to delete backup files which are 7 days old - 11-20-2008 , 01:08 PM



KPoku,

One easy way is to use your backup history to drive the process. For
example:

-- SQL Server 2005 data type
DECLARE @DeleteFiles NVARCHAR(MAX)

SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ physical_device_name + '"''' +CHAR(13)+CHAR(10)
FROM msdb.dbo.backupmediafamily ms
JOIN msdb.dbo.backupset us ON ms.media_set_id = us.media_set_id
WHERE backup_finish_date < GETDATE() - 7
AND backup_finish_date > GETDATE() - 9
and database_name = 'Admin2000FCDisaster'

EXEC (@DeleteFiles)

The two dates is just to allow for the job having been missed once or twice.
(Depending on the exact time.) You can make it more complicated, but this
is one way of working.

The other is, if you name your files consistently with date in the name,
such as:
DatabaseName_db_YYYYMMDDHHMM.BAK
DatabaseName_log_YYYYMMDDHHMM.TRN

Then you could run a script to search for existing files with names earlier
than 7 days ago. E.g.

CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))

DECLARE @Command NVARCHAR(256)

SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ '\\BackupServer\BackupPath\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)

DECLARE @DeleteFiles NVARCHAR(MAX)
SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK'
OR FullPathName LIKE '%_%.TRN')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')

EXEC(@DeleteFiles)

Beware of any typos. Test in a test enviroment, etc.

RLF

"opokad06" <opokad06 (AT) discussions (DOT) microsoft.com> wrote

Quote:
In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku


Reply With Quote
  #6  
Old   
Russell Fields
 
Posts: n/a

Default Re: Script to delete backup files which are 7 days old - 11-20-2008 , 01:08 PM



KPoku,

One easy way is to use your backup history to drive the process. For
example:

-- SQL Server 2005 data type
DECLARE @DeleteFiles NVARCHAR(MAX)

SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ physical_device_name + '"''' +CHAR(13)+CHAR(10)
FROM msdb.dbo.backupmediafamily ms
JOIN msdb.dbo.backupset us ON ms.media_set_id = us.media_set_id
WHERE backup_finish_date < GETDATE() - 7
AND backup_finish_date > GETDATE() - 9
and database_name = 'Admin2000FCDisaster'

EXEC (@DeleteFiles)

The two dates is just to allow for the job having been missed once or twice.
(Depending on the exact time.) You can make it more complicated, but this
is one way of working.

The other is, if you name your files consistently with date in the name,
such as:
DatabaseName_db_YYYYMMDDHHMM.BAK
DatabaseName_log_YYYYMMDDHHMM.TRN

Then you could run a script to search for existing files with names earlier
than 7 days ago. E.g.

CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))

DECLARE @Command NVARCHAR(256)

SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ '\\BackupServer\BackupPath\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)

DECLARE @DeleteFiles NVARCHAR(MAX)
SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK'
OR FullPathName LIKE '%_%.TRN')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')

EXEC(@DeleteFiles)

Beware of any typos. Test in a test enviroment, etc.

RLF

"opokad06" <opokad06 (AT) discussions (DOT) microsoft.com> wrote

Quote:
In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku


Reply With Quote
  #7  
Old   
Russell Fields
 
Posts: n/a

Default Re: Script to delete backup files which are 7 days old - 11-20-2008 , 01:08 PM



KPoku,

One easy way is to use your backup history to drive the process. For
example:

-- SQL Server 2005 data type
DECLARE @DeleteFiles NVARCHAR(MAX)

SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ physical_device_name + '"''' +CHAR(13)+CHAR(10)
FROM msdb.dbo.backupmediafamily ms
JOIN msdb.dbo.backupset us ON ms.media_set_id = us.media_set_id
WHERE backup_finish_date < GETDATE() - 7
AND backup_finish_date > GETDATE() - 9
and database_name = 'Admin2000FCDisaster'

EXEC (@DeleteFiles)

The two dates is just to allow for the job having been missed once or twice.
(Depending on the exact time.) You can make it more complicated, but this
is one way of working.

The other is, if you name your files consistently with date in the name,
such as:
DatabaseName_db_YYYYMMDDHHMM.BAK
DatabaseName_log_YYYYMMDDHHMM.TRN

Then you could run a script to search for existing files with names earlier
than 7 days ago. E.g.

CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))

DECLARE @Command NVARCHAR(256)

SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ '\\BackupServer\BackupPath\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)

DECLARE @DeleteFiles NVARCHAR(MAX)
SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK'
OR FullPathName LIKE '%_%.TRN')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')

EXEC(@DeleteFiles)

Beware of any typos. Test in a test enviroment, etc.

RLF

"opokad06" <opokad06 (AT) discussions (DOT) microsoft.com> wrote

Quote:
In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku


Reply With Quote
  #8  
Old   
Russell Fields
 
Posts: n/a

Default Re: Script to delete backup files which are 7 days old - 11-20-2008 , 01:08 PM



KPoku,

One easy way is to use your backup history to drive the process. For
example:

-- SQL Server 2005 data type
DECLARE @DeleteFiles NVARCHAR(MAX)

SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ physical_device_name + '"''' +CHAR(13)+CHAR(10)
FROM msdb.dbo.backupmediafamily ms
JOIN msdb.dbo.backupset us ON ms.media_set_id = us.media_set_id
WHERE backup_finish_date < GETDATE() - 7
AND backup_finish_date > GETDATE() - 9
and database_name = 'Admin2000FCDisaster'

EXEC (@DeleteFiles)

The two dates is just to allow for the job having been missed once or twice.
(Depending on the exact time.) You can make it more complicated, but this
is one way of working.

The other is, if you name your files consistently with date in the name,
such as:
DatabaseName_db_YYYYMMDDHHMM.BAK
DatabaseName_log_YYYYMMDDHHMM.TRN

Then you could run a script to search for existing files with names earlier
than 7 days ago. E.g.

CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))

DECLARE @Command NVARCHAR(256)

SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ '\\BackupServer\BackupPath\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)

DECLARE @DeleteFiles NVARCHAR(MAX)
SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK'
OR FullPathName LIKE '%_%.TRN')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')

EXEC(@DeleteFiles)

Beware of any typos. Test in a test enviroment, etc.

RLF

"opokad06" <opokad06 (AT) discussions (DOT) microsoft.com> wrote

Quote:
In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku


Reply With Quote
  #9  
Old   
Russell Fields
 
Posts: n/a

Default Re: Script to delete backup files which are 7 days old - 11-20-2008 , 01:08 PM



KPoku,

One easy way is to use your backup history to drive the process. For
example:

-- SQL Server 2005 data type
DECLARE @DeleteFiles NVARCHAR(MAX)

SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ physical_device_name + '"''' +CHAR(13)+CHAR(10)
FROM msdb.dbo.backupmediafamily ms
JOIN msdb.dbo.backupset us ON ms.media_set_id = us.media_set_id
WHERE backup_finish_date < GETDATE() - 7
AND backup_finish_date > GETDATE() - 9
and database_name = 'Admin2000FCDisaster'

EXEC (@DeleteFiles)

The two dates is just to allow for the job having been missed once or twice.
(Depending on the exact time.) You can make it more complicated, but this
is one way of working.

The other is, if you name your files consistently with date in the name,
such as:
DatabaseName_db_YYYYMMDDHHMM.BAK
DatabaseName_log_YYYYMMDDHHMM.TRN

Then you could run a script to search for existing files with names earlier
than 7 days ago. E.g.

CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))

DECLARE @Command NVARCHAR(256)

SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ '\\BackupServer\BackupPath\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)

DECLARE @DeleteFiles NVARCHAR(MAX)
SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK'
OR FullPathName LIKE '%_%.TRN')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')

EXEC(@DeleteFiles)

Beware of any typos. Test in a test enviroment, etc.

RLF

"opokad06" <opokad06 (AT) discussions (DOT) microsoft.com> wrote

Quote:
In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku


Reply With Quote
  #10  
Old   
Russell Fields
 
Posts: n/a

Default Re: Script to delete backup files which are 7 days old - 11-20-2008 , 01:08 PM



KPoku,

One easy way is to use your backup history to drive the process. For
example:

-- SQL Server 2005 data type
DECLARE @DeleteFiles NVARCHAR(MAX)

SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ physical_device_name + '"''' +CHAR(13)+CHAR(10)
FROM msdb.dbo.backupmediafamily ms
JOIN msdb.dbo.backupset us ON ms.media_set_id = us.media_set_id
WHERE backup_finish_date < GETDATE() - 7
AND backup_finish_date > GETDATE() - 9
and database_name = 'Admin2000FCDisaster'

EXEC (@DeleteFiles)

The two dates is just to allow for the job having been missed once or twice.
(Depending on the exact time.) You can make it more complicated, but this
is one way of working.

The other is, if you name your files consistently with date in the name,
such as:
DatabaseName_db_YYYYMMDDHHMM.BAK
DatabaseName_log_YYYYMMDDHHMM.TRN

Then you could run a script to search for existing files with names earlier
than 7 days ago. E.g.

CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))

DECLARE @Command NVARCHAR(256)

SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ '\\BackupServer\BackupPath\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)

DECLARE @DeleteFiles NVARCHAR(MAX)
SET @DeleteFiles = ''

SELECT @DeleteFiles = @DeleteFiles +' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK'
OR FullPathName LIKE '%_%.TRN')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')

EXEC(@DeleteFiles)

Beware of any typos. Test in a test enviroment, etc.

RLF

"opokad06" <opokad06 (AT) discussions (DOT) microsoft.com> wrote

Quote:
In our environment all jobs are scripted, we do not use maintenance plan.

Is there a script that I can add to my jobs that will run daily and delete
jobs that arre 7 days old?
--
KPoku


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.