{"id":3078,"date":"2021-03-03T20:39:54","date_gmt":"2021-03-04T04:39:54","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=3078"},"modified":"2021-03-03T20:39:54","modified_gmt":"2021-03-04T04:39:54","slug":"backup-and-restore-a-sql-server-database-on-premises","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=3078","title":{"rendered":"Backup and Restore a SQL Server Database On-premises"},"content":{"rendered":"\n<p>In this quickstart, you will create a new database, take a simple backup of it, and then restore it.<\/p>\n\n\n\n<p>Prerequisites<\/p>\n\n\n\n<p>To complete this quickstart, you will need the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.microsoft.com\/sql-server\/sql-server-downloads\">SQL Server<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/ssms\/download-sql-server-management-studio-ssms?view=sql-server-ver15\">SQL Server Management Studio (SSMS)<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create-a-test-database\">Create a test database<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Launch&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/ssms\/download-sql-server-management-studio-ssms?view=sql-server-ver15\">SQL Server Management Studio (SSMS)<\/a>&nbsp;and connect to your SQL Server instance.<\/li><li>Open a&nbsp;<strong>New Query<\/strong>&nbsp;window.<\/li><li>Run the following Transact-SQL (T-SQL) code to create your test database. Refresh the&nbsp;<strong>Databases<\/strong>&nbsp;node in&nbsp;<strong>Object Explorer<\/strong>&nbsp;to see your new database.<\/li><\/ol>\n\n\n\n<p>SQLCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USE &#91;master]\nGO\n\nCREATE DATABASE &#91;SQLTestDB]\nGO\n\nUSE &#91;SQLTestDB]\nGO\nCREATE TABLE SQLTest (\n\tID INT NOT NULL PRIMARY KEY,\n\tc1 VARCHAR(100) NOT NULL,\n\tdt1 DATETIME NOT NULL DEFAULT getdate()\n)\nGO\n\n\nUSE &#91;SQLTestDB]\nGO\n\nINSERT INTO SQLTest (ID, c1) VALUES (1, 'test1')\nINSERT INTO SQLTest (ID, c1) VALUES (2, 'test2')\nINSERT INTO SQLTest (ID, c1) VALUES (3, 'test3')\nINSERT INTO SQLTest (ID, c1) VALUES (4, 'test4')\nINSERT INTO SQLTest (ID, c1) VALUES (5, 'test5')\nGO\n\nSELECT * FROM SQLTest\nGO\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"take-a-backup\">Take a backup<\/h2>\n\n\n\n<p>To take a backup of your database, do the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Launch&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/ssms\/download-sql-server-management-studio-ssms?view=sql-server-ver15\">SQL Server Management Studio (SSMS)<\/a>&nbsp;and connect to your SQL Server instance.<\/li><li>Expand the&nbsp;<strong>Databases<\/strong>&nbsp;node in&nbsp;<strong>Object Explorer<\/strong>.<\/li><li>Right-click the database, hover over&nbsp;<strong>Tasks<\/strong>, and select&nbsp;<strong>Back up&#8230;<\/strong>.<\/li><li>Under&nbsp;<strong>Destination<\/strong>, confirm the path for your backup is correct. If you need to change this, select&nbsp;<strong>Remove<\/strong>&nbsp;to remove the existing path, and then&nbsp;<strong>Add<\/strong>&nbsp;to type in a new path. You can use the ellipses to navigate to a specific file.<\/li><li>Select&nbsp;<strong>OK<\/strong>&nbsp;to take a backup of your database.<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/backup-restore\/media\/quickstart-backup-restore-database\/backup-db-ssms.png?view=sql-server-ver15\" alt=\"Take SQL backup\"\/><\/figure>\n\n\n\n<p>Alternatively, you can run the following Transact-SQL command to back up your database:SQLCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BACKUP DATABASE &#91;SQLTestDB] \nTO DISK = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQL\\Backup\\SQLTestDB.bak' \nWITH NOFORMAT, NOINIT,  \nNAME = N'SQLTestDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10\nGO\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"restore-a-backup\">Restore a backup<\/h2>\n\n\n\n<p>To restore your database, do the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Launch&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/ssms\/download-sql-server-management-studio-ssms?view=sql-server-ver15\">SQL Server Management Studio (SSMS)<\/a>&nbsp;and connect to your SQL Server instance.<\/li><li>Right-click the&nbsp;<strong>Databases<\/strong>&nbsp;node in&nbsp;<strong>Object Explorer<\/strong>&nbsp;and select&nbsp;<strong>Restore Database&#8230;<\/strong>.<img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/backup-restore\/media\/quickstart-backup-restore-database\/restore-db-ssms1.png?view=sql-server-ver15\" alt=\"Restore a database\"><\/li><li>Select&nbsp;<strong>Device:<\/strong>, and then select the ellipses (&#8230;) to locate your backup file.<\/li><li>Select&nbsp;<strong>Add<\/strong>&nbsp;and navigate to where your&nbsp;<code>.bak<\/code>&nbsp;file is located. Select the&nbsp;<code>.bak<\/code>&nbsp;file and then select&nbsp;<strong>OK<\/strong>.<\/li><li>Select&nbsp;<strong>OK<\/strong>&nbsp;to close the&nbsp;<strong>Select backup devices<\/strong>&nbsp;dialog box.<\/li><li>Select&nbsp;<strong>OK<\/strong>&nbsp;to restore the backup of your database.<img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/backup-restore\/media\/quickstart-backup-restore-database\/restore-db-ssms2.png?view=sql-server-ver15\" alt=\"Restore the database\"><\/li><\/ol>\n\n\n\n<p>Alternatively, you can run the following Transact-SQL script to restore your database:SQLCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USE &#91;master]\nRESTORE DATABASE &#91;SQLTestDB] \nFROM DISK = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQL\\Backup\\SQLTestDB.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 5\nGO\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"clean-up-resources\">Clean up resources<\/h3>\n\n\n\n<p>Run the following Transact-SQL command to remove the database you created, along with its backup history in the MSDB database:SQLCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'SQLTestDB'\nGO\n\nUSE &#91;master]\nDROP DATABASE &#91;SQLTestDB]\nGO<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this quickstart, you will create a new database, take a simple backup of it, and then restore it. Prerequisites To complete this quickstart, you will need the following: SQL Server SQL Server Management Studio (SSMS) Create a test database Launch&nbsp;SQL Server Management Studio (SSMS)&nbsp;and connect to your SQL Server instance. Open a&nbsp;New Query&nbsp;window. Run <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=3078\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,625],"tags":[622],"class_list":["post-3078","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-sql","tag-backup-and-restore-a-sql-server-database"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3078"}],"version-history":[{"count":1,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3078\/revisions"}],"predecessor-version":[{"id":3079,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3078\/revisions\/3079"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}