Solved

how to delete a .bak file using a job in sqlserver

Posted on 2009-04-13
6
886 Views
Last Modified: 2012-06-27
how to delete a .bak file using a job in sqlserver.let us assume the backup is in c:\software.i have to delete .bak file.can some one tell the command.i have to keep it in the job.
0
Comment
Question by:vijay11
6 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
You can either use xp_cmdshell command to delete your *.bak files from the specified path or if you are taking regular backups using maintenance plans, then you can specify the expiry set for the backup files so that age old files would be deleted automatically by the Maintenance plan itself.
0
 

Author Comment

by:vijay11
Comment Utility
i want this in sqlserver 2000.let ud suppose the files are in c:\software and i have to delete files older than 20 hrs.can you please write the command
0
 
LVL 14

Expert Comment

by:rob_farley
Comment Utility
Vijay,

Try using a maintenance plan, configuring the CleanUp History section of it. It's very easy to do.

Rob
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:vijay11
Comment Utility
if we use maintainance plan(sql 2000).we can not delete backups untill the backup jobs are totally completed.i want a script using xp_cmdshell to delete back ups older than 20 hrs
0
 
LVL 14

Expert Comment

by:rob_farley
Comment Utility
Is PowerShell okay? Otherwise, you'll need VBScript, which I'm less keen on these days.

dir c:\software\*.bak | where-object -filter {$_.LastWriteTime -lt (Get-Date).AddHours(-20)} | % {$_.Delete()}

Hope this helps,

Rob
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
Comment Utility
well you can do it by date time calculation. I used to create backfile with specified structure.

like:

backupfilename=@DBName+'_'+CAST(DATEPART(hour,@CurDT) AS varchar(2))+'.'+CAST(DATEPART(minute,@CurDT) AS varchar(2))+'.0'+'.bak';

I used to run one job to delete one day old backup from specified path. The script I am using is below. you can modify it as per your need. I am deleting 24 hours old backup.




DECLARE @BackupFolder2DaysBack AS varchar(250); 

DECLARE @BackupFolder1DayBack AS varchar(250);

DECLARE @Path AS varchar(250);

DECLARE @CurDT AS datetime;

DECLARE @PrevDT AS datetime;

DECLARE @DBName AS varchar(250);

SET @Path = 'E:\BACKUP\';

SET @CurDT = DateAdd(Day,-1,getdate());

SET @PrevDT = DateAdd(Day,-2,getdate());

-- Loop through databases

DECLARE delExpDatabases CURSOR

FOR

 select name from master..sysdatabases where name not in ('tempdb','Northwind','pubs','model','test1','test2','ASPState') 

OPEN delExpDatabases

FETCH NEXT FROM delExpDatabases INTO @DBName

WHILE (@@FETCH_STATUS = 0)

BEGIN

 

 SET @BackupFolder2DaysBack = @Path + @DBName + '\' + CONVERT(varchar(10), @PrevDT, 110);

 SET @BackupFolder1DayBack = @Path + @DBName + '\' + CONVERT(varchar(10), @CurDT, 110); 

 DECLARE @cmdStr AS varchar(250);

 SET @cmdStr = 'rmdir /Q /S '+@BackupFolder2DaysBack

 --print @cmdStr

 EXEC master..xp_cmdshell @cmdStr, NO_OUTPUT

 SET @cmdStr = 'ERASE /Q /S '+@BackupFolder1DayBack+'\*.dif '+@BackupFolder1DayBack+'\*.trn' 

 --print @cmdStr

 EXEC master..xp_cmdshell @cmdStr, NO_OUTPUT

   FETCH NEXT FROM delExpDatabases INTO @DBName

END

CLOSE delExpDatabases

DEALLOCATE delExpDatabases

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now