Solved

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

Posted on 2009-04-13
6
894 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
ID: 24134350
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
ID: 24134391
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 15

Expert Comment

by:rob_farley
ID: 24134544
Vijay,

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

Rob
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:vijay11
ID: 24134581
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 15

Expert Comment

by:rob_farley
ID: 24134613
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
ID: 24134615
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server tables join on parse list 6 21
Webservices in T-SQL 3 30
Inserting oldest record into new table. 5 21
insert wont work in SQL 14 18
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

813 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

15 Experts available now in Live!

Get 1:1 Help Now