Solved

SQL 2008 Backup Removal

Posted on 2013-01-31
5
275 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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.

690 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