By: Matteo Lorini | Updated: 2011-08-12 | Comments (6) | Related: > Disaster Recovery
Problem
Your SQL Server has crashed and you need to quickly get it up and running on another physical server. Instead of having to build a new server and install SQL, we look at how to recreate the entire SQL Server environment on an existing server.
Solution
To quickly recreate the environment we are going to use an existing SQL Server that is already setup for maybe standby, QA or something else. The process will involve restoring the master, msdb and user databases. In order for this to work, both the source and target SQL Servers must be on the exact service pack otherwise it will not be possible to successfully restore the master database on the target server.
For our example these are the environments that I am working with:
- Source: SQL 2008 SP2 (10.0.4000). master, model and msdb databases are located on drive E: while tempdb is on a dedicated drive K: and set with an initial size of 10GB
- Target: SQL 2008 SP2 (10.0.4000). target server has only one drive C: and it does not have enough disk space to create a 10GB file for tempdb
To get the version of the target environment we can run the following command in SSMS.
To get the version of the source environment we can run this command in SSMS against the master backup file and check the following fields: SoftwareVersionMajor, SoftwareVersionMinor, SoftwareVersionBuild
In the image below, we can see the version is 10.0.4000.
Now that we have verified that both environments have the same version of SQL Server we can start the rebuild process on the target server.
Rebuild Steps
Step 1:
On the target SQL Server shutdown the services: SQL Server, Agent, SSIS, etc.. using SQL Server Configuration Manager.
Step 2:
Open a CMD window and start SQL Server in single user mode using this command:
Step 3:
In a new CMD window start SQLCMD and restore the master database using a command similar to the following.
FROM DISK ='X:\Backup\master_backup_2011_02_17_000001_1662523.BAK'
WITH REPLACE,
MOVE 'MASTER' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\master.mdf',
MOVE 'MASTLOG' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\mastlog.ldf'
I
Step 4
As soon as the master database restore is complete, the instance of SQL Server that was started in single user mode stops. If we try to run it again using "sqlservr -m", SQL Server will not start because the master database contains a reference that model and msdb are on the E: drive and tempdb is on the K: drive, but our target server does not have drives E or K.
In order to fix this drive issue, we have to start SQL server as follow from a CMD window:
These are what the parameters do:
-c: start sql not as a service
-f: start with minimal configuration
-T3608: trace flag 3608 skips automatic recovery for all databases except the master database
Step 5:
In another CMD window start SQLCMD and run the following commands to relocate model and tempdb. Also, let's resize tempdb as well.
MODIFY FILE (NAME=modeldev, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\model.mdf')
GO
ALTER DATABASE model
MODIFY FILE (NAME=modellog, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\modellog.ldf')
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME=tempdev, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\tempdb.mdf')
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME=templog, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\templog.ldf')
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME=tempdev, SIZE = 1MB)
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME=templog, SIZE = 1MB)
GO
Step 6:
Stop SQL Server from the CMD window by either closing the CMD window or pressing CTRL+BREAK. Then start SQL Server using "SQL Server Configuration Manager"
Step 7:
Restore msdb database by issuing the following command which can be done using SSMS or SQLCMD.
FROM DISK ='X:\Backup\msdb_backup_2011_02_17_000001_1662523.BAK'
WITH REPLACE,
MOVE 'msdbdata' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\msdb.mdf',
MOVE 'msdblog' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\msdb.ldf'
Step 8:
Restore your user databases and start SQL Server Agent.
Summary
Since we restored the master and msdb databases we were able to recover the system information such as logins, jobs, schedules, etc... without having to recreate them. This approach is much faster instead of having to build an entire server and install SQL Server, but as mentioned the versions of SQL Server need to be the same in order to restore the system databases.
Next Steps
Here are some related articles.
- Rebuilding the SQL Server master database Part 1
- Rebuilding the SQL Server master database Part 2
- Rebuilding the SQL Server master database Part 3
- SQL Server System Databases
- Simplified process to move all SQL Server system databases at one time
- SQL Server backup and restore of the Resource database
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips
Article Last Updated: 2011-08-12