在MSDB中清除SQL Server 还原备份历史数据(Purging MSDB Backup and Restore History from SQL Server)
2009-04-20 16:35
567 查看
Purging MSDB Backup and Restore History from SQL Server
Written By: Armando Prato -- 4/10/2009 -- 1 comments Stay informed - get the MSSQLTips.com newsletter and win - click here |
In one of your recent tips, you mention that SQL Server keeps a history of all backups and restores that have been made in the system. Our organization performs backups and restores frequently and I've noticed our msdb database is very large because of this. How can I purge some of this data and free some database space?
Solution
As mentioned in this tip, SQL Server keeps records of backup and restore activity in system tables within the msdb database. Without regular maintenance, these system tables can grow to very large sizes leading to a large overall size for the msdb database.
These msdb tables include:
backupfile
backupfilegroup
backupmediafamily
backupmediaset
backupset
restorefile
restorefilegroup
restorehistory
Luckily, Microsoft provides two system stored procedures that you can use to trim the size of the msdb database tables that hold this data. The first procedure deletes all backup and restore history older than a provided date. The 2nd procedure allows you to delete all backup and restore history for a specific database. Let's look at these in more detail.
On my system, looking at msdb tables backupset and restorehistory, I see the following database backups and restores that have been performed:
The first procedure that can be used is sp_delete_backuphistory. This system stored procedure takes a single parameter - a cutoff date. Any data older than the supplied date is purged from the msdb tables listed earlier in this tip. In the following example, I'll purge all data older than 4/2/2009.
-- delete all backup/restore history prior to a specified date use msdb go exec sp_delete_backuphistory '2009-04-02' go |
The 2nd system stored procedure allows you to delete historical backup data for a specific database and is called sp_delete_database_backuphistory. Unfortunately, this procedure does not offer the finer option of choosing a cutoff date; It's all or nothing. In the next example, I'll delete all backup history for a specific database.
-- delete all backup history for a specific database use msdb go exec sp_delete_database_backuphistory 'test1' go |
Next Steps
If you're concerned about the size of your msdb database, look at incorporating these system procedures as part of your routine database maintenance
If you have a lot of data to purge and you want to use sp_delete_backuphistory look at purging in batches to minimize contention on the msdb tables
Read this tip about analyzing and correcting a large SQL Server MSDB database
Read more about sp_delete_backuphistory and sp_delete_database_backuphistory in the SQL Server 2005/2008 Books Online
Read SQL Server MVP Geoff Hiten's blog entry about adding indexes to the msdb database tables to efficiently support purges
相关文章推荐
- [SQL Server][FILESTREAM] -- How to Backup and Restore a SQL Server FILESTREAM Enabled Database
- SQL Server Reporting Service 报错:报表服务器无法解密用于访问报表服务器数据库中的敏感数据或加密数据的对称密钥,必须还原备份密钥或删除所有加密的内容。
- shrink log和查询数据库 backup,restore,shrink 进度 和 还原历史 的sql (for sqlserver)re
- backup and restore database on Microsoft SQL Server 2005
- sqlserver backup use jobs and clear history backup file, restore database
- How to backup and restore database in SQL Server
- [转]Configure Network Drive Visible for SQL Server During Backup and Restore Using SSMS
- 如何还原Navicat for SQL Server的备份数据
- SQL Server Reporting Service 报错:报表服务器无法解密用于访问报表服务器数据库中的敏感数据或加密数据的对称密钥,必须还原备份密钥或删除所有加密的内容。
- SQL Server backup and restore
- 关于moss子站点的备份和还原 backup and restore subweb
- SQL Server 2005 清除备份 还原历史记录
- shrink log和查询数据库 backup,restore,shrink 进度 和 还原历史 的sql (for sqlserver)re
- SQL Server Database Backup and Restore in C#
- Asp.net 备份、还原Ms SQLServer及压缩Access数据库
- Check SQL Server Backup History Record
- Microsoft SQL Server 2005或者2008还原2000数据库备份的方法
- SQL Server Management Studio完整备份与还原数据库
- Asp.net 备份、还原Ms SQLServer及压缩Access数据库
- sql server - Restore database backup from a different database name