Restore database to SQL Server in ubuntu

seffu

Seffu Kioi Nyambura

Posted on April 27, 2022

Restore database to SQL Server in ubuntu

After successful installation of SQL Server, sometimes there is need to restore a database. Below we will look at how to do that using terminal.
For this, we will use AdventureWorks2019 sample database which is a publicly provided by microsoft.
You can download it using this link AdventureWorks2019

Prerequisites:

  • Successfully installed SQL Server in Ubuntu
  • File of database to be restored in .bak format

Procedure:

Step 1: Open terminal
Use the shortcut Ctrl + alt + t to open the terminal
Step 2: Ensure the database file is in the present directory

ls
Enter fullscreen mode Exit fullscreen mode

viewfile
Step 2: Connect to SQL Server
Replace 'YourPassword' with the password you set during installation

sqlcmd -S localhost -U SA -P '<YourPassword>'
Enter fullscreen mode Exit fullscreen mode

Step 3: Restore the Database
Replace /home/skn/Downloads/ with the path to where you have your database file.
NB:You can use the pwd command in the terminal to get your present working directory.

USE [master]
RESTORE DATABASE [AdventureWorks2019]
FROM DISK = '/home/skn/Downloads/AdventureWorks2019.bak'
WITH MOVE 'AdventureWorks2017' TO '/var/opt/mssql/data/AdventureWorks2019.mdf',
MOVE 'AdventureWorks2017_log' TO '/var/opt/mssql/data/AdventureWorks2019_log.ldf',
FILE = 1,  NOUNLOAD,  STATS = 5
GO
Enter fullscreen mode Exit fullscreen mode

restoration

Congratulations, restoration successful

💖 💪 🙅 🚩
seffu
Seffu Kioi Nyambura

Posted on April 27, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related