how to create a del script?

Posted on 2012-08-21
Last Modified: 2012-08-26

I have the following backup script to back up all database in my MS SQL 2008. This script works fine. I create a job and attach to it with schedule run. But, now I want to have another script to delete the oldest backup after 7 days in the backup folder. Do you have any sample I can borrow.

Here is the backup script

DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name

--SET @path = 'C:\Backup\'


FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb' )

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @name  

       SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
       BACKUP DATABASE @name TO DISK = @fileName

       FETCH NEXT FROM db_cursor INTO @name  

CLOSE db_cursor  
DEALLOCATE db_cursor
Question by:wasabi3689

    Author Comment

    or, maybe add more in this backup script to remove the backup older than 7 days.
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    LVL 10

    Expert Comment

    I Create a table in a Central repository bkp_Files
    I populate this table from by using cmd_shell and cursuring recursively through a directory share and executing
    Restore Header 

    Open in new window

    against BAK and TRN files

    I have seperate job that identifys the 7 most recent full backups and then deletes older files.

    This method preserves the last weeks activity with restore to point in time recovery regardless of how old the files are.

    Deleting by date removes archived databases that may be required

    Author Comment

    It seems the following code is simplest

    REM del_old.bat
    REM usage: del_old MM-DD-YYY
    for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> FILES_TO_KEEP.TXT
    for /f "tokens=*" %%a IN ('xcopy *.* /L /I /EXCLUDE:FILES_TO_KEEP.TXT null') do if exist "%%~nxa" del "%%~nxa"

    But it doesn't say the directory, correct? where to put?
    LVL 10

    Accepted Solution

    ok, if you don't care about preserving the last database backup
    forfiles /p "E:\MSSQL\Backups" /s /m *.* /c "cmd /c Del @path" /d -1

    Open in new window

    Where -1 means how many days old to be deleted

    Author Comment

    I only care about the last 7 day backup or keep last 7 days backup. I don't understand your code. It's run in cmd? why there is no day stated?. I don't want to remove all backup
    LVL 10

    Expert Comment

    Changing -1. To. -7
    Change e:\mssql\backups to your backup location
    Execute from CMD prompt or wrap in xp_cmd_shell.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    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.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    760 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

    6 Experts available now in Live!

    Get 1:1 Help Now