{"id":233,"date":"2018-06-23T19:01:47","date_gmt":"2018-06-23T19:01:47","guid":{"rendered":"https:\/\/projects-42.nl\/?p=233"},"modified":"2018-06-24T14:30:27","modified_gmt":"2018-06-24T14:30:27","slug":"mssql-keep-your-transaction-logs-from-exploding","status":"publish","type":"post","link":"https:\/\/projects-42.nl\/index.php\/2018\/mssql-keep-your-transaction-logs-from-exploding\/","title":{"rendered":"MSSQL: keep your transaction log&#8217;s from exploding"},"content":{"rendered":"<p>I recently discovered that my employers database server was running out of storage space on the database volume. After some searching I traced the problem to our Xendesktop site transaction log file witch had grown almost 80GB in 3 months.<\/p>\n<p>I never noticed problems with the transaction logs before but previously the SQL server was a physics machine with around 1 TB storage for the databases while the new database server is a VM with only 350GB available for the databases.<\/p>\n<p>If you work with Microsoft SQL server from time to time, you might know that a database usually consists of two files. The database file &#8220;&lt;dbname&gt;.mdf&#8221; and the transaction log file &#8220;&lt;dbname&gt;.ldf&#8221;. The database file stores the current version of the database while the transaction log file saves all changes to the database. It is important to have a transaction log, because it enables you to rollback the database to a point in time, or recover the database after a software crash or an unexpected power failure.<\/p>\n<p>There are 3 modes in with the transaction log can operate: Full, Bulk-logged and Simple. I wont be going into detail about the different recovery models, <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/backup-restore\/recovery-models-sql-server?view=sql-server-2017\">Microft has a good article<\/a> about this subject.<\/p>\n<p>In short, Full saves all transactions indefinitely while Simple removes them after committing them to the database. Bulk-logged is almost the same as Full but it has some enhancements for logging bulk transactions.<\/p>\n<p>Our Citrix database was configured to use the Full recovery model, this is the default setting.<\/p>\n<p>To solve this issue while retaining the ability to recover data in the event of a crash we will have to set up transaction log back-ups. If configured right this can be an automated process.<\/p>\n<p>Follow the following steps to back-up a transaction log:<\/p>\n<figure id=\"attachment_242\" aria-describedby=\"caption-attachment-242\" style=\"width: 622px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-242 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog01.png\" alt=\"\" width=\"622\" height=\"476\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog01.png 622w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog01-300x230.png 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><figcaption id=\"caption-attachment-242\" class=\"wp-caption-text\">From the SQL Server Management Studio, click right on the DB of witch you want to backup the transaction log. Navigate to <strong>Tasks<\/strong> and click &#8220;<strong>Back Up&#8230;<\/strong>&#8220;.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_243\" aria-describedby=\"caption-attachment-243\" style=\"width: 926px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-243 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog02.png\" alt=\"\" width=\"926\" height=\"674\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog02.png 926w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog02-300x218.png 300w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog02-768x559.png 768w\" sizes=\"auto, (max-width: 926px) 100vw, 926px\" \/><figcaption id=\"caption-attachment-243\" class=\"wp-caption-text\">In the Back Up Database dialog on the General tab, change the backup type to &#8220;Transaction log&#8221; and specify a destination for your backup.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_239\" aria-describedby=\"caption-attachment-239\" style=\"width: 925px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-239 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog03.png\" alt=\"\" width=\"925\" height=\"675\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog03.png 925w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog03-300x219.png 300w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog03-768x560.png 768w\" sizes=\"auto, (max-width: 925px) 100vw, 925px\" \/><figcaption id=\"caption-attachment-239\" class=\"wp-caption-text\">In the Back Up Database dialog on the Media Options tab, check the &#8220;Verify backup when finished&#8221; checkbox if you want SQL server to verify the backup after it finishes.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_240\" aria-describedby=\"caption-attachment-240\" style=\"width: 925px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-240 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog04.png\" alt=\"\" width=\"925\" height=\"674\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog04.png 925w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog04-300x219.png 300w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog04-768x560.png 768w\" sizes=\"auto, (max-width: 925px) 100vw, 925px\" \/><figcaption id=\"caption-attachment-240\" class=\"wp-caption-text\">In the Back Up Database dialog on the Backup Options tab, Set backup compression to &#8220;Compress backup&#8221; (unless you just want to move your transaction log in witch case you can live the setting in the default).<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>At this point you can simply click the OK button and the backup will start. In this case it will be a one time only backup. We ICT people like to automate things so we wont have to repeat actions over and over again. F<span class=\"gt-baf-word-clickable\">ortunately, we can turn this process in a automated task.<\/span><\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_241\" aria-describedby=\"caption-attachment-241\" style=\"width: 922px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-241 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog05.png\" alt=\"\" width=\"922\" height=\"198\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog05.png 922w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog05-300x64.png 300w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog05-768x165.png 768w\" sizes=\"auto, (max-width: 922px) 100vw, 922px\" \/><figcaption id=\"caption-attachment-241\" class=\"wp-caption-text\">to turn the above process in an automated task, click the downward arrow next to the script button at the top of the dialog and select &#8220;Script Action to Job&#8221; (or press &#8220;Ctrl+Shift+M&#8221;).<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Unfortunately SQL server Express edition wont allow automated jobs so for the rest of the process i have used a much older SQL Server Standard Instance<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_244\" aria-describedby=\"caption-attachment-244\" style=\"width: 699px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-244 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog06.png\" alt=\"\" width=\"699\" height=\"632\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog06.png 699w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog06-300x271.png 300w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><figcaption id=\"caption-attachment-244\" class=\"wp-caption-text\">Clicking on the &#8220;create job&#8221; button will open a new dialog named &#8220;New Job&#8221;. Most of the information on this page and the script itself are already added to the new job. Navigate to the &#8220;Schedules&#8221; tab to schedule the new job.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_246\" aria-describedby=\"caption-attachment-246\" style=\"width: 760px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-246 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog08.png\" alt=\"\" width=\"760\" height=\"656\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog08.png 760w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog08-300x259.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><figcaption id=\"caption-attachment-246\" class=\"wp-caption-text\">At the schedule tab we can create new schedules. in this case we want to backup the transaction log every Sunday at 1 AM. You can set multiple schedules for each job.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_247\" aria-describedby=\"caption-attachment-247\" style=\"width: 706px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-247 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog09.png\" alt=\"\" width=\"706\" height=\"631\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog09.png 706w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog09-300x268.png 300w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><figcaption id=\"caption-attachment-247\" class=\"wp-caption-text\">On the notifications tab we can set what type of notifications we want to receive from this Job. You can configure this to your liking. Click OK to Save the Job.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>If everything went as planed, we now have an automated transaction log backup Job. Unfortunately the transaction log remains the same size as it was before. to release the backed up space we need to shrink the Transaction log.<\/p>\n<p><strong>WE DON&#8217;T WANT TO SHRINK THE TRANSACTION LOG TO OFTEN, GROWING THE LOG IS A SLOW PROCESS!!!<\/strong><\/p>\n<p>If your transaction log is <span class=\"gt-baf-word-clickable\">huge and you want to re<span id=\"result_box\" class=\"short_text\" lang=\"en\"><span class=\"\">claim <\/span><\/span> some of the space, continue following these steps:<\/span><\/p>\n<p>to be able to shrink the transaction log file, we first need to switch the database to simple recovery mode.<\/p>\n<p>click right on the database you want to shrink and click on <strong>&#8220;Properties&#8221;<\/strong>.<\/p>\n<figure id=\"attachment_250\" aria-describedby=\"caption-attachment-250\" style=\"width: 703px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-250 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog10.png\" alt=\"\" width=\"703\" height=\"630\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog10.png 703w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog10-300x269.png 300w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><figcaption id=\"caption-attachment-250\" class=\"wp-caption-text\">In the property&#8217;s dialog, select the options tab and select &#8220;Simple&#8221; from the recovery model drop down list. Click <strong>&#8220;OK&#8221;<\/strong> to apply the change.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_251\" aria-describedby=\"caption-attachment-251\" style=\"width: 745px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-251 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog11.png\" alt=\"\" width=\"745\" height=\"318\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog11.png 745w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog11-300x128.png 300w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><figcaption id=\"caption-attachment-251\" class=\"wp-caption-text\">Right click the database you want to shrink, navigate to Tasks -&gt; Shrink and click <strong>&#8220;Files&#8221;<\/strong>.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_252\" aria-describedby=\"caption-attachment-252\" style=\"width: 704px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-252 size-full\" src=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog12.png\" alt=\"\" width=\"704\" height=\"633\" srcset=\"https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog12.png 704w, https:\/\/projects-42.nl\/wp-content\/uploads\/2018\/06\/MSSQLTransactionLog12-300x270.png 300w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><figcaption id=\"caption-attachment-252\" class=\"wp-caption-text\">In the shrink File dialog, change the file type to <strong>&#8220;Log&#8221;<\/strong> and make sure the shrink action is set to &#8220;Release unused space&#8221;. Click <strong>&#8220;OK&#8221;<\/strong> tot start the shrink action.<\/figcaption><\/figure>\n<p>Don&#8217;t forget to change the recovery model back to full, if it was set to full to begin with \ud83d\ude09<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently discovered that my employers database server was running out of storage space on the database volume. After some searching I traced the problem to our Xendesktop site transaction log file witch had grown almost 80GB in 3 months. I never noticed problems with the transaction logs before but previously the SQL server was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":235,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[52,53,51,54],"class_list":["post-233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general-ict","tag-mssql","tag-sql","tag-sql-server","tag-transaction-log"],"_links":{"self":[{"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/comments?post=233"}],"version-history":[{"count":5,"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":254,"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/posts\/233\/revisions\/254"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/media\/235"}],"wp:attachment":[{"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/projects-42.nl\/index.php\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}