Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Deleting old files (with/without archive bit) Expand / Collapse
Author
Message
Posted Saturday, December 13, 2008 9:40 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 12:59 PM
Points: 810, Visits: 2,119
Comments posted to this topic are about the item Deleting old files (with/without archive bit)

Gaby
________________________________________________________________
"In theory, theory and practice are the same. In practice, they are not."
- Albert Einstein
Post #619223
Posted Friday, January 2, 2009 9:53 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, April 11, 2013 6:43 AM
Points: 12, Visits: 88
Also try the following code. We had some issues where our tape backup system missed archiving some DB backup files so I scheduled the following code to check for files with the "archive" bit set. I cut this code out of a larger set of code and didn't test it -so I may have missed a chunk, but you get the picture. In the larger picture, this process would send an e-mail to the DBA team if anything wasn't archived (any results in #hb_backups).

P.S. Our server admin team switched to Tivoli backup system about 18 months ago which rendered this check useless (Tivoli doesn't change the archive bit flag).

P.P.S. We've since disabled the use of "xp_cmdshell" for security reasons - so this code would no longer work anyway.

create table #hb_backups
( database_name nvarchar(128),
backup_type char(1),
backup_start_date datetime,
backup_finish_date datetime,
backup_file_name nvarchar(200),
physical_name nvarchar(1000)
)

set nocount on
declare @database_name nvarchar(128), @backup_type char(1), @backup_start_date datetime, @backup_finish_date datetime, @physical_name nvarchar(260)
declare backup_cur cursor for
select mf.physical_device_name, s.type, s.database_name, s.backup_start_date, s.backup_finish_date
from msdb.dbo.backupmediafamily mf inner join msdb.dbo.backupset s on mf.media_set_id = s.media_set_id
where s.backup_start_date > (getdate() - 3)
open backup_cur
fetch next from backup_cur into @physical_name, @backup_type, @database_name, @backup_start_date, @backup_finish_date
while @@fetch_status = 0
begin
insert into #hb_backups (physical_name)
exec ('exec master.dbo.xp_cmdshell ''dir "' + @physical_name + '" /a:a /b''')
delete from #hb_backups where physical_name is null or physical_name in('File Not Found')
update #hb_backups set
database_name = @database_name,
backup_type = @backup_type,
backup_start_date = @backup_start_date,
backup_finish_date = @backup_finish_date,
backup_file_name = @physical_name
where database_name is null
fetch next from backup_cur into @physical_name, @backup_type, @database_name, @backup_start_date, @backup_finish_date
end
close backup_cur
deallocate backup_cur
Post #628921
Posted Friday, January 2, 2009 10:09 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 12:59 PM
Points: 810, Visits: 2,119
Dennis Stephani (1/2/2009)
Also try the following code. We had some issues where our tape backup system missed archiving some DB backup files so I scheduled the following code to check for files with the "archive" bit set. I cut this code out of a larger set of code and didn't test it -so I may have missed a chunk, but you get the picture. In the larger picture, this process would send an e-mail to the DBA team if anything wasn't archived (any results in #hb_backups).

P.S. Our server admin team switched to Tivoli backup system about 18 months ago which rendered this check useless (Tivoli doesn't change the archive bit flag).

P.P.S. We've since disabled the use of "xp_cmdshell" for security reasons - so this code would no longer work anyway.

create table #hb_backups
( database_name nvarchar(128),
backup_type char(1),
backup_start_date datetime,
backup_finish_date datetime,
backup_file_name nvarchar(200),
physical_name nvarchar(1000)
)

set nocount on
declare @database_name nvarchar(128), @backup_type char(1), @backup_start_date datetime, @backup_finish_date datetime, @physical_name nvarchar(260)
declare backup_cur cursor for
select mf.physical_device_name, s.type, s.database_name, s.backup_start_date, s.backup_finish_date
from msdb.dbo.backupmediafamily mf inner join msdb.dbo.backupset s on mf.media_set_id = s.media_set_id
where s.backup_start_date > (getdate() - 3)
open backup_cur
fetch next from backup_cur into @physical_name, @backup_type, @database_name, @backup_start_date, @backup_finish_date
while @@fetch_status = 0
begin
insert into #hb_backups (physical_name)
exec ('exec master.dbo.xp_cmdshell ''dir "' + @physical_name + '" /a:a /b''')
delete from #hb_backups where physical_name is null or physical_name in('File Not Found')
update #hb_backups set
database_name = @database_name,
backup_type = @backup_type,
backup_start_date = @backup_start_date,
backup_finish_date = @backup_finish_date,
backup_file_name = @physical_name
where database_name is null
fetch next from backup_cur into @physical_name, @backup_type, @database_name, @backup_start_date, @backup_finish_date
end
close backup_cur
deallocate backup_cur

Good point...I also thought about using the dir /a:a option which would have also helped me avoid another awkward table manipulation scenario.

As for xp_cmdshell, as much as I'd love to get rid of that, we have too many legacy systems that potentially use the feature. We have a third party program backing up to the local drive (in our case Litespeed) and a third party archiving it. A short cut is to keep only one backup stored locally, the most recent one, and damn the archiving. Hopefully the archive will have finished in time. I guess the archive bit option is for people like me who are truly paranoid. :D


Gaby
________________________________________________________________
"In theory, theory and practice are the same. In practice, they are not."
- Albert Einstein
Post #628937
Posted Wednesday, February 9, 2011 2:44 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Sunday, July 20, 2014 1:08 PM
Points: 537, Visits: 1,918
i tried your code but i keep getting the error The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. [SQLSTATE 22007] (Error 296) The statement has been terminated. [SQLSTATE 01000] (Error 3621). The step failed
Post #1061575
Posted Monday, January 16, 2012 9:47 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 1:51 PM
Points: 321, Visits: 1,272
I tried you script it executed well but not deleting the file from folder.

Aim to inspire rather than to teach.
SQL Server DBA
Post #1236706
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse