Backup and Restore master Database in SQL Server
By : Kasim Wirama, MCDBA, MCITP, SQL Server MVP
SQL Server contains system databases. They are master, model, msdb, tempdb and distribution databases. Distribution databases exist only when you enable replication feature in your current SQL Server instance. A good backup and recovery plan includes backup for both user databases and system databases. The most important system database to restore is master database.
Master database contains SQL Server system level configuration information, linked server configuration, SQL Server logins, Service broker endpoints, system stored procedure and system functions. Backing up master database in periodical basis or system configuration changes is recommended. To backup master database issue this statement :
BACKUP DATABASE master
TO DISK = ‘C:\master.bak’;
In case your master database is corrupt, you need to restore master database. This restore statement wouldn’t work for master database :
RESTORE DATABASE master
FROM DISK = ‘C:\master.bak’;
It fails because master database should be restore under offline mode on SQL Server wide setting. Go to configuration manager, click on SQL Service on left pane, and open Properties page of your SQL Server instance. Go to Advanced tab, in startup parameter you will find out location of data and log file of master database like sample below :
-dF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\DATA\master.mdf ;-eF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\Log\ERRORLOG ;-lF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\DATA\mastlog.ldf;
Add –m switch for each of them, so final result would be :
-dF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\DATA\master.mdf;-m ;-eF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\Log\ERRORLOG;-m ;-lF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\DATA\mastlog.ldf;-m;
Then, you click restart button on Log On tab of SQL Server properties page. After restarting, your SQL Server is under single user mode. Open only 1 window (query analyzer or SQL Server Management Studio (SSMS)) and restore master database with TSQL statement here :
RESTORE DATABASE master
FROM DISK = ‘C:\master.bak’
WITH REPLACE;
After you have restore master database, change back parameter below to original one.
-dF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\DATA\master.mdf;-m ;-eF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\Log\ERRORLOG;-m ;-lF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\DATA\mastlog.ldf;-m;
Original one :
-dF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\DATA\master.mdf ;-eF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\Log\ERRORLOG ;-lF:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008_1\MSSQL\DATA\mastlog.ldf;
Restart your SQL Server to make it multi user mode again.
Now your master database has successfully been recovered.