{"id":1152,"date":"2007-10-12T17:09:43","date_gmt":"2007-10-12T17:09:43","guid":{"rendered":"\/blogs\/paul\/post\/BACKUP-LOG-WITH-NO_LOG-use-abuse-and-undocumented-trace-flags-to-stop-it.aspx"},"modified":"2017-08-04T12:10:26","modified_gmt":"2017-08-04T19:10:26","slug":"backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/","title":{"rendered":"BACKUP LOG WITH NO_LOG &#8211; use, abuse, and undocumented trace flags to stop it"},"content":{"rendered":"<p style=\"text-align: justify;\">This has come up several times over the last few days, and is something that Kimberly and I discuss (and deplore) when we lecture &#8211; the use of <em>BACKUP LOG WITH NO_LOG<\/em> (or \u00a0<em>TRUNCATE_ONLY<\/em> &#8211; they&#8217;re synonymous) to allow log truncation.<\/p>\n<p style=\"text-align: justify;\"><strong>How is it used?<\/strong><\/p>\n<p style=\"text-align: justify;\">The common use is when the transaction log grows to be inordinately large on a database that&#8217;s in FULL (or BULK_LOGGED) recovery mode. This happens after a database backup has been taken, which switches the log into a mode where it won&#8217;t truncate until its been backed up.\u00a0In these circumstances, if you don&#8217;t take a transaction log backup, the log will continue to grow until it runs out of disk space.<\/p>\n<p style=\"text-align: justify;\">The alternative to taking a real log backup\u00a0is to issue a <em>BACKUP LOG dbname WITH NO_LOG<\/em> command. Let&#8217;s see this in action &#8211; <strong>note<\/strong> that I&#8217;m not advocating its use but I want to show you what it does. First off I&#8217;ll create some transaction log after taking a full database backup:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nUSE &#x5B;nologtest];\r\nGO\r\n\r\nBACKUP DATABASE &#x5B;nologtest] TO DISK = N'C:\\sqlskills\\nologtest.bck' WITH INIT;\r\nGO\r\n\r\nCREATE TABLE &#x5B;t1] (&#x5B;c1] INT IDENTITY, &#x5B;c2] CHAR (8000) DEFAULT 'a')\r\nGO\r\n\r\nSET NOCOUNT ON\r\nGO\r\n\r\nINSERT INTO &#x5B;t1] DEFAULT VALUES;\r\nGO 10000\r\n<\/pre>\n<p>How large is the transaction log now?<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT &#x5B;name], &#x5B;size] FROM sys.database_files;\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nname\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 size\r\n--------------- --------\r\nnologtest\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 90264\r\nnologtest_log\u00a0\u00a0 104128\r\n<\/pre>\n<p style=\"text-align: justify;\">A little bit larger than the data file (which is what I&#8217;d expect after the operation I just performed) and they&#8217;re both around 100MB. I&#8217;ll pretend that I haven&#8217;t been paying attention to the size of the database and log and now I don&#8217;t have any space to perform a backup. Can I just shrink the log?<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nDBCC SHRINKFILE (nologtest_log, 2);\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nCannot shrink log file 2 (nologtest_log) because all logical log files are in use.\r\nDbId\u00a0\u00a0 FileId\u00a0\u00a0\u00a0\u00a0\u00a0 CurrentSize MinimumSize UsedPages\u00a0\u00a0 EstimatedPages\r\n------ ----------- ----------- ----------- ----------- --------------\r\n11\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 102944\u00a0\u00a0\u00a0\u00a0\u00a0 63\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 102944\u00a0\u00a0\u00a0\u00a0\u00a0 56\r\n<\/pre>\n<p>No. What&#8217;s stopping me (well <em>DBCC SHRINKFILE<\/em> just told me, but let&#8217;s double-check)?<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT log_reuse_wait_desc FROM sys.databases WHERE NAME = 'nologtest';\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nLOG_BACKUP\r\n<\/pre>\n<p>Ok &#8211; so I can&#8217;t perform a backup so I&#8217;ll use <em>BACKUP LOG WITH NO_LOG<\/em>:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nBACKUP LOG &#x5B;nologtest] WITH NO_LOG;\r\nGO\r\n\r\nSELECT &#x5B;log_reuse_wait_desc] FROM sys.databases WHERE &#x5B;name] = N'nologtest';\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nNOTHING\r\n<\/pre>\n<p>Now it looks like I can do the shrink:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nDBCC SHRINKFILE (nologtest_log, 2);\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nDbId\u00a0\u00a0 FileId\u00a0\u00a0\u00a0\u00a0\u00a0 CurrentSize MinimumSize UsedPages\u00a0\u00a0 EstimatedPages\r\n------ ----------- ----------- ----------- ----------- --------------\r\n11\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 256\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 63\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 256\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 56\r\n<\/pre>\n<p>Success!<\/p>\n<p>Hold on, are you sure?<\/p>\n<p><strong>Why is it bad?<\/strong><\/p>\n<p style=\"text-align: justify;\">Do you realize what just happened? We <em><u>discarded<\/u><\/em> the contents of the transaction log since the last full\/differential\/log backup. That means that until the next full or differential backup <em>completes<\/em>, all work since the last backup completed will be lost if the database is destroyed. So if your business relies on not losing data\/work in the event of a disaster &#8211; then what I just did is <em>sacrilege<\/em>!<\/p>\n<p style=\"text-align: justify;\">The <em>whole point<\/em> of FULL (and BULK_LOGGED) recovery modes\u00a0is to preserve the transaction log so that the database can be recovered right up to the point of a failure, or to do point-in-time restores. Using <em>BACKUP LOG WITH NO_LOG<\/em> negates all of that. (For completeness, note that you can&#8217;t do a point-in-time restore to any time\u00a0in a transaction log backup containing a\u00a0bulk-logged transaction).<\/p>\n<p style=\"text-align: justify;\"><strong>What are the alternatives?<\/strong><\/p>\n<p style=\"text-align: justify;\">If you don&#8217;t want the FULL recovery mode behavior, then don&#8217;t use FULL recovery mode &#8211; switch to SIMPLE mode. But be aware that you lose the ability to restore to any point in between your full or differential backups.<\/p>\n<p style=\"text-align: justify;\">If you want the FULL recovery mode behavior, but don&#8217;t want to run out of log space &#8211; then the answer is again simple (pun intended :-), take log backups! If your log is growing too quickly, take more frequent log backups! If you don&#8217;t have enough disk space to store the log backups, talk to your management about the options &#8211; buying more disks and continuing to use the FULL recovery options, or being forced to move to SIMPLE recovery mode.<\/p>\n<p style=\"text-align: justify;\">One thing to bear in mind &#8211; you actually have to monitor the size of your log to tell whether its growing. That&#8217;s what gets people into trouble in the first place &#8211; a combination of:<\/p>\n<ul style=\"text-align: justify;\">\n<li>Not knowing that the database is in FULL recovery mode<\/li>\n<li>Not tracking the size of the log<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">The problem comes for those people who just use an app and have no idea that the developer put the database (maybe for a web application log) into FULL recovery mode, or even that they have SQL Server on their system. For these people there is no good answer unfortunately apart from educating developers.<\/p>\n<p style=\"text-align: justify;\"><strong>How to prevent it being used<\/strong><\/p>\n<p style=\"text-align: justify;\">If you&#8217;re a sysadmin who wants to stop your database owners and backup operators from using the <em>NO_LOG<\/em> option, there is way to do it. Trace flag 3231 in SQL Server 2000 and SQL Server 2005 will turn the <em>NO_LOG<\/em> and <em>TRUNCATE_ONLY<\/em> options into no-ops in FULL\/BULK_LOGGED recovery mode, and will clear the log in SIMPLE recovery mode. Trace flag 3031 in SQL Server 2005 turns them in checkpoints in all recovery modes. These trace flags are undocumented but they&#8217;re allowed to be publicized.<\/p>\n<p style=\"text-align: justify;\">This way no-one can truncate the log unless you allow them to, and when they come to you to complain, you can educate them!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This has come up several times over the last few days, and is something that Kimberly and I discuss (and deplore) when we lecture &#8211; the use of BACKUP LOG WITH NO_LOG (or \u00a0TRUNCATE_ONLY &#8211; they&#8217;re synonymous) to allow log truncation. How is it used? The common use is when the transaction log grows to [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,14,31,35,38,96,98],"tags":[],"class_list":["post-1152","post","type-post","status-publish","format-standard","hentry","category-backuprestore","category-bad-advice","category-database-maintenance","category-disaster-recovery","category-example-scripts","category-trace-flags","category-transaction-log"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>BACKUP LOG WITH NO_LOG - use, abuse, and undocumented trace flags to stop it - Paul S. Randal<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"BACKUP LOG WITH NO_LOG - use, abuse, and undocumented trace flags to stop it - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"This has come up several times over the last few days, and is something that Kimberly and I discuss (and deplore) when we lecture &#8211; the use of BACKUP LOG WITH NO_LOG (or \u00a0TRUNCATE_ONLY &#8211; they&#8217;re synonymous) to allow log truncation. How is it used? The common use is when the transaction log grows to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2007-10-12T17:09:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-08-04T19:10:26+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/\",\"name\":\"BACKUP LOG WITH NO_LOG - use, abuse, and undocumented trace flags to stop it - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2007-10-12T17:09:43+00:00\",\"dateModified\":\"2017-08-04T19:10:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"BACKUP LOG WITH NO_LOG &#8211; use, abuse, and undocumented trace flags to stop it\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"BACKUP LOG WITH NO_LOG - use, abuse, and undocumented trace flags to stop it - Paul S. Randal","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/","og_locale":"en_US","og_type":"article","og_title":"BACKUP LOG WITH NO_LOG - use, abuse, and undocumented trace flags to stop it - Paul S. Randal","og_description":"This has come up several times over the last few days, and is something that Kimberly and I discuss (and deplore) when we lecture &#8211; the use of BACKUP LOG WITH NO_LOG (or \u00a0TRUNCATE_ONLY &#8211; they&#8217;re synonymous) to allow log truncation. How is it used? The common use is when the transaction log grows to [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/","og_site_name":"Paul S. Randal","article_published_time":"2007-10-12T17:09:43+00:00","article_modified_time":"2017-08-04T19:10:26+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/","name":"BACKUP LOG WITH NO_LOG - use, abuse, and undocumented trace flags to stop it - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2007-10-12T17:09:43+00:00","dateModified":"2017-08-04T19:10:26+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/backup-log-with-no_log-use-abuse-and-undocumented-trace-flags-to-stop-it\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"BACKUP LOG WITH NO_LOG &#8211; use, abuse, and undocumented trace flags to stop it"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/1152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=1152"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/1152\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=1152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=1152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=1152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}