Restore database from database using script - sql

I'm looking for a simple script that I can use to overwrite one database with another one. We have a master db with master schema and data and every so often a developer wants to blow away his messed up db with a complete overwrite from the master. I currently accomplish this with SQL Server Studio and the GUI controls but I want something similar to what we use when restoring from a backup file (just without the backup file step):
RESTORE DATABASE [SlaveDB]
FROM DISK = N'E:\Backup\MasterDB.bak'
WITH FILE = 1,
MOVE N'SlaveDB_Data' TO N'E:\Data\SlaveDB_Data.mdf',
MOVE N'SlaveDB_Log' TO N'E:\Log\SlaveDB_Log.ldf',
NOUNLOAD,
STATS = 10
GO
What's the syntax for getting the db from another db instead of a backup file?

if you want to restore database from a generated script file you can use windows command line.
open CDM and run the below command (database=NorthWind, script file C:\MyScript.sql)
sqlcmd -S localhost -d NorthWind -i "C:\MyScript.sql"

I use this script every day to restore production backup to test database.
db names on production and on test are the same, suppose its name is MyDb.
delete test database
Run the script
RESTORE FILELISTONLY
FROM DISK = 'E:\WorkCopy\BackUp.bak'
RESTORE DATABASE [MyDb]
FROM DISK = 'E:\WorkCopy\BackUp.bak'
WITH
MOVE 'MyDbPrimary' TO 'D:\data\MyDb\WorkCopy.mdf',
MOVE 'MyDbImp' TO 'D:\data\MyDb\WorkCopy_1.ndf',
MOVE 'MyDbCut' TO 'D:\data\MyDb\WorkCopy_2.ndf',
MOVE 'MyDbIX' TO 'D:\data\MyDb\WorkCopy_3.ndf',
MOVE 'MyDbAUD' TO 'D:\data\MyDb\WorkCopy_4.ndf',
MOVE 'MyDbLog' TO 'D:\data\MyDb\WorkCopy_5.ldf',
move 'sysft_FTIndexCatalog' TO 'D:\data\MyDb\FTIndexCatalog'
ALTER DATABASE MyDb
Set RECOVERY SIMPLE

Related

How to make a copy of a database in SQL Server

I can't seem to find any SQL that will clone one database in SQL Server within the same server.
Let's say I have a database called MyDB. I simply want to make a copy of MyDB to MyDb2. I thought that this would work:
BACKUP DATABASE MyDB TO MyDB2;
But I get this error when I try to execute it:
Backup device 'DbTestBack' does not exist. To view existing backup devices, use the sys.backup_devices catalog view. To create a new backup device use either sp_addumpdevice or SQL Server Management Studio.
Does anyone know what the best way to do this is? I want an exact duplicate of the original including security permissions.
A simple way is taking a back up copy of current DB and restoring it.
You Can do this in single step with a simple script
backup database MyDB
to disk='D:\MyDB.bak';
restore database MyDB2
from disk='D:\MyDB.bak'
WITH move 'MyDB_Data' to 'D:\MyDB2_Data.mdf',
move 'MyDB_log' to 'D:\MyDB2_Data.ldf';
GO
Note: I made an assumption on your current data file and log file name (MyDB_Data, MyDB_log), you need to check them and make correct
DBAtools is your friend here.
Use Copy-DbaDatabase
ie.
Copy-DbaDatabase -Source SRV1 -Destination SRV1 -Database myDB -BackupRestore -SharedPath \\<<your temporary server location such as c:\temp>>

Restore database in sql server

I have some sql server's backup file(format .bck). In my case, I do not know log file name.
So I can't use with move in restore command.
Is there anyway to restore database without move? If yes, please guide me.
I want to use like below one.
restore database testdb from disk = 'folderpath\aatest.bak'
You sure can know the log file name and use WITH MOVE. Use RESTORE FILELISTONLY to retrieve the list of database files (including log) from the backup first.

