Restoring .File Extension SQL Server have empty content - sql-server

Can someone help me restore this backup database?
I backup my database to my D drive it created file with .file extension after that i uninstall my sql server then re install it when i try to restored the database it showed no database.
i ve tried to restore using File and Filegroups but there is no database to select.
this is my file
https://drive.google.com/file/d/0B-VCua2N1hePQjNxa3hCSy0tZjQ/view
it only contain created table with no data

Run something like this, just specify correct path to a file and where to restore a database:
USE [master]
RESTORE DATABASE Test FROM
DISK = N'D:\....\TreasuryDB' WITH FILE = 1, NOUNLOAD,
MOVE N'TreasuryDB' TO N'D:\....\Data\Test.mdf',
MOVE N'TreasuryDB_log' TO N'D:\....\Log\Testlog.ldf'

Related

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.

Create Duplicate SQL Database for Testing

I have created a database on SQL server and a front end user application in winforms c#.
It's up and running and working fine, but I've now been asked to set up a test version by the client for training new employees, so they can put in dummy data whilst they are trained without effecting the 'real' live database.
When I installed the database I had it all scripted, but things have changed since, and I don't particularly want to script all the tables again.
Is there a fast, convenient way of duplicating the database (and its data) on the same server, but under a different name?
EDIT (subsequent to Tony Hopkinson post)
I've got as far as this
exec sp_addumpdevice 'Disk','MyDatabaseBackup',N'D:\MyDatabaseBackup'
Restore Database MyDatabase From MyDatabaseBackup With File = 1,
Move 'MyDatabase' To N'C:\Program Files\Microsoft SQL
Server\MSSQL11.SQLE\MSSQL\DATA\MyDatabaseTEST.mdf',
Move 'MyDatabase_Log' To N'C:\Program Files\Microsoft SQL
Server\MSSQL11.SQLE\MSSQL\DATA\MyDatabaseTEST_log.ldf',
NORECOVERY, NOUNLOAD, STATS = 10
RESTORE LOG [MyDatabaseTEST] FROM [MyDatabaseBackup] WITH FILE = 2, NOUNLOAD, STATS = 10
exec sp_dropdevice MyDatabaseBackup
But I'm getting the following error message
Msg 3234, Level 16, State 2, Line 2
Logical file 'MyDatabase' is not part of database 'MyDatabase'. Use RESTORE FILELISTONLY to list the logical file names.
Msg 3013, Level 16, State 1, Line 2
RESTORE DATABASE is terminating abnormally.
Msg 3154, Level 16, State 4, Line 6
The backup set holds a backup of a database other than the existing 'MyDatabaseTEST' database.
Msg 3013, Level 16, State 1, Line 6
RESTORE LOG is terminating abnormally.
Device dropped.
use copy database option in SQL server management studio
I found this method to be most effective on SQL Server 2005 and 2008, Express Editions:
From the Microsoft Docs:
Right click on the database you want to duplicate and choose Tasks->"Back Up..."
Save the back up to a .bak file
Right click on the "Databases" folder in the Object Explorer in SQL Server Management Studio
Choose "Restore Database"
As the source, select "File" and point to the .bak file you created earlier.
Change the name of the database to restore to (this was the key step for me - you are not constrained to the options in the dropdown.)
SSMS will restore your .bak file to a new database, according to the name that you give it.
First do a full backup your current database, which of course you have :)
The you restore it to another one
e.g. something like
exec sp_addumpdevice 'Disk','LiveDataBackup',N'Insert backup file name here including path'
Restore Database TestData From LiveDataBackup With File = 1,
Move 'LiveData' To N'Path to where sqlserver expects the mdfs to be\TestData.mdf',
Move 'LiveData_Log' To N'Path to where sqlserver expects the ldf to be\TaxData1.ldf',
NORECOVERY, NOUNLOAD, STATS = 10
RESTORE LOG [TestData] FROM [LiveDataBackup] WITH FILE = 2, NOUNLOAD, STATS = 10
exec sp_dropdevice LiveDataBackup
Above assume your live database is cunningly named LiveData and test, TestData.
The path to where the mdf and ldf will be depends on the version of sql server and the instance name
It should be something like
C:\Program Files\Microsoft SQL Server\MSSQL11.DENALI\MSSQL\DATA\
MSSQL11 because it's sql 2012, and DENALI is my instance name and it was installed by default in C: \Program Files
Also there's no with replace, so if you wanted to run it again, you'd need to Drop your test database.
There's probably some way to do this from the GUI, but I found it a massive PIA trying to relate the UI, to what I wanted to do.
using MS SQLServer 2012, you need to perform 3 basic steps
first, generate .sql file containing only the structure of the source DB
=> right click on the source DB and then Tasks then Generate Scripts
=> follow the wizard and u can save the .sql file locally
Second, replace in .sql file the source db with the destination one
=> right click on the destination file, open the .sql file and press New Query and Ctrl-H or (edit - find and replace - Quack replace)
finally, populate with data
=> right click on the detination DB, then Tasks and then Import Data
=> Data source drop dow set to ".net framework data procider for sql server" + set connection string text field under DATA ex: Data Source=Mehdi\SQLEXPRESS;Initial Catalog=db_test;User ID=sa;Password=sqlrpwrd15
=> Same thing to do with the destination
=> check the table you want to transfer or check box besides "source :....." to check all of them
you are done.
You want to copy one Database_Production to Database_Testing in the same server. I would take database_production database as an example. I tested it in my server successfully.
Firstly, backup the database Database_Production.
BACKUP DATABASE Database_Production TO DISK ='H:\test\Database_Production.bark';
Secondly, restore Database_Production and this could rename the database name to Database_Testing.
RESTORE DATABASE Database_Testing
FROM DISK='H:\test\Database_Production.bark'
WITH
MOVE 'Database_Production_Data' TO 'H:\test\Database_Testing_Data.mdf',
MOVE 'Database_Production_log' to 'H:\test\Database_Testing_Data.ldf';
GO
Then the database Database_Production is copied to database Database_Testing. The MOVE statement causes the data and log file to be restored to the specified locations. You do not need to create database Database_Testing and the script would create it.
Dump your database into a backup file
Re-create your database from your dump - that is a script which you can run - with different name (that you have to change into the script)
You can follow (this)
If you need to create a database on the same server just create the empty database. Right Click on it and Select Restore-> Choose the database you want to make a copy of and click okay.

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