{"id":133,"date":"2016-03-23T12:39:39","date_gmt":"2016-03-23T04:39:39","guid":{"rendered":"https:\/\/eligiblestore.com\/blog\/?p=133"},"modified":"2020-06-30T21:49:31","modified_gmt":"2020-06-30T13:49:31","slug":"ms-sql-how-to-find-the-latest-restore-info","status":"publish","type":"post","link":"https:\/\/eligiblestore.com\/blog\/2016\/03\/23\/ms-sql-how-to-find-the-latest-restore-info\/","title":{"rendered":"MS SQL How to find the latest restore info"},"content":{"rendered":"<p>So in my work I do automatically downloading and restore a database, however sometimes the automation doesn&#8217;t work properly perhaps due internet connection problem or space full, etc. In order to check that restore has been preformed successfully I just need to check if today is the last restore date.<\/p>\n<p>So here&#8217;s my query, using below query you can get information of when, duration of restore and what backup filename used to restore the database. Please note on line 10 you are supposed to put your database name.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT TOP 1 &#x5B;rs].&#x5B;destination_database_name] ,\r\n&#x5B;rs].&#x5B;restore_date] ,\r\n&#x5B;bs].&#x5B;backup_start_date] ,\r\n&#x5B;bs].&#x5B;backup_finish_date] ,\r\n&#x5B;bs].&#x5B;database_name] AS &#x5B;source_database_name] ,\r\n&#x5B;bmf].&#x5B;physical_device_name] AS &#x5B;backup_file_used_for_restore]\r\nFROM msdb..restorehistory rs\r\nINNER JOIN msdb..backupset bs ON &#x5B;rs].&#x5B;backup_set_id] = &#x5B;bs].&#x5B;backup_set_id]\r\nINNER JOIN msdb..backupmediafamily bmf ON &#x5B;bs].&#x5B;media_set_id] = &#x5B;bmf].&#x5B;media_set_id]\r\nWHERE rs.destination_database_name LIKE 'DATABASE_NAME_HERE'\r\nORDER BY &#x5B;rs].&#x5B;restore_date] DESC<\/pre>\n<p>How to implement in batch file to compare with today&#8217;s date? Here&#8217;s what I do. Don&#8217;t forget to change the connection ID on line 7-10.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">@echo off\r\nrem Created at 5:51 PM 29-May-15 by David B to check if database DB_NAME is current\r\nrem DO NOT MODIFY UNLESS YOU KNOW WHAT YOU ARE DOING !!\r\n\r\nrem -=-=-=-=-=-=-=-=-=- SQL Connection =-=-=-=-=-=-=-=-=-=-=\r\n\r\nset &quot;sqlConnection=SERVER_NAME\\INSTANCE_NAME&quot;\r\nset &quot;sqlUser=DATABASE_USER&quot;\r\nset &quot;sqlPassword=DATABASE_PASSWORD&quot;\r\nset &quot;sqlDB=DATABASE_NAME&quot;\r\n\r\nrem -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r\n\r\nset &quot;RegKey=HKCU\\Control Panel\\International&quot;\r\nrem echo.\r\nrem echo.Copy current format to temporary\r\nreg copy &quot;%RegKey%&quot; &quot;%RegKey%-Temp&quot; \/f&amp;gt;nul\r\nrem echo.\r\nrem echo.Modify date format\r\nreg add &quot;HKCU\\Control Panel\\International&quot; \/v sShortDate \/d &quot;dd\/MM\/yyy&quot; \/f&amp;gt;nul\r\n@REM reg query &quot;HKCU\\Control Panel\\International&quot; \/v sShortDate\r\nset LogDate=%date%\r\n\r\nset yyyy=\r\n\r\nset $tok=1-3\r\nfor \/f &quot;tokens=1 delims=.:\/-, &quot; %%u in ('date \/t') do set $d1=%%u\r\nif &quot;%$d1:~0,1%&quot; GTR &quot;9&quot; set $tok=2-4\r\nfor \/f &quot;tokens=%$tok% delims=.:\/-, &quot; %%u in ('date \/t') do (\r\nfor \/f &quot;skip=1 tokens=2-4 delims=\/-,().&quot; %%x in ('echo.^|date') do (\r\nset %%x=%%u\r\nset %%y=%%v\r\nset %%z=%%w\r\nset $d1=\r\nset $tok=))\r\n\r\nif &quot;%yyyy%&quot;==&quot;&quot; set yyyy=%yy%\r\nif \/I %yyyy% LSS 100 set \/A yyyy=2000 + 1%yyyy% - 100\r\n\r\nset todDate=%yyyy%-%mm%-%dd%\r\n\r\nrem Restore date old format configuration\r\nreg copy &quot;%RegKey%-Temp&quot; &quot;%RegKey%&quot; \/f&amp;gt;nul\r\nreg delete &quot;%RegKey%-Temp&quot; \/f&amp;gt;nul\r\n\r\nfor \/f &quot;delims= &quot; %%i in (\r\n'SQLCMD -S %sqlConnection% -d %sqlDB% -U %sqlUser% -P %sqlPassword% -Q &quot;SET NOCOUNT ON;SELECT TOP 1 bmf.physical_device_name FROM msdb..restorehistory rs INNER JOIN msdb..backupset bs ON &#x5B;rs].&#x5B;backup_set_id] = &#x5B;bs].&#x5B;backup_set_id] INNER JOIN msdb..backupmediafamily bmf ON &#x5B;bs].&#x5B;media_set_id] = &#x5B;bmf].&#x5B;media_set_id] WHERE rs.destination_database_name LIKE '%sqlDB%' ORDER BY &#x5B;rs].&#x5B;restore_date] DESC&quot; -h -1 -s &quot;,&quot; '\r\n) do set fileLoc=%%i\r\n\r\nfor \/f &quot;delims=&quot; %%i in (\r\n'SQLCMD -S %sqlConnection% -d %sqlDB% -U %sqlUser% -P %sqlPassword% -Q &quot;SET NOCOUNT ON;SELECT TOP 1 &#x5B;rs].&#x5B;restore_date] FROM msdb..restorehistory rs INNER JOIN msdb..backupset bs ON &#x5B;rs].&#x5B;backup_set_id] = &#x5B;bs].&#x5B;backup_set_id] INNER JOIN msdb..backupmediafamily bmf ON &#x5B;bs].&#x5B;media_set_id] = &#x5B;bmf].&#x5B;media_set_id] WHERE rs.destination_database_name LIKE '%sqlDB%' ORDER BY &#x5B;rs].&#x5B;restore_date] DESC&quot; -h -1 -s &quot;,&quot; '\r\n) do set resDate=%%i\r\n\r\nfor \/f &quot;tokens=1-4 delims=- &quot; %%d in (&quot;%resDate%&quot;) do (\r\nset year=%%d\r\nset month=%%e\r\nset day=%%f\r\n)\r\nif %month% == 01 (\r\nset monthdesc=Jan\r\n) else if %month% == 02 (\r\nset monthdesc=Feb\r\n) else if %month% == 03 (\r\nset monthdesc=Mar\r\n) else if %month% == 04 (\r\nset monthdesc=Apr\r\n) else if %month% == 05 (\r\nset monthdesc=May\r\n) else if %month% == 06 (\r\nset monthdesc=Jun\r\n) else if %month% == 07 (\r\nset monthdesc=Jul\r\n) else if %month% == 08 (\r\nset monthdesc=Aug\r\n) else if %month% == 09 (\r\nset monthdesc=Sep\r\n) else if %month% == 10 (\r\nset monthdesc=Oct\r\n) else if %month% == 11 (\r\nset monthdesc=Nov\r\n) else if %month% == 12 (\r\nset monthdesc=Dec\r\n)\r\nset myDate=%day%-%monthdesc%-%year%\r\n\r\nfor \/f &quot;tokens=1-4 delims=: &quot; %%d in (&quot;%resDate%&quot;) do (\r\nset hour=%%e\r\nset minute=%%f\r\nset second=%%g\r\n)\r\nset myTime=%hour%:%minute%\r\n\r\necho.\r\necho %resDate% | findstr %todDate% &amp;gt;nul &amp;amp;&amp;amp; (\r\necho Database '%sqlDB%' is FRESH, restored today at %myTime%\r\necho Restored from '%fileLoc%'\r\n) || (\r\necho Database '%sqlDB%' is OLD, last restored on %myDate% at %myTime%\r\necho Restored from '%fileLoc%'\r\necho.\r\necho Please check if backup exist or manually restore\r\n)\r\npause&amp;gt;nul<\/pre>\n<p>Feel free to use the whole code or portion to your liking and purpose. Let me know if you have question and put it in comment section below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So in my work I do automatically downloading and restore a database, however sometimes the automation doesn&#8217;t work properly perhaps due internet connection problem or space full, etc. In order to check that restore has been preformed successfully I just need to check if today is the last restore date. So here&#8217;s my query, using [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":156,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[9],"tags":[15,16],"class_list":["post-133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-database","tag-batch","tag-sql"],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8pg5H-29","jetpack-related-posts":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/eligiblestore.com\/blog\/wp-content\/uploads\/2016\/03\/logo-sql.png?fit=810%2C441&ssl=1","_links":{"self":[{"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/comments?post=133"}],"version-history":[{"count":2,"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":1270,"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/posts\/133\/revisions\/1270"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/media\/156"}],"wp:attachment":[{"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eligiblestore.com\/blog\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}