Thursday, September 4, 2014

How to Restore Database in SQL Server - Steps to Restore a Full Database Back Up

http://www.mytechmantra.com/LearnSQLServer/How-to-Restore-Database-SQL-Server-P1.html

Introduction

In this article we will take a look at the step you need to follow to restore a SQL Server Database from a Full Database Backup. The steps mentioned in this article are applicable for SQL Server 2005, SQL Server 2008, SQL Server 2008 R2 and SQL Server 2012. If you are interested in knowing the steps you need to follow to back up a database then read the following article titled “How to Back up Database in SQL Server”.
"This article has a total of 3 pages including this page. Click the links below to open all 3 pages in a seperate window to learn How to Restore a Full Database Back up in SQL Server. Page 1, Page 2, Page 3"

Permissions Required to Restore Database Backup in SQL Server

In order to restore database a user must be a member of DBCREATOR Server Role and DB_OWNER Database Role else you will receive the below mentioned error message while trying to restore a database in SQL Server.

Error Message

CREATE DATABASE permission denied in database 'master'.
RESTORE HEADERONLY is terminating abnormally. (Microsoft SQL Server, Error: 262)

Sample TSQL Script to Create Login with DBCREATOR Server Role and DB_OWNER Database Role

USE [master]
GO
DROP USER [BackupRestoreAdmin]
GO 
DROP LOGIN [BackupRestoreAdmin]
GO
CREATE LOGIN BackupRestoreAdmin WITH PASSWORD='$tr0ngP@$$w0rd'
GO
CREATE
USER BackupRestoreAdmin FOR LOGIN BackupRestoreAdmin
GO
EXEC
sp_addsrvrolemember 'BackupRestoreAdmin', 'dbcreator'
GO
EXEC
sp_addrolemember 'db_owner','BackupRestoreAdmin'
GO

A Database Administrator or a user who is a member of DBCREATOR Server Role and DB_OWNER Database Role will be able to restore a SQL Server database from databases full back up using either :-
Restore Database Using SQL Server Management Studio Restore Database Using TSQL Scripts
Let us take a look at both the above mentioned options to restore a SQL Server Database from a Full Database Backup in detail.

Restore a Full Database Backup Using SQL Server Management Studio

1. Open SQL Server Management Studio and connect to the appropriate instance of Microsoft SQL Server Database Engine in Object Explorer.
2. Right click Databases node and then select Restore Database… option from the drop down list as shown in the below snippet to open up Restore Database dialog box.
How to Restore Database in SQL Server - Steps to Restore a Full Database Back Up
3. In General Page of Restore Database dialog box, select or type the name of a new or existing database for your restore operation. In Source for restore specify the source and location of backup sets to restore. Choose From Device radio button and then click the “” button to specify backup file location.
Restore Database Dialog Box in SQL Server Management Studio

4. In Specify Backup dialog box choose File as Backup Media and then click the Add button to choose the location of database backup file from which you want to restore the database as shown in the below snippet. Click OK to return to Restore Database dialog box.
Specify Backup Location in SQL Server Management Studio
5. In Restore Database Dialog box select the checkbox under Restore as shown in the below snippet and then select Option Page from the left pane.
Restore Database Dialog Box in SQL Server Management Studio



6. In Options Page of Restore Database dialog box select the checkbox next to Overwrite the existing database (WITH REPLACE) and choose the radio button next to Leave the database ready to use by rolling back uncommitted transactions. Additional transactional logs cannot be restored. (RESTORE WITH RECOVERY) as shown in the below snippet. Finally, click OK to start restoring the SQL Server Database.
Restore Database Options Page in SQL Server Management Studio

Read more: http://www.mytechmantra.com/LearnSQLServer/How-to-Restore-Database-SQL-Server-P2.html#ixzz3CLaicFDU
Follow us: @MyTechMantra on Twitter | MyTechMantra on Facebook

7. To Generate TSQL Script for the database restore click Scripts and choose Script Action to your choice from the different options which are available as shown in the snippet below.
Generate Database Restore Script Using SSMS
8. Once the database is successfully restored you will get a popup message similar to the one shown in below snippet. 
Successful Database Restore Dialog Box in SQL Server Management Studio

How to Restore a Full Database Backup Using TSQL scripts

The below script can be used to restore AdventureWorks database.
RESTORE DATABASE [AdventureWorks] 
FROM  DISK = N'C:\DBBackups\AdventureWorks.BAK' 
WITH  FILE = 1,  
MOVE N'AdventureWorks_Data' TO N'D:\MSSQL\MSSQL10_50.SQL2008R2\MSSQL\DATA\AdventureWorks.mdf',  
MOVE N'AdventureWorks_Log' TO N'D:\MSSQL\MSSQL10_50.SQL2008R2\MSSQL\DATA\AdventureWorks.ldf',  
NOUNLOAD,  REPLACE,  STATS = 10

No comments:

Post a Comment