Sunday 31 August 2014

How to Backup Database in SQL Server - Create a Full Back up of Database

Introduction

In this article we will take a look at the steps you need to follow to take a full backup of SQL Server Database. The steps mentioned in this article are applicable for SQL Server 2005, SQL Server 2008, SQL Server 2008 R2 and SQL Server 2012.

Permissions Required to Take Database Backup in SQL Server

In order to take backups a user must be a member of DBCREATOR Server Role and DB_OWNER Database Role else you will receive the below mentioned error while performing backups.

Error Message

Msg 262, Level 14, State 1, Line 1
BACKUP DATABASE permission denied in database 'DatabaseName'.
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.

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

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
Database Administrator or a user who is a member of DBCREATOR Server Role and DB_OWNER Database Role can take a full backup of database using either:-
• Using SQL Server Management Studio
• Using TSQL scripts
Let us take a look at both the above mentioned options to perform a full backup of a database in SQL Server.

Take Full Backup of a Database 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. Expand Databases node and then right click the database which you want to take a full backup and point to Tasks, and then click Back Up… option as shown in the below snippet to open up Back Up Database dialog box.
How to backup Database Using SQL Server Management Studio - Perform Full Database Backup
3. In Back Up Database Dialog box, in Database List box, verify the database name. Optionally you can even select a different database to backup. You will be able to perform a Full Backup of a database which is FULL, BULK_LOGGED or Simple recovery model. To know more about different recovery models in SQL Server read the following article titled Understanding SQL Server Recovery Model.
4. In Backup type list box select Full. Once you have taken a full backup of a database then you can perform Differential backup or Transactional log backups. However, if you database is in Simple recovery model you will not be able to take a Transactional log backup. This is by design from Microsoft.
5. If you want to perform a Copy-Only backup of a database then choose Copy-Only Backup option in Back Up Database Dialog box. Using copy-only backup option you can take a full, differential or transactional log backup which is independent of the sequence of convential SQL Server backups. The copy-only backup option was introduced in SQL Server 2005.
6. In Backup Component select Databases and in Backup set leave the default Name and optionally enter Desciption. Leave the default value as 0 days for Backup set will expire after option.
Backup Database Dialog Box in SQL Server Management Studio
7. Click the Add button to select the file or backup device for the backup destination as shown in the below snippet and click OK to return to Backup Database Dialog box.
Select Backup Destination
8. Once you have completed the General settings you will be able to see a screen similar to below snippet.
Backup Database Dialog Box in SQL Server Management Studio

9. Select Options in the Select a page pane to view the advanced options. In Overwrite Media choose Back up to the existing media set and Append to the existing backup set option as shown in the below snippet

10. Under Reliability section, you can select the checkbox Verify backup when finished option. By selecting this option you can make sure the database backup is good.

Back up Database Options Page in SQL Server Management Studio
11. Under Compression section, you can select the Compress backup from the drop down list as shown in the below snippet. Microsoft introduced Database Backup Compression Feature in SQL Server 2008 for Enterprise Edition. To know more about Backup Compression read the article titled Implement Backup Compression Feature In SQL Server 2008.
12. To generate TSQL Script for the database backup click Scripts and choose Script Action to your choice from the different options which are available as shown in the snippet below.
Generate Database Backup Script Using SSMS
13. Once the database is successfully backed up you will get a popup message similar to the one shown in below snippet.
Successful Database Backup Dialog Box in SQL Server Management Studio

How to Create Full Database Backup Using TSQL Script

The following example TSQL script can be used to take the full back up of AdventureWorks database.
DECLARE @BackupDatabaseName          SYSNAME 
DECLARE
 @DatabaseBackupLocation      NVARCHAR(120) 
SET @BackupDatabaseName = 'AdventureWorks'
SET
 @DatabaseBackupLocation = 'C:\DBBackups\AdventureWorks.BAK'
/* Database Backup Script Using Compression Feature of SQL Server 2008 */
BACKUP
 DATABASE  @BackupDatabaseName
TO
  DISK = @DatabaseBackupLocation
WITH
 NOFORMAT, NOINIT,  
SKIP
, NOREWIND, NOUNLOAD, COMPRESSION,  STATS = 25 
/* Verify the Database Backup File Once the Backup has completed successfully */
RESTORE
 VERIFYONLY
FROM
 DISK = @DatabaseBackupLocation
GO
TSQL Script to Restore Database Backup in SQL Server

Conclusion

In this article you have seen how easily you can take a Full Database Back of an SQL Server Database.


SQL Server Query Execution Plans for beginners – Clustered Index Operators

In this article we will continue discussing the various execution plan operators related to clustered indexes and what they do, when do the...