Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Custom cleanup procedure for backup files

By Roshan Joe Joseph,

There are times when maintenance plan is too simple to do the job, and SSIS is unreachable for you. Here is a simple stored procedure that will make use of xp_cmdshell stored procedure to cleanup backup files from a folder. You can add any additional logic relevant to your setup.

  • Disclaimer: You know database backups are like life support for DBA in times of crisis. Mess up with it at your own risk. Test well and use it with caution.
  • Note: Enabling xp_cmdshell makes your server open to threats. Please see BOL on this topic. Disable it immediately after use.

 

The procedure:
Enough warnings!! Now lets see what the procedure does. The procedure assumes that the backup filename starts with the database name eg. Mydb_someformat.bak. The backup date is calculated connecting to the msdb database. This procedure will work only on the corresponding server.

The procedure uses two parameters.A) The folder to check and b) Number of days retention ie, If you need to cleanup files older than 10 days, your second parameter is 10.

The advantage of the procedure is that you can add any custom logic to the procedure to suit your requirement.

A table is created to keep track of all the deletes.

 

Total article views: 1438 | Views in the last 30 days: 4
 
Related Articles
FORUM

database backup

database backup

FORUM

backup database

backup database

FORUM

Database backup

Database backup

FORUM

Database Backup

Database Backup

FORUM

Backup mssqlsystemresource database

Backup mssqlsystemresource database

Tags
administration    
backup / restore    
cleanup    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones