SQL Backup 9

Help for older versions available.

These pages cover SQL Backup 9, which is not the latest version. Help for other versions is also 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() -- 15
  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. SET @command = 'delete from backupfiles_copylist_log where copylist_id IN (select id from backupfiles_copylist where backup_id IN (' + @IDList
  67. + '))'
  68. EXEC [master]..sqbdata @command
  69.  
  70. SET @command = 'delete from backupfiles_copylist where backup_id IN (' + @IDList
  71. + ')'
  72. EXEC [master]..sqbdata @command
  73.  
  74. --delete from temp file
  75. SET @Command = 'DELETE FROM #DeleteTheseIDs where id IN (' + @IDList
  76. + ')'
  77. EXEC (@Command)
  78. END
  79.  
  80. ----------------------------------------------------------------------------------------------------
  81. -- Delete restore history from SQL Backup Pro GUI activity cache
  82. ----------------------------------------------------------------------------------------------------
  83. --get all of the ids that we want to delete
  84. SET @command = 'select id from restorehistory where restore_end < '''
  85. + @retain_date + ''''
  86. TRUNCATE TABLE #DeleteTheseIDs
  87. INSERT INTO #DeleteTheseIDs
  88. EXEC [master]..sqbdata @command
  89. SELECT 'Deleting # Restore ids ' = COUNT(*)
  90. FROM #DeleteTheseIDs
  91.  
  92. --loop until they're all deleted
  93. WHILE EXISTS ( SELECT 1
  94. FROM #DeleteTheseIDs )
  95. BEGIN
  96.  
  97. -- get next set of ids
  98. SET @IDList = ''
  99. SELECT TOP 200
  100. @IDList = @IDList + CASE WHEN @IDList = '' THEN ''
  101. ELSE ','
  102. END + CONVERT(VARCHAR(10), id)
  103. FROM #DeleteTheseIDs
  104. ORDER BY id
  105.  
  106. --delete history
  107. SET @command = 'delete from restorefiles where restore_id IN ('
  108. + @IDList + ')'
  109. EXEC [master]..sqbdata @command
  110. SET @command = 'delete from restorelog where restore_id IN ('
  111. + @IDList + ')'
  112. EXEC [master]..sqbdata @command
  113. SET @command = 'delete from restorehistory where id IN (' + @IDList
  114. + ')'
  115. EXEC [master]..sqbdata @command
  116.  
  117. --delete from temp file
  118. SET @Command = 'DELETE FROM #DeleteTheseIDs where id IN (' + @IDList
  119. + ')'
  120. EXEC (@Command)
  121. END
  122. SELECT 'Done.'

Didn't find what you were looking for?