SQL Backup 7

Help for older versions available.

Deleting backup and restore history manually

The SQL Backup Pro graphical user interface (GUI) provides the option to delete the backup and restore history from the msdb database and SQL Server Compact database for each SQL Server (Tools > Server Options > File Management). For more information, see File management options.

Selecting this option can cause performance problems when running backup or restore operations, or when using the SQL Backup Pro GUI. If this is the case, you may find it helpful to clear the option and delete backup and restore history manually or as a scheduled task. 

The following script deletes backup and restore history older than the specified number of days from the msdb database, the SQL Server Compact database and the SQL Backup Pro GUI activity cache:

  1. ----------------------------------------------------------------------------------------------------
  2. -- Delete backup history from SQL Backup Pro's SQL Server Compact database
  3. ----------------------------------------------------------------------------------------------------
  4. SET NOCOUNT ON
  5. DECLARE @backup_id INT ,
  6. @command NVARCHAR(4000) ,
  7. @retain_date NVARCHAR(64) ,
  8. @IDList VARCHAR(2000)
  9.  
  10. -- number of days of history that you want to retain (currently 30 days)
  11. SET @retain_date = GETDATE() - 30
  12.  
  13. -- temp table for ids we're deleting.
  14. IF OBJECT_ID('tempdb..#DeleteTheseIDs') IS NOT NULL
  15. DROP TABLE #DeleteTheseIDs
  16. CREATE TABLE #DeleteTheseIDs ( id INT )
  17.  
  18. -- get total from the SQL Server Compact database (this will also show an error if its corrupted)
  19. EXEC [master]..sqbdata N'select COUNT(*) AS TotalBackupRows from backuphistory'
  20. EXEC [master]..sqbdata N'select COUNT(*) AS TotalRestoreRows from restorehistory '
  21.  
  22. ----------------------------------------------------------------------------------------------------
  23. -- Delete backup history from msdb database
  24. ----------------------------------------------------------------------------------------------------
  25. -- first do a standard delete
  26. EXEC msdb.dbo.sp_delete_backuphistory @retain_date
  27.  
  28. ----------------------------------------------------------------------------------------------------
  29. -- Delete backup history from SQL Backup Pro GUI activity cache
  30. ----------------------------------------------------------------------------------------------------
  31.  
  32. --get all of the ids that we want to delete
  33. SET @command = 'select id from backuphistory where backup_end < '''
  34. + @retain_date + ''''
  35. TRUNCATE TABLE #DeleteTheseIDs
  36. INSERT INTO #DeleteTheseIDs
  37. EXEC [master]..sqbdata @command
  38. SELECT 'Deleteing # Backup ids ' = COUNT(*)
  39. FROM #DeleteTheseIDs
  40.  
  41. --loop until they're all deleted
  42. WHILE EXISTS ( SELECT 1
  43. FROM #DeleteTheseIDs )
  44. BEGIN
  45.  
  46. --get next set of ids (do not get too many at a time, otherwise the list will be truncated)
  47. SET @IDList = ''
  48. SELECT TOP 150
  49. @IDList = @IDList + CASE WHEN @IDList = '' THEN ''
  50. ELSE ','
  51. END + CONVERT(VARCHAR(10), id)
  52. FROM #DeleteTheseIDs
  53. ORDER BY id
  54.  
  55. --delete history
  56. SET @command = 'delete from backupfiles where backup_id IN ('
  57. + @IDList + ')'
  58. EXEC [master]..sqbdata @command
  59. SET @command = 'delete from backuplog where backup_id IN (' + @IDList
  60. + ')'
  61. EXEC [master]..sqbdata @command
  62. SET @command = 'delete from backuphistory where id IN (' + @IDList
  63. + ')'
  64. EXEC [master]..sqbdata @command
  65.  
  66. --delete from temp file
  67. SET @Command = 'DELETE FROM #DeleteTheseIDs where id IN (' + @IDList
  68. + ')'
  69. EXEC (@Command)
  70. END
  71.  
  72. ----------------------------------------------------------------------------------------------------
  73. -- Delete restore history from SQL Backup Pro GUI activity cache
  74. ----------------------------------------------------------------------------------------------------
  75. --get all of the ids that we want to delete
  76. SET @command = 'select id from restorehistory where restore_end < '''
  77. + @retain_date + ''''
  78. TRUNCATE TABLE #DeleteTheseIDs
  79. INSERT INTO #DeleteTheseIDs
  80. EXEC [master]..sqbdata @command
  81. SELECT 'Deleting # Restore ids ' = COUNT(*)
  82. FROM #DeleteTheseIDs
  83.  
  84. --loop until they're all deleted
  85. WHILE EXISTS ( SELECT 1
  86. FROM #DeleteTheseIDs )
  87. BEGIN
  88.  
  89. -- get next set of ids
  90. SET @IDList = ''
  91. SELECT TOP 200
  92. @IDList = @IDList + CASE WHEN @IDList = '' THEN ''
  93. ELSE ','
  94. END + CONVERT(VARCHAR(10), id)
  95. FROM #DeleteTheseIDs
  96. ORDER BY id
  97.  
  98. --delete history
  99. SET @command = 'delete from restorefiles where restore_id IN ('
  100. + @IDList + ')'
  101. EXEC [master]..sqbdata @command
  102. SET @command = 'delete from restorelog where restore_id IN ('
  103. + @IDList + ')'
  104. EXEC [master]..sqbdata @command
  105. SET @command = 'delete from restorehistory where id IN (' + @IDList
  106. + ')'
  107. EXEC [master]..sqbdata @command
  108.  
  109. --delete from temp file
  110. SET @Command = 'DELETE FROM #DeleteTheseIDs where id IN (' + @IDList
  111. + ')'
  112. EXEC (@Command)
  113. END
  114. SELECT 'Done.'

Didn't find what you were looking for?