?
Solved

SQL 2008 Backup Removal

Posted on 2013-01-31
5
Medium Priority
?
280 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.
Suggested Courses

765 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