{"id":3258,"date":"2012-12-03T20:10:47","date_gmt":"2012-12-03T20:10:47","guid":{"rendered":"http:\/\/craftydba.com\/?p=3258"},"modified":"2024-02-17T17:16:00","modified_gmt":"2024-02-17T17:16:00","slug":"maintenance-history-cleanup-tasks","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=3258","title":{"rendered":"Maintenance &#038; History Cleanup Tasks"},"content":{"rendered":"<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/jigsaw_puzzle_ssis.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/jigsaw_puzzle_ssis.jpg\" alt=\"\" title=\"jigsaw_puzzle_ssis\" width=\"120\" height=\"120\" class=\"alignleft size-full wp-image-3149\" \/><\/a><\/p>\n<p>Today, I will be continuing the series of articles on the control flow tasks available in SQL Server Integration Services (SSIS) to perform Database Administration (DBA) tasks. We will be designing a SSIS package that executes a data retention policy in regards to backup files and [msdb] history records.<\/p>\n<p>One of the most important job functions of a DBA is to have a recent backup chain in which a point in time restore can be performed.  A backup chain consists of the most recent <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186289.aspx\">FULL<\/a> backup, differential (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms175526.aspx\">DIFF<\/a>) backup, and transaction <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms191429.aspx\">LOG<\/a> backup.  <\/p>\n<p>I am going to start up Visual Studio and create a project named <span style=\"color: #cc0000;\">&#8220;cleanup-backup-info&#8221;<\/span> under the &#8220;c:\\ssis-depot\\&#8221; directory.  Upon having a blank package, we will be adding and configuring several objects.  I am going to list each of the objects we need to solve this business solution.<\/p>\n<p><span style=\"color: #cc0000;\">cmMySqlServer<\/span> &#8211; The local database server connection information.<\/p>\n<p><span style=\"color: #cc0000;\">cmMySmtpServer<\/span> &#8211; The local mail server connection information.<\/p>\n<p><span style=\"color: #cc0000;\">tskRemoveOldBakFiles<\/span> &#8211; A maintenance cleanup task for FULL &#038; DIFF backups.<\/p>\n<p><span style=\"color: #cc0000;\">tskRemoveOldTrnFiles<\/span> &#8211; A maintenance cleanup task for LOG backups.<\/p>\n<p><span style=\"color: #cc0000;\">tskCleanupMsdbHistory<\/span> &#8211; A history cleanup task for [msdb] database.<\/p>\n<p><span style=\"color: #cc0000;\">tskEmailSuccess<\/span> &#8211; Mail notice is sent when the package completes successfully.<\/p>\n<p><span style=\"color: #cc0000;\">tskEmailFailure1<\/span> &#8211; Mail notice is sent when package fails to remove old &#8220;bak&#8221; files.<\/p>\n<p><span style=\"color: #cc0000;\">tskEmailFailure2<\/span> &#8211; Mail notice is sent when package fails to remove old &#8220;trn&#8221; files.<\/p>\n<p><span style=\"color: #cc0000;\">tskEmailFailure3<\/span> &#8211; Mail notice is sent when package fails to clean up [msdb] history.<\/p>\n<p>The key items to configure the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms345177(v=sql.110).aspx\">Maintenance Cleanup task<\/a> for removing old &#8220;bak&#8221; files are the location of the root directory, whether to search sub-directories, the file extension to look for, and the number of weeks to retain files.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info1-260x300.jpg\" alt=\"\" title=\"cleanup-backup-info1\" width=\"260\" height=\"300\" class=\"aligncenter size-medium wp-image-3266\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info1-260x300.jpg 260w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info1.jpg 568w\" sizes=\"auto, (max-width: 260px) 100vw, 260px\" \/><\/a><\/p>\n<p>Again, we need to configure the Maintenance Cleanup task to apply a retention policy for the &#8220;trn&#8221; files.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info2-259x300.jpg\" alt=\"\" title=\"cleanup-backup-info2\" width=\"259\" height=\"300\" class=\"aligncenter size-medium wp-image-3267\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info2-259x300.jpg 259w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info2.jpg 566w\" sizes=\"auto, (max-width: 259px) 100vw, 259px\" \/><\/a><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms139794.aspx\">History Cleanup task <\/a>is even easier to configure.  Select the SQL Server connection, all historical data items you want to cull, and the number of weeks to retain entries.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info3.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info3-300x197.jpg\" alt=\"\" title=\"cleanup-backup-info3\" width=\"300\" height=\"197\" class=\"aligncenter size-medium wp-image-3269\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info3-300x197.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info3.jpg 508w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Make sure the package looks like mine with the precedence constraints correctly configured.  Now, lets run the package to completion.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info41.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info41-300x207.jpg\" alt=\"\" title=\"cleanup-backup-info4\" width=\"300\" height=\"207\" class=\"aligncenter size-medium wp-image-3273\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info41-300x207.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info41-1024x707.jpg 1024w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info41.jpg 1240w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>I just noticed a success email that was sent to my email account and it appear in MS Outlook application.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info5.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info5-300x140.jpg\" alt=\"\" title=\"cleanup-backup-info5\" width=\"300\" height=\"140\" class=\"aligncenter size-medium wp-image-3275\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info5-300x140.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info5.jpg 970w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>I wonder what the SSIS package is actually doing to perform the backup?  To discover what T-SQL is being sent to the server, we can open a client\/server side trace using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms181091.aspx\">SQL Server Profiler<\/a>.  I am going to skip over the mechanics of starting a profiler trace and running the SSIS package in debug mode.<\/p>\n<p>The whole trace file (<span style=\"color: #cc0000;\">&#8220;cleanup-backup-info.trc&#8221;<\/span>) along with the SSIS package (<span style=\"color: #cc0000;\">&#8220;cleanup-backup-info.dtsx&#8221;<\/span>) are included as a download bundle at the end of the article.<\/p>\n<p>Basically, the TSQL commands can be broken down into three parts:  remove old backup files, remove old transaction files, and cleanup [msdb] history.<\/p>\n<p>I cleaned up the code that was sent by the SSIS package to the database.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"history clean up task - sql trace commands\">\r\n--\r\n-- TASK #1\r\n-- \r\n\r\nEXEC master.dbo.xp_delete_file 0,N'C:\\mssql\\backup',N'bak',N'2012-11-03T09:15:56',1\r\n\r\n--\r\n-- TASK #2\r\n-- \r\n\r\nEXEC master.dbo.xp_delete_file 0,N'C:\\mssql\\backup',N'trn',N'2012-11-03T09:15:56',1\r\n\r\n--\r\n-- TASK #3\r\n-- \r\n\r\nDECLARE @dt datetime select @dt = cast(N'2012-11-03T09:15:56' as datetime) \r\nEXEC msdb.dbo.sp_delete_backuphistory @dt\r\nEXEC msdb.dbo.sp_purge_jobhistory  @oldest_date='2012-11-03T09:15:56'\r\nEXEC msdb..sp_maintplan_delete_log null,null,'2012-11-03T09:15:56'\r\n<\/pre>\n<p>In summary, the Maintenance Cleanup task can be used to apply a data retention policy to the backup files that are physically on disk.    The History Cleanup task can be used to apply a record retention policy to backup\/restore jobs, job history, and maintenance plan history inside the [msdb] database.<\/p>\n<p>Like any ETL package, this one can be made more re-usable by parameterizing the arguments needed by each control flow object.  In my next article, we will exploring how to use the Check Database Integrity task to validate our databases.<\/p>\n<p><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2012\/12\/cleanup-backup-info-pkg.zip'>cleanup backup info bundle<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I will be continuing the series of articles on the control flow tasks available in SQL Server Integration Services (SSIS) to perform Database Administration (DBA) tasks. We will be designing a SSIS package that executes a data retention policy in regards to backup files and [msdb] history records. One of the most important job functions of a DBA is to have a recent backup chain in which a point in time restore can be performed. A backup chain consists of the most recent FULL backup, differential (DIFF) backup, and&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[12,649,15,648,638,652,654,653,28,642,636,650,651],"class_list":["post-3258","post","type-post","status-publish","format-standard","hentry","category-other","tag-free-code","tag-history-cleanup-task","tag-john-f-miner-iii","tag-maintenance-cleanup-task","tag-send-mail-task","tag-sp_delete_backuphistory","tag-sp_maintplan_delete_log","tag-sp_purge_jobhistory","tag-sql-server","tag-sql-server-profiler","tag-ssis-developer","tag-trace-file--tsql","tag-xp_delete_file"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/3258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3258"}],"version-history":[{"count":1,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/3258\/revisions"}],"predecessor-version":[{"id":8951,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/3258\/revisions\/8951"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}