Thursday, December 22, 2011

How to recover a SQL server to new file locations

I this example we have a recovered server where all databases is gone. This is often the situation if a server is fully restored from tape. To even get a chance to restore the databases, even the system databases you need to start SQL server. So to be able to proceed we need to have a master database in place. There is two way to solve this. Do a rebuild of the master database from the setup or copy a database file from some other system. My opinion is that it is often problem when you try to rebuild the master database so my favorite is to have a bunch of database files from all different version stored on my USB disk, include also the resource database (MSSQLSYSTEMRESOURCE).

First we have to start SQL server. Let’s put the files for master db in the location pointed out in the startup parameters for the service. If we start the SQL in normal way we certainly will get problem with the other system databases. The information where the other databases location is, reside in the master database and if this is changed or rebuilded the information will be incorrect.

To be able to start the SQL we need to start it in a mode where we tell it to not use any other databases then Master. To do this run from the command prompt sqlservr.exe  –C –F –T3608.

Now the server is started and you can now connect one session to it from Management Studio. Anyway the SQL server is up but in this situation we can´t restore the master database. To be able to restore it the server must be started in single user mode sqlservr.exe  –C –M. But this is not possible when the other systemdatabases not in place. So first we change the location to them so the SQL server can be able to start without the traceflagg.

When the server is started with  sqlservr.exe  –C –F –T3608 we can change the locations of the other system databases.

ALTER DATABASE mssqlsystemresource MODIFY FILE (NAME=data, FILENAME= 'E:\ProgramFiles\Microsoft SQL Server\MSSQL.1\MSSQL\Data\mssqlsystemresource.mdf');
ALTER DATABASE mssqlsystemresource MODIFY FILE (NAME=log, FILENAME= 'E:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\mssqlsystemresource.ldf');

ALTER DATABASE model MODIFY FILE ( NAME = modeldev , FILENAME = 'E:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\model.mdf')
ALTER DATABASE model MODIFY FILE ( NAME = modellog , FILENAME = 'E:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\modellog.ldf')

ALTER DATABASE tempdb MODIFY FILE (NAME = tempdev, FILENAME = 'E:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\tempdb.mdf')
ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'E:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\templog.ldf')

Now the locations are changed and the SQL server would be able to start with single user mode parameter sqlservr.exe  –C –M

Restart the service with the single user mode option and then do the restore of the master database. restore database master from disk = 'E:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\master.bak' with recovery, replace
The Master is restored and probably the location now are correct for the systemdatabases if you put them in the original location before. So if we are lucky it will start ok and the other databases will come up as normal. If not restore them one by one.

No comments: