Mssql Database Restore

Restore a MSSQL database from a bak file

Microsoft SQL Server Database Restore

SQL Server stores backups in a bak format. Currently Linux does not have a utility for extracting the schema and data from this files we have to use an SQL Server instance.

Start SQL server container

Use the SQL Server container image to avoid installing it system wide.

user@host ~$ podman run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=$PASSWORD" \
   -p 1433:1433 --name sql1 --hostname sql1 -d    \
   -v ./files:/files:Z                            \
   mcr.microsoft.com/mssql/server:2025-latest

Verify the container environment

user@host ~$ podman exec -it sql1 "bash"

Start an sqlcmd session inside the container

user@host ~$ podman exec -it sql1 /opt/mssql-tools18/bin/sqlcmd -No -S localhost -U sa -P $PASSWORD

Restore the backup file

To avoid problems with the file paths use the MOVE option.

RESTORE DATABASE gourmet FROM DISK = N'/files/my_sql_backup.bak'
    WITH FILE = 1,
    NOUNLOAD,
    REPLACE,
    NORECOVERY,
    STATS = 5
    MOVE N'gourmet' TO N'/var/opt/mssql/data/imcsk8.mdf'
    MOVE N'gourmet_log' TO N'/var/opt/mssql/data/imcsk8_log.LDF';
RESTORE DATABASE imcsk8 WITH RECOVERY;

Enter the database

USE imcsk8;
GO

Verify that the tables exist

select name from sys.Tables;
GO

References