{"id":4651,"date":"2016-10-20T17:24:21","date_gmt":"2016-10-21T00:24:21","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/paul\/?p=4651"},"modified":"2016-10-21T10:35:39","modified_gmt":"2016-10-21T17:35:39","slug":"getting-a-history-of-database-snapshot-creation","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/","title":{"rendered":"Getting a history of database snapshot creation"},"content":{"rendered":"<p>Earlier today someone asked on the #sqlhelp Twitter alias if there is a history of database snapshot creation anywhere, apart from scouring the error logs.<\/p>\n<p>There isn&#8217;t, unfortunately, but you can dig around the transaction log of the <em>master<\/em> database to find some information.<\/p>\n<p>When a database snapshot is created, a bunch of entries are made in the system tables in\u00a0<em>master<\/em> and they are all logged, under a transaction named\u00a0<em>DBMgr::CreateSnapshotDatabase<\/em>. So that&#8217;s where we can begin looking.<\/p>\n<p>Here&#8217;s a simple example of a database snapshot:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nUSE &#x5B;master];\r\nGO\r\n\r\nIF DATABASEPROPERTYEX (N'Company_Snapshot', N'Version') &gt; 0\r\nBEGIN\r\n    DROP DATABASE &#x5B;Company_Snapshot];\r\nEND\r\nGO\r\nIF DATABASEPROPERTYEX (N'Company', N'Version') &gt; 0\r\nBEGIN\r\n    ALTER DATABASE &#x5B;Company] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;\r\n    DROP DATABASE &#x5B;Company];\r\nEND\r\nGO\r\n\r\n-- Create a database\r\nCREATE DATABASE &#x5B;Company];\r\nGO\r\n\r\n-- Create the snapshot\r\nCREATE DATABASE &#x5B;Company_Snapshot]\r\nON (NAME = N'Company', FILENAME = N'C:\\SQLskills\\CompanyData.mdfss')\r\nAS SNAPSHOT OF &#x5B;Company];\r\nGO\r\n<\/pre>\n<p>And I can find the transaction using the following code, plus who did it and when:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nUSE &#x5B;master];\r\nGO\r\n\r\nSELECT\r\n    &#x5B;Transaction ID],\r\n    SUSER_SNAME (&#x5B;Transaction SID]) AS &#x5B;User],\r\n    &#x5B;Begin Time]\r\nFROM fn_dblog (NULL, NULL)\r\nWHERE &#x5B;Operation] = N'LOP_BEGIN_XACT'\r\n    AND &#x5B;Transaction Name] = N'DBMgr::CreateSnapshotDatabase';\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nTransaction ID User             Begin Time\r\n-------------- ---------------- ------------------------\r\n0000:00099511  APPLECROSS\\Paul  2016\/10\/20 13:07:53:143\r\n<\/pre>\n<p>Now to get some useful information, I can crack open one of the system table inserts, specifically the insert into one of the nonclustered indexes of the <em>sys.sysdbreg<\/em> table:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT\r\n    &#x5B;RowLog Contents 0]\r\nFROM fn_dblog (NULL, NULL)\r\nWHERE &#x5B;Transaction ID] = N'0000:00099511'\r\n    AND &#x5B;Operation] = N'LOP_INSERT_ROWS'\r\n    AND &#x5B;AllocUnitName] = N'sys.sysdbreg.nc1';\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nRowLog Contents 0\r\n-------------------------------------------------------------------------------------\r\n0x26230000000100290043006F006D00700061006E0079005F0053006E0061007000730068006F007400\r\n<\/pre>\n<p>Bytes 2 through 5 (considering the first byte as byte 1) are the byte-reversed database ID of the snapshot database, and bytes 10 through the end of the data are the <em>sysname\u00a0<\/em>name of the database. Similarly, grabbing the insert log record for\u00a0the nonclustered index of the\u00a0<em>sys.syssingleobjrefs<\/em> table allows\u00a0us to get the source database ID.<\/p>\n<p>Here&#8217;s the finished code:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT * FROM\r\n(\r\nSELECT\r\n    SUSER_SNAME (&#x5B;Transaction SID]) AS &#x5B;User],\r\n    &#x5B;Begin Time]\r\nFROM fn_dblog (NULL, NULL)\r\nWHERE &#x5B;Transaction ID] = N'0000:00099511'\r\n    AND &#x5B;Operation] = N'LOP_BEGIN_XACT'\r\n) AS &#x5B;who],\r\n(\r\nSELECT\r\n    CONVERT (INT,\r\n        SUBSTRING (&#x5B;RowLog Contents 0], 5, 1) +\r\n        SUBSTRING (&#x5B;RowLog Contents 0], 4, 1) +\r\n        SUBSTRING (&#x5B;RowLog Contents 0], 3, 1) +\r\n        SUBSTRING (&#x5B;RowLog Contents 0], 2, 1)) AS &#x5B;Snapshot DB ID],\r\n    CONVERT (SYSNAME, SUBSTRING (&#x5B;RowLog Contents 0], 10, 256)) AS &#x5B;Snapshot DB Name]\r\nFROM fn_dblog (NULL, NULL)\r\nWHERE &#x5B;Transaction ID] = N'0000:00099511'\r\n\tAND &#x5B;Operation] = N'LOP_INSERT_ROWS'\r\n\tAND &#x5B;AllocUnitName] = N'sys.sysdbreg.nc1'\r\n) AS &#x5B;snap],\r\n(\r\nSELECT\r\n    CONVERT (INT,\r\n        SUBSTRING (&#x5B;RowLog Contents 0], 5, 1) +\r\n        SUBSTRING (&#x5B;RowLog Contents 0], 4, 1) +\r\n        SUBSTRING (&#x5B;RowLog Contents 0], 3, 1) +\r\n        SUBSTRING (&#x5B;RowLog Contents 0], 2, 1)) AS &#x5B;Source DB ID]\r\nFROM fn_dblog (NULL, NULL)\r\nWHERE &#x5B;Transaction ID] = N'0000:00099511'\r\n\tAND &#x5B;Operation] = N'LOP_INSERT_ROWS'\r\n\tAND &#x5B;AllocUnitName] = N'sys.syssingleobjrefs.nc1'\r\n) AS &#x5B;src];\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nUser             Begin Time               Snapshot DB ID Snapshot DB Name  Source DB ID\r\n---------------- ------------------------ -------------- ----------------- ------------\r\nAPPLECROSS\\Paul  2016\/10\/20 13:07:53:143  35             Company_Snapshot  22\r\n<\/pre>\n<p>I&#8217;ll leave it as an exercise for the reader to wrap a cursor around the code to operate on all such transactions, and you can also look in the <em>master<\/em> log backups using the <em>fn_dump_dblog<\/em> function (see <a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/using-fn_dblog-fn_dump_dblog-and-restoring-with-stopbeforemark-to-an-lsn\/\" target=\"_blank\">here<\/a> for some examples).<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier today someone asked on the #sqlhelp Twitter alias if there is a history of database snapshot creation anywhere, apart from scouring the error logs. There isn&#8217;t, unfortunately, but you can dig around the transaction log of the master database to find some information. When a database snapshot is created, a bunch of entries are [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,98],"tags":[],"class_list":["post-4651","post","type-post","status-publish","format-standard","hentry","category-database-snapshots","category-transaction-log"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Getting a history of database snapshot creation - 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\/getting-a-history-of-database-snapshot-creation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Getting a history of database snapshot creation - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"Earlier today someone asked on the #sqlhelp Twitter alias if there is a history of database snapshot creation anywhere, apart from scouring the error logs. There isn&#8217;t, unfortunately, but you can dig around the transaction log of the master database to find some information. When a database snapshot is created, a bunch of entries are [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2016-10-21T00:24:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-10-21T17:35:39+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=\"3 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\/getting-a-history-of-database-snapshot-creation\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/\",\"name\":\"Getting a history of database snapshot creation - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2016-10-21T00:24:21+00:00\",\"dateModified\":\"2016-10-21T17:35:39+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Getting a history of database snapshot creation\"}]},{\"@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":"Getting a history of database snapshot creation - 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\/getting-a-history-of-database-snapshot-creation\/","og_locale":"en_US","og_type":"article","og_title":"Getting a history of database snapshot creation - Paul S. Randal","og_description":"Earlier today someone asked on the #sqlhelp Twitter alias if there is a history of database snapshot creation anywhere, apart from scouring the error logs. There isn&#8217;t, unfortunately, but you can dig around the transaction log of the master database to find some information. When a database snapshot is created, a bunch of entries are [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/","og_site_name":"Paul S. Randal","article_published_time":"2016-10-21T00:24:21+00:00","article_modified_time":"2016-10-21T17:35:39+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/","name":"Getting a history of database snapshot creation - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2016-10-21T00:24:21+00:00","dateModified":"2016-10-21T17:35:39+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/getting-a-history-of-database-snapshot-creation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Getting a history of database snapshot creation"}]},{"@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\/4651","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=4651"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/4651\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=4651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=4651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=4651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}