SQL Backup 10

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 SQLite/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. 

If you are using SQL Backup 10.1.10 or later versions, use the following script to delete backup and restore history older than the specified number of days from the msdb database, the SQLite database and the SQL Backup Pro GUI activity cache:

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

If you are using SQL Backup 10.1.9 or earlier versions, use the following script to delete 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?