星期三

Restore Sql Server

SQL Server:用SQL 語句還原數據庫(with Move 選項用法)。



當我們把服務器A上的一個數據庫備份後,在服務器B上還原的時候,直接restore 的時候,大多情況下都會出錯,可能的原因是數據庫的文件(數據和日誌)在不同服務器上所在的path不同。這時候就需要restore with move 選項,來把數據庫文件還原到目標path 中。 with move 的詳細語法:



MOVE 'logical_file_name' TO 'operating_system_file_name' [ ,...n ]

當我在把SQL Server 2000 Instance(實例)中的數據庫Nortwind,還原到SQL Server 2005 Instance 中的時候,也需要使用restore with move 選項來進行還原。



1. 查看SQL Server 2000 中Northwind 數據庫文件的邏輯文件名(logical file name)和物理文件路徑(operation system file name):



use Nothwind

go



select name, filename, * from dbo.sysfiles

name filename

-------------- ---------------------------------- --------------------------------

Northwind d:\program files\microsoft sql server\mssql$sqla\data\northwnd .mdf

Northwind_log d:\program files\microsoft sql server\mssql$sqla\data\northwnd.ldf

2. 備份SQL Server 2000.Northwind 數據庫



backup database Northwind to disk = 'c:\Northwind.bak'

3. 在SQL Server 2005 Instance 中還原Northwind 數據庫。



use master

go



restore database Northwind from disk = 'c:\Northwind.bak'

用普通方式restore 的話,SQL Server 會報出一大堆的錯誤信息。



Msg 3634, Level 16, State 1, Line 1

The operating system returned the error '32(error not found)'

while attempting 'RestoreContainer::ValidateTargetForCreation'

on 'd:\Program Files\Microsoft SQL Server\MSSQL$SQLA\data \northwnd.mdf'.



Msg 3156, Level 16, State 8, Line 1

File 'Northwind' cannot be restored to

'd:\Program Files\Microsoft SQL Server\MSSQL$SQLA\data\northwnd.mdf'.

Use WITH MOVE to identify a valid location for the file.



Msg 3634, Level 16, State 1, Line 1

The operating system returned the error '32(error not found)'

while attempting 'RestoreContainer::ValidateTargetForCreation'

on 'd:\Program Files\ Microsoft SQL Server\MSSQL$SQLA\data\northwnd.ldf'.



Msg 3156, Level 16, State 8, Line 1

File 'Northwind_log' cannot be restored to

'd:\Program Files\Microsoft SQL Server\MSSQL$SQLA\data \northwnd.ldf'.

Use WITH MOVE to identify a valid location for the file.



Msg 3119, Level 16, State 1, Line 1

Problems were identified while planning for the RESTORE statement. Previous messages provide details.



Msg 3013, Level 16, State 1, Line 1

RESTORE DATABASE is terminating abnormally.

4. 在SQL Server 2005 中還原Northwind 數據庫正確方法:restore with move。



SQL Server 2005 中數據文件所在目錄為:d:\microsoft sql server\mssql.1\mssql\data\



restore database Northwind from disk = 'c:\Northwind.bak'

with move 'Northwind' to 'd:\microsoft sql server\mssql.1\mssql\data\Northwind.mdf'

,move 'Northwind_log' to 'd:\ microsoft sql server\mssql.1\mssql\data\Northwind.ldf'

Northwind 數據庫在SQL Server 2005 中順利還原。



注意,在SQL Server 2005 中還原Northwind 的時候,並不需要首先創建一個同名的Northwind 數據庫,而是直接進行restore。



另外的一個問題是,如果客戶給你一個Northwind.bak 備份文件,讓你在自己的機器上restore。那麼如何來確定with move 中的邏輯文件名呢?一個方法是向客戶諮詢獲取,二是使用SQL Server restore filelistonly 來查看。



5. 使用SQL Server restore filelistonly 命令來查看邏輯文件名



從SQL Server restore filelistonly 命令結果中可以獲取很多信息,下面僅列出LogicalName and PhysicalName。



restore filelistonly from disk='c:\Northwind.bak'

LogicalName PhysicalName

--------------- --------------------------------- ---------------------------------

Northwind d:\Program Files\Microsoft SQL Server\MSSQL$SQLA\data\ northwnd.mdf

Northwind_log d:\Program Files\Microsoft SQL Server\MSSQL$SQLA\data\northwnd.ldf

沒有留言:

張貼留言