Cannot restore mssql database because .mdf file is missing - sql-server

I am trying to backup a database from SqlServer 2014 (in Windows 10 OS) and restore it to SqlServer 2017 (in Ubunutu 18.04 Digital Ocean).
I'm used the following SQL Command to Backup:
USE [master]
GO
BACKUP DATABASE [SampleDb] TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\Backup\SampleDb.bak' WITH CHECKSUM, COPY_ONLY, FORMAT, INIT, STATS = 10;
I'm trying to use the following SQL Command to Restore:
RESTORE DATABASE [SampleDb] FROM DISK = '/home/faizan/SampleDb.bak' WITH CHECKSUM, MOVE 'SampleDb_Data' TO '/home/faizan/SampleDb_Data.mdf', MOVE 'SampleDb_Log' TO '/home/faizan/SampleDb_log.ldf', RECOVERY, REPLACE, STATS = 10;
However, I'm getting the following error in my Ubuntu server when I try to restore the database:
When I ran the following Command to check the status of the .mdf and .ldf files,
RESTORE FILELISTONLY FROM DISK = '/home/faizan/SampleDb.bak'
I get the following result:
I am an ubuntu newbie and I don't see an mdf file here at all. And when I try to restore without the mdf file I get an error.
So how do I restore this Mssql backup in ubuntu when the .mdf file is missing?
(Extra note: The SampleDb.mdf file is present in Windows 10, in C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA, it just doesnt get transfered to Ubuntu)

This issue is not really Ubuntu-specific, it would appear also on Windows boxes.
There is one small thing to fix in your RESTORE script. The logical name of MDF file is according to a screenshot is SampleDb but not a SampleDb_Data.
Therefore adjusted command is:
RESTORE DATABASE [SampleDb] FROM DISK = '/home/faizan/SampleDb.bak'
WITH CHECKSUM
, MOVE 'SampleDb' TO '/var/opt/mssql/data/SampleDb_Data.mdf'
, MOVE 'SampleDb_Log' TO '/var/opt/mssql/data/SampleDb_log.ldf'
, RECOVERY, REPLACE, STATS = 10;
Update: As Larnu remarked, another thing to consider is the location of the database files. MSSQL user should have read/write access and /var/opt/mssql/data is a default location for such files.

Related

My VB program is not managing to restore file in SQL server because path

I Have a vb program that executes a SQL server script to restore a database that was created in a different computer. This is my script...
Dim sql As String = $"RESTORE DATABASE [NewStock] FROM DISK = N'C:\NewStock\NewStock.bak' WITH FILE = 1, NOUNLOAD, STATS = 5"
But when I run it it says:
System.Data.SqlClient.SqlException: 'Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\NewStock.mdf" failed with the operating system error 3(The system cannot find the path specified.).
File 'NewStock' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\NewStock.mdf'. Use WITH MOVE to identify a valid location for the file.
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\NewStock_log.ldf" failed with the operating system error 3(The system cannot find the path specified.).
File 'NewStock_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\NewStock_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
RESTORE DATABASE is terminating abnormally.'
I realized that it is complaining because the path of the SQL sever directory of the machine where the bak file was generated is different from the path in the destination machine. If I create manually the directory C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\ in the destination machine it works correctly. But not immediately because I got a denied access error and I had to change the permission of the DATA directory manually to allow write access. Then it worked.
The first message error suggested me to use WITH MOVE to identify a valid location. Then I tried this...
Dim sql As String = $"RESTORE DATABASE [NewStock] FROM DISK = N'C:\NewStock\NewStock.bak' WITH FILE = 1, MOVE N'NewStock' TO N'C:\NewStock\NewStock.mdf', MOVE N'NewStock_log' TO N'C:\NewStock\NewStock_log.ldf', NOUNLOAD, STATS = 5"
Then I ran in the very same permission value. Again if I change the permission of the C:\newstock folder manually it works. Unfortunately it is unacceptable because the reason we are doing this vb.net program is to make the life of our customers easier. Several of these users use SQL Express and dont even have SSMS in their computers and dont feel secure enough to change permissions and things like that.
I would like to know if there is a parameter I can add to the script that I tell to SQL server to override the original path and just save the new files in its default location.
You have to use WITH MOVE to tell the script where you want the files to physically be located on the destination machine:
RESTORE DATABASE [NewStock]
FROM DISK = N'C:\NewStock\NewStock.bak'
WITH MOVE 'NewStock' TO 'C:\Wherever\NewStock.mdf',
MOVE 'NewStock_log' TO 'C:\Wherever\Log\NewStock_log.ldf',
RECOVERY, REPLACE

I can not restore my database SQL Server Management Studio can't recognize my .bak files

I'm trying to restore a DB from the class. However, when I try to restore the .bak file, it seems like SSMS doesn't recognize it.
I gave full permissions to the folder which contains the .bak file (it is the default backup folder of ms SQL studio).
Steps I've taken to restore the .bak file:
Right click on DB -> Restore DB -> From device (selected the .bak file location) -> To Database (selected the DB destination)
Like here:
C:\Program Files (x86)\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Backup
The DB is not corrupt I checked 2 other DB same result so I have no idea what is the problem
Here images:
I can not see the DB and I have almost the latest version of MS SQL studio so I can not understand what is going on here and why I can not restore DBs
I checked here:
restore database in ssms 2017 selected bak file in device option and showing nothing in Backup sets to restore option and disable Ok button too
and here:
SQL Server Management Studio can't recognize .bak file
https://www.youtube.com/watch?v=U0FpXwQfBaU
It should be easy and simple like the video above but like I said above something is wrong here and I do not know what is it.
Image of my SQL version
Once you (i mean the service account) got full permissions on the folder where the .bak file sitting and file not corrupted you should be able to restore without any issue, but there are times things fail in GUI and work perfectly with command line. not sure the issue might be exists one of the fix of SSMS release notes
However, you probably want to try following:
Verify the backup file - it does the verification of backup file and detects any error/corruption within backup file
RESTORE VERIFYONLY FROM DISK = 'C:\YourbackupLocation\DbName.bak';
Read the header - it returns information of backup file i.e. DB Version, appended backup files, LSN info and database recovery model etc..
RESTORE HEADERONLY FROM DISK = 'C:\YourbackupLocation\DbName.bak';
Get list of file names from backup - This will be helpful if the file location is different from the source server from where the backup was created
RESTORE FILELISTONLY FROM DISK = 'C:\YourbackupLocation\DbName.bak' ;
Restore backup - actual restore via T-SQL
RESTORE Backup DBName FROM DISK = 'C:\YourbackupLocation\DbName.bak' with replace, recovery, stats;
To relocate files
RESTORE Backup DBName FROM DISK = 'C:\YourbackupLocation\DbName.bak'
with replace, recovery, stats
--- Get the logical name from the result of "RESTORE FILELISTONLY" command
move 'DBName' to 'C:\NewLocation\DBName.mdf',
move 'DBName_Log' to 'C:\NewLocation\DBName_log.ldf' ;

Cannot restore database with .mdf extension in sql server

I have back-up my database from LocalDB server which is .mdf file:
C:\THESIS - SOURCE CODE\SIS-JEN\SIS\APP_DATA\SIS_DB.MDF
and it was successfully backed-up with an extension of .bak:
C:\THESIS - SOURCE CODE\SIS-JEN\SIS\APP_DATA\SIS_DB.MDF.bak
Now, I want to restore this back-up file to the new server(my pc/server), but I cannot successfully restore using the .bak file:
C:\THESIS - SOURCE CODE\SIS-JEN\SIS\APP_DATA\SIS_DB.MDF.bak
I've got this error:
Restore of database 'C:\THESIS-SOURCE
CODE\SIS-JEN\SIS\APP_DATA\SIS.DB.MDF' failed
Can anybody help me to fix this error?
Check about RESTORE statement -
USE [master]
GO
RESTORE DATABASE [SIS_DB] FROM DISK = N'D:\SIS_DB.bak'
WITH FILE = 1,
MOVE N'SIS_DB' TO N'D:\SIS_DB.mdf',
MOVE N'SIS_DB_log' TO N'D:\SIS_DB_log.ldf', NOUNLOAD

Restore MSSQL database backup to a new database

I need to make clean database template backup and then generate a new one database with another name, but with backuped content.
Backup script is:
sqlcmd -S %DB_HOST% -Q "BACKUP DATABASE %DB_NAME% TO DISK = '%BACKUP_FILE%'"
Restore script is:
sqlcmd -S %DB_HOST% -Q "RESTORE DATABASE %DB_NAME% FROM DISK = '%BACKUP_FILE%' WITH REPLACE"
And it works on the same database name.
But if i backup DB 'ORIGINAL_DB_NAME' and then restore it to 'NEW_DB_NAME' it will raise exception:
Msg 1834, Level 16, State 1, Server <HOST>, Line 1
The file '.....\MSSQL\Data\ORIGINAL_DB_NAME.mdf' cannot be overwritten. It is being used by database 'ORIGINAL_DB_NAME'.
....
You need to specify a new location for the data/log files.
See this MSDN article, Example D
RESTORE DATABASE AdventureWorks2012
FROM AdventureWorksBackups
WITH NORECOVERY,
MOVE 'AdventureWorks2012_Data' TO
'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.mdf',
MOVE 'AdventureWorks2012_Log'
TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.ldf';
RESTORE LOG AdventureWorks2012
FROM AdventureWorksBackups
WITH RECOVERY;
You can also avoid that conundrum using SSMS. Under the Options tab, after you have selected a source for your restore direct the data and log files to either the files you want to overwrite, or point them to the directory you need and name them appropriately.
If you go to Files on the Restore Configuration, make sure that the Original File Name is not the same as Restore As, that should solve your issue.

Import .bak file to a database in SQL server

I have a file with .bak extension.
How can I import this date to a database in SQL Server?
On SQL Server Management Studio
Right click Databases on left pane (Object Explorer)
Click Restore Database...
Choose Device, click ..., and add your .bak file
Click OK, then OK again
Done.
This will show you a list of database files contained in DB.bak:
RESTORE FILELISTONLY
FROM DISK = 'D:\3.0 Databases\DB.bak'
You will need the logical names from that list for the MOVE operation in the second step:
RESTORE DATABASE YourDB
FROM DISK = 'D:\3.0 Databases\DB.bak'
and you have to move appropriate mdf,ndf & ldf files using
With Move 'primarydatafilename' To 'D:\DB\data.mdf',
Move 'secondarydatafile' To 'D:\DB\data1.ndf',
Move 'logfilename' To 'D:\DB\log.ldf'
You can simply restore these database backup files using native SQL Server methods, or you can use ApexSQL Restore tool to quickly virtually attach the files and access them as fully restored databases.
Disclaimer: I work as a Product Support Engineer at ApexSQL
Instead of choosing Restore Database..., select Restore Files and Filegroups...
Then enter a database name, select your .bak file path as the source, check the restore checkbox, and click Ok. If the .bak file is valid, it will work.
(The SQL Server restore option names are not intuitive for what should a very simple task.)
On Microsoft SQL Server Management Studio 2019:
On Restore Database window:
Choose Device
Choose Add and pick target file
OK to confirm
OK to confirm restore
Connect to a server you want to store your DB
Right-click Database
Click Restore
Choose the Device radio button under the source section
Click Add.
Navigate to the path where your .bak file is stored, select it and click OK
Enter the destination of your DB
Enter the name by which you want to store your DB
Click OK
Done
Although it is much easier to restore database using SSMS as stated in many answers.
You can also restore Database using .bak with SQL server query, for example
RESTORE DATABASE AdventureWorks2012 FROM DISK = 'D:\AdventureWorks2012.BAK'
GO
In above Query you need to keep in mind about .mdf/.ldf file location.
You might get error
System.Data.SqlClient.SqlError: Directory lookup for the file "C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL.1\MSSQL\DATA\AdventureWorks.MDF" failed with the operating system error 3(The system cannot find the path specified.). (Microsoft.SqlServer.SmoExtended)
So you need to run Query as below
RESTORE FILELISTONLY
FROM DISK = 'D:\AdventureWorks2012.BAK'
Once you will run above Query you will get location of mdf/ldf use it Restore database using query
USE MASTER
GO
RESTORE DATABASE DBASE
FROM DISK = 'D:\AdventureWorks2012.BAK'
WITH
MOVE 'DBASE' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DBASE\MSSQL\DATA\DBASE.MDF',
MOVE 'DBASE_LOG' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DBASE\MSSQL\DATA\DBASE_1.LDF',
NOUNLOAD, REPLACE, NOUNLOAD, STATS = 5
GO
Source:Restore database from .bak file in SQL server (With & without scripts)
Simply use
sp_restoredb 'Your Database Name' ,'Location From you want to restore'
Example: sp_restoredb 'omDB','D:\abc.bak'
You can use node package, if you often need to restore databases in development process.
Install:
npm install -g sql-bak-restore
Usage:
sql-bak-restore <bakPath> <dbName> <oldDbName> <owner>
Arguments:
bakpath, relative or absolute path to file
dbName, to which database to restore (!! database with this name will be deleted if exists !!)
oldDbName, database name (if you don't know, specify something and run, you will see available databases after run.)
owner, userName to make and give him db_owner privileges (password "1")
!! sqlcmd command line utility should be in your PATH variable.
https://github.com/vladimirbuskin/sql-bak-restore/

Resources