Solved

SQL 2008 Backup Removal

Posted on 2013-01-31
5
266 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
  • 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 39

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 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

773 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