[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL 2008 Backup Removal

Posted on 2013-01-31
5
Medium Priority
?
283 Views
Last Modified: 2013-04-12
SQL 2008

Is there a way I can delete old backups stored on my local hard drive.

I currently have a folder on the server that I backup to.   I then back this folder up over night.
I want to have say a weeks worth of the backup files on the hard drive and then remove anything over 7 days.

Any advise?
0
Comment
Question by:Bransby-IT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 38840557
You can add a cleanup task to your maintenance plan that will delete old backups
0
 
LVL 40

Expert Comment

by:lcohan
ID: 38840711
--The code below does just that and all you need is to addapt it to your folder/file names. I suggest use the PRINT commands until you get exactly what you need.

--delete old weeky bkps

---check if zip file exists on C:\SQL_BACKUP then delete old files from G:
set nocount on
declare @crtdate varchar(255)
declare @sqlcmd varchar(4000)

set  @crtdate = CAST(YEAR(getdate()) AS VARCHAR(4))+ '_' +
case when MONTH(getdate()) < 10
      then '0'+CAST(MONTH(getdate()) AS VARCHAR(2))
      else CAST(MONTH(getdate()) AS VARCHAR(2))
end + '_' +
case when DAY(getdate()) < 10
      then '0'+CAST(DAY(getdate()) AS VARCHAR(2))
      else CAST(DAY(getdate()) AS VARCHAR(2))
end
--print @crtdate

--test command
--set @sqlcmd = 'EXEC xp_cmdshell ''dir "C:\SQL_BACKUP\SQL_BACKUP_'+@crtdate+'.zip"'''
--print (@sqlcmd)

create table #t (c1 text)
set @sqlcmd = 'insert into #t exec xp_cmdshell ''dir I:\SQL_BACKUP\SQL_BACKUP_'+@crtdate+'.zip'''
--print (@sqlcmd)
exec (@sqlcmd)
--select * from #t
--select * from #t where c1 like '%.zip'

if exists (select * from #t where c1 like '%.zip')
begin
      -- delete all old backup files from G:\sql_backup directory
      declare @source varchar(255)

      set @source = '*_backup_' + CAST(YEAR(getdate()-7) AS VARCHAR(4))+ '_' +
      case when MONTH(getdate()-7) < 10
            then '0'+CAST(MONTH(getdate()-7) AS VARCHAR(2))
            else CAST(MONTH(getdate()-7) AS VARCHAR(2))
      end + '_' +
      case when DAY(getdate()-7) < 10
            then '0'+CAST(DAY(getdate()-7) AS VARCHAR(2))
            else CAST(DAY(getdate()-7) AS VARCHAR(2))
      end
      +'*.bak'''

      --test command
      set @sqlcmd = 'EXEC xp_cmdshell ''dir G:\SQL_BACKUP\'+@source
      exec (@sqlcmd)
      --print @sqlcmd
            
      set @sqlcmd = 'EXEC xp_cmdshell ''del G:\SQL_BACKUP\'+@source
      exec (@sqlcmd)
      --print @sqlcmd

end
drop table #t

GO
0
 
LVL 3

Author Comment

by:Bransby-IT
ID: 38843628
HI Team

I thought the Clean up would not actualy remove old backup files?

Also I cant make heads of the script lol, this is what I would like to do.

2 x Backup Locations

E:\SQL2008\Access_Backups\Backup_SQL_Database_1300Hrs
E:\SQL2008\Access_Backups\Backup_SQL_Database_1800Hrs
All backup files are in there own sub folder

Filenames

.bak

want to keep backups for 7 days
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 2000 total points
ID: 38843687
backup-cleanup.docx
I have mine set for 2 weeks, see the attachment.  It is definitely deleting the old .bak files
0
 
LVL 3

Author Comment

by:Bransby-IT
ID: 38913489
Thanks will look at this this afternon.
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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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