restoring database sql server - sql-server

I have a probleme while restoring my database from a .bak file. If I use sql query line I'll find the database but with no table in it, and if I use restoring wizard with the same file and the default configuration i'll get my database and my tables. I think that I'm missing something in this query:
first query
RESTORE DATABASE ColisageTest
FROM DISK = 'e:\colisageTest.bak'
second query
RESTORE DATABASE ColisageTest
FROM DISK = 'e:\colisageTest.bak'
WITH REPLACE,
MOVE 'ColisageTest' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.COLISAGE\MSSQL\DATA\ColisageTest.mdf',
MOVE 'ColisageTest_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.COLISAGE\MSSQL\DATA\ColisageTest_log.ldf'
GO
third query
RESTORE DATABASE ColisageTest
FROM DISK = 'e:\colisageTest.bak'
with RECOVERY;
GO
No one of them is working all of them it just restore my database but no tables

Are you sure the database is not empty on tables?
One way to test this is to make another .bak file (that you know have tables) and try to restore it for validation.
No problem with empty tables I'm saying you might have an empty database, ie there is no table there at all.

Related

RESTORE database with different name Fails - "The backup set holds a backup of a database other than the existing"

I have database backup A.bak
I want to restore that backup file, using the SQL Server Management Studio, into database B which has all the same tables/columns but just different name.
If I try to do the restore - I am getting error:
The backup set holds a backup of a database other than the existing
How can I resolve this? I tried renaming the .bak file but it didn't work
Specify the "Overwrite the existing database (WITH REPLACE)" option:

Database restore is missing tables

I am using SSMS 2017 and attempting to restore a SQL Server 2016 backup (.bak) file to a new database (the data file and log file names in the original backup are different from the new database I am restoring to), so for example, db1.bak is being used to create a new database called db2 through a restore operation.
After the restore completes, all stored procedures and views are present, but none of the tables are there.
These are the steps I've taken in SSMS to perform the restore:
Right click the Databases folder
Select "Restore Database"
Under the General Page: For "Source" I choose Device and select the db1.bak file, and for "Destination" I enter the name of the new database to be created from the backup (db2)
Under the Files Page I select Relocate all files to folder (both Data and Log folder paths have been pre-populated with the correct names) as have the Logical File Name and the Restore As paths.
Under the Options Page I either select nothing or select Overwrite the existing database (WITH REPLACE).
Once the restore completes "successfully" I expand the newly created db2 and click to expand the tables and receive this error message:
What am I doing wrong?
Here is the script:
USE [master]
RESTORE DATABASE [PdsClone] FROM DISK = N'F:\SQLBackups\PDS_backup_2017_12_12_001015_0199267.bak' WITH FILE = 1, MOVE N'PDS_Data' TO N'E:\Program Files\Microsoft SQL Server\MSSQL13.PDS\MSSQL\DATA\PdsClone.mdf', MOVE N'PDS_Log' TO N'E:\Program Files\Microsoft SQL Server\MSSQL13.PDS\MSSQL\DATA\PdsClone_log.ldf', NOUNLOAD, REPLACE, STATS = 5 GO
UPDATE
I had the server group add another disk to that server and moved the SQL backups there, and I was able to successfully complete the restore this morning. I had previously cleaned off most of the backup files, but could it have been a space issue that prevented the tables from showing up in the restore? Every try it indicated success but no tables. They are there now, and I am thankful for your suggestions and help!
Not really an answer to what happened, but perhaps SSMS was doing something a little buggy as #JeroenMostert suggested.

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/

MSSQL2005: Restore database without restoring full text catalog

When restoring my database i have a problem with the physical file of the full text catalog being in use.
The file 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\FTData\MyCatalog' cannot be overwritten. It is being used by database 'demo2'.
I use this restore statement
RESTORE database demo from disk = N'c:\temp\demo.bak' WITH REPLACE
,MOVE 'demo_Data' TO 'd:\Program Files\Microsoft SQL Server 2005\MSSQL\Data\demo.MDF'
,MOVE 'demo_Log' TO 'd:\Program Files\Microsoft SQL Server 2005\MSSQL\Data\demo.LDF';
A solution would be to restore without the full text catalog, but i can's figure out how to do that.
I have had to restore databases with full-text as well (in fact, fixed my auto-restore script to auto-restore full-text index as well)
I don't know any way to NOT restore them (SQL restore will give you an error if you don't specify RESTORE WITH MOVE for the full-text index, unless there is a path that matches the original path inside the .bak file)
I suggest
restore the entire database, including full-text index
delete all full-text index inside the database
enumerate all full-text catalogs
SELECT name FROM DB.sys.fulltext_catalogs (SQL2005/2008)
then generate the drop command (you may have to drop the index on the tables before dropping the catalogs)
DROP FULLTEXT CATALOG [name]
I'm not sure how to restore without fulltext file but you can always restore with move. You are doing it already for database and log file, just complete your statement with:
,MOVE 'sysft_demo_ft' TO 'd:\NewLocationForFullTextFile.ft'
You can get the exact names of the files with:
RESTORE FILELISTONLY
FROM DISK='C:\yourbackupfile.bak'

Resources