<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sandor Drieënhuizen</title>
    <description>The latest articles on DEV Community by Sandor Drieënhuizen (@sandord).</description>
    <link>https://dev.to/sandord</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F538485%2F3e370fc0-3347-4402-8a0c-a44ba027662f.jpeg</url>
      <title>DEV Community: Sandor Drieënhuizen</title>
      <link>https://dev.to/sandord</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sandord"/>
    <language>en</language>
    <item>
      <title>Restoring an SQL server backup in a Docker Container on Windows</title>
      <dc:creator>Sandor Drieënhuizen</dc:creator>
      <pubDate>Mon, 21 Feb 2022 11:23:40 +0000</pubDate>
      <link>https://dev.to/sandord/restoring-a-sql-server-backup-in-a-windows-docker-container-1em</link>
      <guid>https://dev.to/sandord/restoring-a-sql-server-backup-in-a-windows-docker-container-1em</guid>
      <description>&lt;p&gt;I don't know about you but personally I'm not too fond of installing SQL server directly on my development machine. Since I only occasionally need to work with an SQL Server database, it makes more sense to me to run it in a way that doesn't permanently 'pollute' my system :)&lt;/p&gt;

&lt;p&gt;This is why, in this post, we'll be running SQL Server in a container running SQL server. We'll also restore a database backup file and finally browse through the database.&lt;/p&gt;

&lt;p&gt;I'm assuming that you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are running Windows 10 or 11 with WSL2 (follow &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/install" rel="noopener noreferrer"&gt;these instructions&lt;/a&gt; if you don't have WSL2 installed yet).&lt;/li&gt;
&lt;li&gt;Are running Docker Desktop with WSL2 enabled in its settings.&lt;/li&gt;
&lt;li&gt;Have an existing SQL server database backup file at hand.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Spin up a container running SQL Server
&lt;/h2&gt;

&lt;p&gt;Let's start with spinning up a SQL Server container called &lt;code&gt;mssql-server&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;gt; docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=&amp;lt;YourStrong!Passw0rd&amp;gt;" --name "mssql-server" -p 1433:1433 -v sql1data:/var/opt/mssql -d mcr.microsoft.com/mssql/server


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that you can change the port by altering the &lt;code&gt;-p 1433:1433&lt;/code&gt; argument to use a different port, e.g. &lt;code&gt;-p 12345:1433&lt;/code&gt;. The port on the right side of the colon is the internal port, which should remain unchanged.&lt;/p&gt;

&lt;h2&gt;
  
  
  Copy a backup file into the container
&lt;/h2&gt;

&lt;p&gt;Now let's restore a previously made SQL server database backup file.&lt;/p&gt;

&lt;p&gt;There are two ways we can go here: either do it directly through your PC's filesystem or use &lt;code&gt;docker cp&lt;/code&gt; to copy the file to the Docker volume. &lt;/p&gt;

&lt;h3&gt;
  
  
  Option 1 - Copy using File Explorer
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that the paths that are mentioned below might change in the future as the Docker team sees fit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Use File Explorer to browse to &lt;code&gt;\\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Enter the directory that matches your container, e.g. &lt;code&gt;mssql-server-data&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Enter the &lt;code&gt;_data&lt;/code&gt; directoy.&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;backup&lt;/code&gt; directory if it doesn't exist.&lt;/li&gt;
&lt;li&gt;Copy the backup file (e.g. &lt;code&gt;DATABASE.bak&lt;/code&gt;) to the &lt;code&gt;backup&lt;/code&gt; directory.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Option 2 - Copy using Docker CLI
&lt;/h3&gt;

&lt;p&gt;Make sure that the backup file (e.g. &lt;code&gt;DATABASE.bak&lt;/code&gt;) is located in the current directory. Alternatively, you can specify its actual path in the &lt;code&gt;docker cp&lt;/code&gt; statement below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;gt; docker exec -it mssql-server mkdir /var/opt/mssql/backup
&amp;gt; docker cp DATABASE.bak mssql-server:/var/opt/mssql/backup/


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Restore a backup file
&lt;/h2&gt;

