Run mssql in mac and restore database

Parmeshwor Thapa
Sep 7, 2018 · 2 min read

Its docker

[root@thapa ~]# docker pull microsoft/mssql-server-linux

Install sql client

[root@thapa ~]# npm install -g sql-cli

copy your db-name.bak in your /local/dir/ then docker run

[root@thapa ~]# docker run -v /local/dir/:/var/tmp -d --name sql_server_demo -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=samp1ePassw0rd' -p 1433:1433 microsoft/mssql-server-linux

enter the running container and copy db-name.bak to /var/mssql/

[root@thapa ~]# docker exec -it <container-id> bash
[root@f1fc99fde0c ~]# cd /var/opt/mssql
[root@f1fc99fde0c ~]# cp /var/tmp/db-name.bak .

open mssql client and try restoring db-name.bak

[root@thapa ~]# mssql -u sa -p samp1ePassw0rd
mssql> restore database db-name from disk='/var/tmp/db-name.bak' with stats=10
Error: Cannot open backup device '/var/tmp/db-name.bak'. Operating system error 2(The system cannot find the file specified.).

you might see

mssql> RESTORE DATABASE db-name FROM DISK='/var/opt/mssql/db-name.bak' WITH MOVE 'db-name' TO '/var/opt/mssql/data/db-name.MDF', MOVE 'db-name_log' TO '/var/opt/mssql/data/db-name_log.ldf'Error: Logical file 'db-name' is not part of database 'db-name'. Use RESTORE FILELISTONLY to list the logical file names.

right way to do is to find the Logical name

[root@thapa ~]# restore filelistonly from disk='/var/tmp/db-name.bak'

This will list logical name, physical name and what not

LogicalName  PhysicalName Type  FileGroupName  Size ..

pick up the logical name and execute below command in sql client

IF DB_ID('db-name') IS NULL 
BEGIN
RESTORE DATABASE db-name
--FILE = N'db-name_Data'
FROM DISK = N'/var/opt/mssql/db-name.bak'
WITH
FILE = 1, NOUNLOAD, STATS = 10,
MOVE N'[Logical name from above command for data]'
TO N'/var/opt/mssql/data/db-name.mdf',
MOVE N'[logical name from above command for log]'
TO N'/var/opt/mssql/log/db-name.mdf'
END

Success

Command completed successfully.
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade