SQL Server DATABASE BACKUP With Multiple Methods Using Transact-SQL

Every database developer and Database Administrator should be familiar with these methods. I will first provide a brief introduction to SQL Server databases and backup.

About SQL Server DATABASE

A database is a collection of data sets, SQL Server database consists of at least two physical files on the hard drive, an MDF file and an LDF file. The MDF file contains all of the real data. The LDF file, or Log file, contains change records for ecah data. By default, both the MDF and LDF files are located in the SQL Server, as in the following screen.

 

About SQL Server BACKUP

Backs up a complete SQL Server database to create a database backup, or one or more files or file groups of the database to create a file backup (BACKUP DATABASE).

We can create a SQL Server DATABASE BACKUP with multiple methods using Transact-SQL, as in the following:

  • Full backups to disk
  • Differential backup
  • File level backup
  • Filegroup backup
  • Full backups with progress stats
  • Backup with description
  • Mirrored backup
  • Backup with multiple options
  • Backup with [Media Name]
  • Backup with Expiration Date
  • Backup with Logical Backup Device
  • Backup with Logical Backup Device for tape

Now I am creating a SQL Server database backup using all the above method of the [DBAWorks] database.

Full backups to disk

A Full backup is a complete backup of the database and the To Disk specifies the location to create the backup. Now take the database "DBAWorks" to create a backup.

  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\DBAWorks.BAK'  
  5. GO 
 
 
 Differential backup

A Differential backup is based on the latest full backup of data. A Differential backup we can be created using a .BAK or .DIF file with the Differential command.

  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorks].DIF'  
  5. WITH DIFFERENTIAL  
  6. GO 
 

  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorks1].BAK'  
  5. WITH DIFFERENTIAL  
  6. GO 
 
 File level backup

Now I am creating a File level backup. First I will specify the logical filename within the database that can be obtained with the command sp_helpdb [DBAWorks]


 For example:
  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks] FILE= 'DBAWorks'  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorks].FIL'  
  5. GO 

Filegroup backup

Now I am creating a Filegroup backup. First I will specify the logical filename within the database that can be obtained with the command sp_helpdb [DBAWorks]



 For example:
  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks] FILEGROUP = 'PRIMARY'  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorks].FLG'  
  5. GO 

Full backups with progress stats

Stats display the progress of the backup. By default it shows the progress after every 10%. You can set the stats value to whatever you want.

  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorks_DefaultStats].BAK'  
  5. WITH STATS  
  6. GO 

  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorks_statsValue].BAK'  
  5. WITH STATS=5  
  6. GO 
 

Backup with description

This option gives the backup name. The maximum size is 255 characters we can use it, this can later be used with some of the restore commands to see what is contained with the backup.

  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorks_Des].BAK'  
  5. WITH DESCRIPTION = 'Full backup for DBAWorks'  
  6. GO 

Mirrored backup

Mirrored backup allows you to create multiple copies of the backups, preferably to different locations.

  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorks_M].BAK' --main drive  
  5. MIRROR TO DISK = 'D:\backup\NewBackup\[DBAWorks_mirror].BAK'-- mirror drive you can change path  
  6. WITH FORMAT  
  7. GO 

Backup with multiple options

You can define multiples options in it as in the following:

  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorksP].BAK'  
  5. MIRROR TO DISK = 'D:\backup\NewBackup\[DBAWorks_mirror1].BAK'  
  6. WITH FORMAT, STATS  
  7. GO 
 

Backup with [Media Name]

A database backup to disk using a format to create a new media set.

  1. USE [Master]  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\DBAWorks_Media.Bak'  
  5. WITH FORMAT,  
  6. MEDIANAME ='my_SQLServerBackups',  
  7. NAME = 'Full Backup of DBAWorks';  
  8. GO 


Backup with Expiration Date

You can specify an expiration date for creating the backup as in the following:

  1. USE [Master];  
  2. GO  
  3. BACKUP DATABASE [DBAWorks]  
  4. TO DISK = 'D:\backup\NewBackup\[DBAWorks_date].Bak'  
  5. WITH EXPIREDATE = '01/31/2015'  
  6. GO 
 Backup with Logical Backup Device

Now we create a logical backup device, DBABackup, for a backup disk file. The example then backs up the [DBAWorks] database to this logical backup device.

  1. USE [Master];  
  2. GO  
  3. EXEC sp_addumpdevice 'disk''DBABackup',  
  4. 'D:\backup\NewBackup\[DBAWorks_Drive].bak';  
  5. GO  
  6. BACKUP DATABASE [DBAWorks]  
  7. TO DBABackup  
  8. WITH FORMAT  
  9.   
  10. You can also create network device as same  
  11.   
  12. Syntax:-  
  13.   
  14. USE [Master];  
  15. GO  
  16. EXEC sp_addumpdevice 'disk''networkdevice',  
  17. '\\<servername>\<sharename>\<path>\<filename>.bak' 
 
 Backup with Logical Backup Device for tape

Now we create a logical backup device, DBATapeBackup, for a backup disk file. The example then backs up the [DBAWorks] database to this logical backup device.

  1. USE [Master];  
  2. GO  
  3. EXEC sp_addumpdevice 'tape''DBATapeBackup''\\.\tape0';  
  4. USE [DBAWorks];  
  5. GO  
  6. BACKUP DATABASE [DBAWorks]  
  7. TO TDBATapeBackup  
  8. WITH FORMAT,  
  9. MEDIANAME = 'MyDBABackup_Tape',  
  10. MEDIADESCRIPTION = '\\.\tape0',  
  11. NAME = 'Full Backup of DBAWorks'  
  12. GO 

 You can see the following query how many backups have been created in the database.
  1. SELECT a.database_name ,  
  2. b.physical_device_name ,  
  3. a.type ,  
  4. a.expiration_date ,  
  5. a.name ,  
  6. a.description  
  7. FROM msdb.dbo.backupset a  
  8. LEFT JOIN msdb.dbo.backupmediafamily b  
  9. ON a.media_set_id = b.media_set_id 

 

You can also see in the following path: