Monday , October 7 2024

How To Restore SQL Server Database In Docker

In today’s article, I will talk about How To Restore SQL Server Database In Docker.

Today, we will be talking about how we can restore our SQL Server installed on Docker,

which works on both windows and linux systems.

For the sample database, we will be using the sample databases shared by Microsoft on GitHub.

For sample databases, you can download the one you want from the address below.

For our operations on Windows Server;

1) We download our sample database to a directory on our server (Example: C:\SQL Backup )

2) After the download is complete, we check the status of the container with PowerShell. If it doesn’t work, we start.

We use the “docker ps – a” command for control.

If the “-a” parameter gives us a list of containers that are not running, and if it is not working, we run the container with the “docker start DockerID” command.

In our example, the container named WinDockerSQL is active.

3) After activating the container, we need to create a path to move the backup file in the container.

For this, we first run powershell on the container and create a folder.

After the Powershell screen comes up, let’s check where we are with “Get-Location”.

Let’s create a folder named DockerSQLBACKUP in the C directory.

Now that we have created the folder, let’s copy our database backup that we downloaded into C:\SQLBackup on the Windows server into the C:\DockerSQLBACKUP folder in the container.

With the code above, we are copying the file from the local to the path we specified on the docker.

Now that our backup of the database is completed, let’s use the “docker inspect” command (available in our previous articles) to find the ip address of the sql server and access it.

Then we perform the classic restore database operation.

As you can see above, there is a DockerSQLBACKUP folder in the C directory and there is a backup of the database called AdventureWorks2017 that we downloaded and copied.

As a result, we have moved a database backup to the container where we installed SQL Server 2017 in Docker, which we run on the Windows Server 2016 operating system.

For our operations on Linux;

We will be following the order of operations on the Windows side.

1) First of all, we need to download our database backup on linux.

But before downloading, we check which directory we are in linux with “pwd”. In our example, we are in the “/home/cozenc” directory.

2) We need to create a folder in the container where we will move the database backup file, we need to access the container first for this process.

To create a folder, “mkdir -p /var/opt/mssql/backup”

3) Since the folder creation process is completed, we need to move the database backup we downloaded to Linux into the container.

4) Now that we have completed the database copying, we can proceed to the restore operation, this is like the classical restore operation.

You can access via linux with sqlcmd or remote IP with SSMS.

I will connect with SSMS (SQL Server Management Studio) via RemoteIP and perform the classic restore process.

The parts marked in the picture are “d32bcf0bcc21” value is our DockerID information,

you can access this information with the docker ps command, Backup path and our database backup we put in the path.

After the restore process is completed, we can see the following information when we check it with SSMS.

 

Loading

About Çağlar Özenç

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories