Pages

Wednesday 27 May 2009

Rebuild system databases (master, model, msdb and tempdb) in sql 2008

Recently I have build a two node Sql Server 2008 cluster to be used for one of our production servers and during testing we noticed that the tempdb and the other system databases on existing production server (SQL 2000) has a different collation that that the tempdb on the new cluster. SO, I decided to rebuild the master database.

The method of rebuilding master and other system databases in Sql server 2008 has changed. It does not use rebuildm.exe as it was the case in Sql Sever 2000

To rebuild the master database in SQL 2008, you need to use setup.exe. There is no need to use the installation DVD or the installation files. All the required files should be available on the server itself.

On a normal installation, the setup.exe can be found at
C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Release

Note; before rebuilding master, it is recommended to take a backup of the existing system databases in case your rebuild is not what you wanted so that you can go back to the previous configuration. Also, before you start rebuilding the master, you need to;

- Detach all the user databases so that they can be re-attached after the rebuild
- Script all the logins
- Script all the jobs

There is a good blog with sample scripts on how to do the above in http://www.mssqltips.com/tip.asp?tip=1531
and
http://blogs.msdn.com/psssql/archive/2008/08/29/how-to-rebuild-system-databases-in-sql-server-2008.aspx


If you are rebuilding the master in a clustered environment,

Make sure that the node that you are rebuilding the master from is the owner of the instance.

Open a command prompt and go to the directory where the setup.exe is located; which typically is in

C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Release

Then type the following substituting the parameters to your environment.

setup /ACTION=REBUILDDATABASE /QUIET /INSTANCENAME=MSSQLSERVER /SQLSYSADMINACCOUNTS=MyDomain\adminUser /SAPWD=9Cazpop1Z123 /SQLCOLLATION=SQL_Latin1_General_CP1_CI_AS

Make sure there are no spaces between the “/” and the parameter otherwise you will get the following error.

“The syntax of the argument “//” is incorrect”



I was running two instances of sql server on the cluster (active-active) so I had to rebuild the master twice. Once for each instance as I wanted to change the collation of the master database on both instances.

You will have to take the sql server resource offline in a cluster other wise you will get the following error.

“If this clustered instance is owned by the current node, the sql server resource must be offline or failed.”



If the Sql server instance that you are trying to rebuild the master for is not owned by the node that you are running the serup.exe command from, you will get the following error:
“The Sql Server resource group must be owned by the current cluster node. Setup requires access to shared cluster resources.”

To fix this, you need to move the cluster resource to the node that you are running the setup.exe from



To take the Sql Serve resource offline, open Failover cluster manager, click on the Sql Server instance (or group as it used to be called) then under Server name on the right hand pane, tight-click and select take resource offline as show in the snapshot below.

After the setup.exe completes, bring the Sql resource online again and start attaching your databases, re-create logins, jobs..etc.

After completing the rebuild on the first node, I had to repeat the same process on the other node substituting the instance name in the command below.

setup /ACTION=REBUILDDATABASE /QUIET /INSTANCENAME=SNAMESSQL01 /SQLSYSADMINACCOUNTS=MyDomain\adminUser /SAPWD=9Cazpop1Z1234 /SQLCOLLATION=SQL_Latin1_General_CP1_CI_AS




Rebuilding the master, recreated the tempdb in the default location. If you are keeping tempdb in a different location perhaps on another set of disks then you need to move it after you start the sql server resource,


In order to move tempdb, use sp_helpfile to find the name of the tempdb files

Then

USE master
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = 'F:\tempdb.mdf')
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = 'F:\tempdblog.ldf')



In order to verify that the collation has changed for the master databases, use

sp_helpdb
or
SELECT DATABASEPROPERTYEX('tempdb', 'Collation')

Wednesday 20 May 2009

Copying backup files to remote server

A very useful tool for any dba to copy database backup files is ROBOCOPY which can be freely downloaded. the tool allow you to copy a list of files from a source server to the destination and it can mirror the files in both location so that if you delete a file in the source, it will automatically be deleted in the destination server. Also, in case of network issues during the copy, it is capable of restarting from where it stopped rather than copying from scratch.

This is very useful as you dont have to worry about deleting old backup files from the second server as long as your backup job on the primary takes care of deleting old backup files, they will automatically be deleted by ROBOCOPY from the secondary server.

as an example, the following command will copy the contents of the c:\backups to another server called \\backupserver and the /MIR switch will make sure that the contents of both folders remain in sync.

The /z switch will restart the copy process in case of a network issue.

you can easily create a windows scheduled task to run overnight and execute the following line to copy your backup files to another server offline.


robocopy C:\foo \\backupserver\bar /MIR /Z

For more details http://en.wikipedia.org/wiki/Robocopy