&lt;p&gt;Now that the backup file is located in the the volume, the database server has access to it. Let's restore the backup into an actual database!&lt;/p&gt;
&lt;h3&gt;
  
  
  Option 1: Restore using Azure Data Studio
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that the directories mentioned below may change in the future (or already has) as the Docker team sees fit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Download from &lt;a href="https://docs.microsoft.com/en-us/sql/azure-data-studio/download-azure-data-studio" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/sql/azure-data-studio/download-azure-data-studio&lt;/a&gt; (or &lt;code&gt;choco install azure-data-studio&lt;/code&gt; or &lt;code&gt;winget install Microsoft.AzureDataStudio&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Start Azure Data Studio &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx77xtjnmjnyfun3axky3.png" alt="Image description"&gt;
&lt;/li&gt;
&lt;li&gt;Connect to the database in the container

&lt;ul&gt;
&lt;li&gt;Expand the &lt;strong&gt;SERVERS&lt;/strong&gt; node and click on the &lt;strong&gt;New Connection&lt;/strong&gt; icon &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7sbtqfmc08txbnn9obfa.png" alt="Image description"&gt;
&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;localhost&lt;/code&gt; as the host. If you mapped the container to a port that is different from the default port 1433 you must use &lt;code&gt;localhost,#&lt;/code&gt; instead, replacing the &lt;code&gt;#&lt;/code&gt; with the actual port number. &lt;/li&gt;
&lt;li&gt;Provide the &lt;code&gt;sa&lt;/code&gt; username and the password that was supplied in the &lt;code&gt;docker run&lt;/code&gt; command above &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj84iyjfbl5mbngcpcumy.png" alt="Image description"&gt;
&lt;/li&gt;
&lt;li&gt;Select the new connection in the connections tree view and click the &lt;strong&gt;Restore&lt;/strong&gt; button &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fauepkehgc5n59q0242lp.png" alt="Image description"&gt;
&lt;/li&gt;
&lt;li&gt;At &lt;strong&gt;Restore from&lt;/strong&gt;, select the &lt;strong&gt;From File&lt;/strong&gt; option.&lt;/li&gt;
&lt;li&gt;At &lt;strong&gt;Backup file path&lt;/strong&gt;, click the browse button (three dots) and browse to the correct backup file in the &lt;code&gt;/var/opt/   mssql/backup&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;Click the &lt;strong&gt;Restore&lt;/strong&gt; button at the bottom of the window.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Option 2: Restore using command line
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Let's find out the logical file names and paths inside the backup.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  docker exec -it mssql-server /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "&amp;lt;YourStrong!Passw0rd&amp;gt;" -Q "RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/DATABASE.bak'"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;You should see output similar to the following:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  LogicalName           PhysicalName
  ----------------------------------------------------------------
  Database1_Data          C:\MSSQL\Data\Database1.mdf
  Database1_Log           C:\MSSQL\Data\Database1.ldf


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;Finally, let's restore the backup using the listed logic file names.&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;docker exec -it mssql-server /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "&amp;lt;YourStrong!Passw0rd&amp;gt;" -Q "RESTORE DATABASE Database1 FROM DISK = '/var/opt/mssql/backup/DATABASE.bak' WITH MOVE 'Database1_Data' TO '/var/opt/mssql/data/Database1.mdf', MOVE 'Database1_Log' TO '/var/opt/mssql/data/Database1.ldf'"&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Connect to the database server&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Now that our backup is restored, let's connect to the database server using Azure Data Studio (of course you can use your preferred database manager instead).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click the &lt;strong&gt;New connection&lt;/strong&gt; button.&lt;/li&gt;
&lt;li&gt;Enter the following information:

&lt;ul&gt;
&lt;li&gt;Connection type: &lt;strong&gt;Microsoft SQL Server&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Server: &lt;code&gt;localhost&lt;/code&gt; (if you mapped the container to a port that is different from the default port 1433 you must append it to the server name, separated by a comma, e.g. &lt;code&gt;localhost,12345&lt;/code&gt;)  1. Click the &lt;strong&gt;Connect&lt;/strong&gt; button.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, expand the &lt;strong&gt;Databases&lt;/strong&gt; folder in the tree, expand the &lt;strong&gt;Tables&lt;/strong&gt; folder and then and browse through the tables to see what's inside your database!&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container?view=sql-server-ver15" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container?view=sql-server-ver15&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>docker</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