SQL server 2008 r2 loading the backup to another database

i have the following query from msdn to restore a SQL Server database backup
RESTORE DATABASE <DBName> FROM DISK = '<BackupFilePath>\<BackupFileName>'
GO
What if you want to load the backup to a new database with new name without restoring it to replace the current one?
In the interface I would select Restore Files and Filegroups and change the to database name then it would create a new one with the backup data in it, is this possible with a simple query?
The restore command above can be used to specify a new name, but when the old database still exists on the same machine you'll get an error because it will try to use the same filenames for the data. You can use the WITH MOVE clause to get around that:
RESTORE DATABASE TestDB
FROM DISK = 'C:\Backup.BAK',
WITH MOVE 'AdventureWorks2012_Data' TO 'C:\MySQLServer\testdb.mdf',
MOVE 'AdventureWorks2012_Log' TO 'C:\MySQLServer\testdb.ldf';
GO
The above example came from the MSDN documentation on the restore command:
http://msdn.microsoft.com/en-us/library/ms186858.aspx

How do I copy SQL Server 2012 database to localdb instance?

I'm looking to copy a SQL Server 2012 Standard database to my localdb instance. I've tried the wizard which complains that localdb isn't a SQL Server 2005 or later express instance. I also did a backup/restore but upon the restore in my localdb I get the following error...
Running this...
RESTORE DATABASE CSODev
FROM DISK = 'C:\MyBckDir\CSODev.bak'
WITH MOVE 'CSOdev_Data' TO 'C:\Users\cblair\CSOdev_Data.mdf',
MOVE 'CSOdev_Log' TO 'C:\Users\cblair\CSOdev_Log.ldf',
REPLACE
Error message I get...
Processed 8752 pages for database 'CSODev', file 'CSOdev_Data' on file 1.
Processed 5 pages for database 'CSODev', file 'CSOdev_Log' on file 1.
Msg 1853, Level 16, State 1, Line 1
The logical database file 'CSOdev_Log' cannot be found. Specify the full path for the file.
Msg 3167, Level 16, State 1, Line 1
RESTORE could not start database 'CSODev'.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
The database ends up in "Recovery Pending" mode. It seems like it has issues with the log file. I have tried 2 different backups in case one was just corrupted.
There is known limitation (a real bug, in fact) for localDB. It will fail any RESTORE with MOVE whenever your database files are located in different folders.
You have to restore in the original folders (no MOVE). Use cmd tool such as SUBST if you need to fake a drive:/path.
I had the same problem. What eventually did work was this:
Trying to restore the database (getting the error in the OP)
Detaching the database
Reattaching the database
What happened in the last step was that SSDT performed an upgrade of the data files, that apparently was in an older format. When that was finished, the database started working without any problem!
I had the same issue, and after doing a little online research I came across an ingenious way to get it to work (albeit quite hacky). Basically, you:
Create a SqlLocalDb instance (SqlLocalDb c tmp -s).
Restore the database as you did above (e.g., SqlCmd -E -S <localdb connection string> -Q "RESTORE DATABASE ...").
Stop the SqlLocalDb instance (SqlLocalDb p tmp).
Delete the SqlLocalDb instance (SqlLocalDb d tmp).
Create a new SqlLocalDb instance (SqlLocalDb c persistent -s).
Create the database in the new instance by attaching it (SqlCmd -E -S <persistent connection string> -Q "Create Database <dbname> On (Filename = '<Mdf file location'), (Filename = '<Ldf Filename'>) For Attach".
And hopefully it should work. See here for original idea.
Edit: Added Jason Brady's correction of the create command.
Try scripting your database as schema and data and then running the script locally.
RESTORE FILELISTONLY
FROM DISK = 'D:\SQLBackups\yourdatabase.BAK'
ALTER DATABASE yourdatabasename
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE yourdatabasename
FROM DISK = 'D:\SQLBackups\yourdatabase.BAK'
with replace,
move 'logical name from file stream' to
'C:\yourdatabase.mdf',
move 'logical name from file stream' to 'C:\Yourdatabase.ldf'
ALTER DATABASE Qatar_DB SET MULTI_USER
Same problem, thanks for the help.
My local database is MS SQL 2014.
Open "SQL Server 2014 Management Studio"
Right click the database, go to "Tasks", click "Take Offline"
Detach the database
Attach the database
It work for me.
After you backup the database, you can restore the database without error.
Thanks.
I had the same problem. Try running visual studio as Administrator and try the following command
RESTORE DATABASE CSODev
FROM DISK = 'C:\MyBckDir\CSODev.bak'
WITH NORECOVERY, MOVE 'CSOdev_Data' TO 'C:\Users\cblair\CSOdev_Data.mdf',
MOVE 'CSOdev_Log' TO 'C:\Users\cblair\CSOdev_Log.ldf',
UPDATE: This did not work exactly!
Although the above statement does not produce any errors and completes successfully, the database remains in "PENDING RECOVERY" state and cannot be accessed in any way. When I tried to 'RESTORE WITH RECOVER' to bring the database online I got the same error as in the question above.
So in my case I ended up restoring the backup to a DEV server I have running with MSSQL 2008 R2 and then chose: Tasks -> Generate Scripts -> chose objects to script & Next -> click on "Advanced" button -> select "types of data to script" : Schema & data.
Now run the generated script against the local db.
Try these scripts (example with adventureworks2012 that I personally tested):
RESTORE FILELISTONLY
FROM DISK = 'c:\temp\adv2012.bak'
This will bring up the filenames as:
AdventureWorks2012 C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012RTM\MSSQL\DATA\AdventureWorks2012.mdf
AdventureWorks2012_log C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012RTM\MSSQL\DATA\AdventureWorks2012_log.ldf
Use these filenames to cinstruct your final script as this:
RESTORE DATABASE AdventureWorks2012
FROM DISK = 'C:\temp\adv2012.bak'
WITH MOVE 'AdventureWorks2012' TO 'C:\cnom_WS\Local-Databases\AdventureWorks\AdventureWorks2012.mdf',
MOVE 'AdventureWorks2012_log' TO 'C:\cnom_WS\Local-Databases\AdventureWorks\AdventureWorks2012_log.ldf',
REPLACE;
BTW I run these through Visual Studio (SQL Server Object explorer), but I strongly suspect this could be run on SSMS easily ;-)
You can do it manually. this can be done by using dot net and opening two kinds of connections and forwarding data from one of them to the other. but this needs to create the same types of columns in the local one.
You can check the importing options of MS Access 2007

Selectively restoring a database

I have been using this query:
BACKUP DATABASE RentalEase
TO DISK = 'C:\RentalEaseBackup\RentalEase.bak'
WITH COPY_ONLY;
GO
To backup my database. Someone deleted something so now I have to restore it from a previous point in time, however I don't want to overwrite new changes (other than the deletions).
What I was thinking I could do it attached the backup to the SQL Server as a new database and then perform the necessary queries to move the few deleted rows over. However, it won't attach the RentalEase.bak file because it says it isn't a primary database file.
How can I attach the database backup so I can run the SQL queries against it?
You have to RESTORE the DB, you can't attach a backup file
RESTORE DATABASE TestDB
FROM DISK = 'c:\Northwind.bak'
WITH MOVE 'Northwind' TO 'c:\test\testdb.mdf',
MOVE 'Northwind_log' TO 'c:\test\testdb.ldf'
Full syntax here
Restore the database to a different database name, and then you can do whatever you want between the two databases (good luck!)
create a new database named RentalEase2, A restore would look like this
RESTORE DATABASE [RentalEase2] FROM DISK = N'C:\RentalEaseBackup\RentalEase.bak'
WITH FILE = 1, NOUNLOAD, STATS = 10
GO