简体   繁体   中英

Can't restore SQL Server database to another server

I'm trying to migrate databases from an old, dying server, to the new one.

All over the internet, I found two handy scripts by Andrew Morgan, for saving and restoring databases. The backup part works well; I get my db.bak on my local pc by sharing the folder over the network and having the script backup the db to the unc path.

Unfortunately, the restoring part won't work, and I don't know why (and my senior DEV doesn't know either, and he's the one knowing SQL aorund here).

I'm calling it this way:

Restore-SQLdatabase -SQLServer "NEWSERVER\MSSQLSERVER" -SQLDatabase "DBToMigrate" -Path "C:\Backup\DBToMigrate.bak" -SQLusername "sa" -SQLpassword "IMAPLAINTEXTPASSWORD"

sa has complete power over the database.

It fails with the following message:

Attempting to connect to the Specified SQL server: Success
WARNUNG: An Exception was caught while restoring the database!
WARNUNG: Ausnahme beim Aufrufen von "ExecuteNonQuery" mit 0 Argument(en): "Incorrect syntax near '''.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon."
WARNUNG: attempting to recover the database

The part in question is:

ALTER DATABASE $SQLDatabase
SET SINGLE_USER WITH ROLLBACK IMMEDIATE

RESTORE DATABASE $SQLDatabase
FROM DISK = '$path'
WITH REPLACE

When it gets executed, the scipt gets the exception.

I tried putting a CREATE DATABASE $SQLDatabase in front of the ALTER part, but then it says something along the lines of "that database name already exists", after creating that db.

Also, when omitting the altering part and changing that part to

RESTORE DATABASE $SQLDatabase FROM DISK = '$path' WITH REPLACE

It says something about the database files, giving me the path on the old server (which I'd like to be completely irrelevant, but it seems there may be my next problem), cannot be found (of course, since I'm on my local machine now, which doesn't have those).

I'm doing it with a script, because there are around 15 databases we need to migrate, and also because I want to learn how to do this stuff without just using the SSMS wizard.

Update: I changed the apostrophes to normal ones, that was indeed the mistake. Now I just need to get the file name of the db files so I can MOVE them to the new server.

All you need is to write a new destinations to your files, you can check the syntax of RESTORE here: RESTORE Statements (Transact-SQL)

And your code will be like this:

RESTORE DATABASE AdventureWorks2012  
   FROM AdventureWorksBackups  
   WITH MOVE 'AdventureWorks2012_Data' TO   
'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Data\NewAdvWorks.mdf',   
      MOVE 'AdventureWorks2012_Log'   
TO 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Data\NewAdvWorks.ldf';  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM