SQL Server Database Backup Examples

By:   |   Updated: 2021-11-18   |   Comments   |   Related: More > Backup


Problem

You may be new to SQL Server and need to know how to backup a SQL Server database and what the different backup types are. In this article we will cover these topics and how to create backups using the GUI and T-SQL scripts.

Solution

While there are a number of important functions a SQL Server Database Administrator performs, backing up user databases is the most important. Without backups there is nothing to restore from. It's important to understand the differences between backup types.

We'll review the three basic types of backups (full, differential, and transaction log), what they are, when to use them and how to take them.

SQL Server Backup Types

  • Full SQL Server Database Backup
    • As the name implies, this is a full backup of the entire database
    • Database recovery model can be full, bulk-logged, or simple
    • Foundation for restoring Differential and Transaction Log backups
    • Used to restore a database to the point in time the backup finished
  • Differential SQL Server Database Backup
    • Backup of the data that has changed since the last successful backup (Diff Backup)
    • Database recovery model can be full or bulk-logged only
    • Restored after a Full Backup restore to restore a database to the point in time the Differential Backup was taken
    • Can make restoring Transaction Log Backups quicker
  • Transaction Log SQL Server Database Backup
    • Backup of the transaction log (sequential record of the changes made to the database) since the last successful full or transaction log backup (Incremental Backup)
    • Database recovery model can be full or bulk-logged only
    • Restored, in the order they were taken, after a Full (and optionally a Differential Backup) to restore to a point in time of during or at the end of a Transaction Log Backup

Full SQL Server Database Backup - SQL Server Management Studio (SSMS)

Open SSMS Object Explorer.

  1. Expand the SQL Server tree
  2. Expand Databases
  3. Right click on database to backup
  4. Select Tasks
  5. Back Up…
SSMS Object Explorer
  1. Select 'Full' in 'Backup type' dropdown
  2. Select 'Disk' in 'Backup to' dropdown
  3. Add…
General
  1. Leave at default directory location or change if desired
  2. … button
Backup Destination
  1. Name backup file (.bak extension indicates full backup)
  2. Click OK
Locate Destination Files
  1. Click OK
Select Backup Destination
  1. Select 'Media Options' page
  2. Check 'Overwrite all existing backup sets' radio button
  3. Check 'Perform checksum before writing to media' box
  4. Select 'Backup Options' page
Media Options
  1. Name backup
  2. Select 'Compress backup' from 'Set backup compression' dropdown
Backup Options
  1. Click OK
Backup Complete

Full Backup - Directly Executing SQL

We can accomplish the same by just running the following SQL.

BACKUP DATABASE [MyDatabase] -- database name
TO  DISK = N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\MyDatabase_FullBackup.bak' -- backup file name
WITH INIT -- overwrite backup file 
   , NAME = N'MyDatabase-Full Database Backup' -- backup name
   , COMPRESSION -- compress backup regardless of server default
   , STATS = 10 -- display backup progress
   , CHECKSUM -- perform checksum before writing to disk
GO
Backup Output

Differential Backup - SQL Server Management Studio (SSMS)

Taking a Differential Backup looks almost the same as a Full Backup.

  1. Instead of the default of Full, select 'Differential' from the 'Backup type' dropdown
  2. Add…
General

The rest of the steps will look almost exactly the same.

  1. Leave at default directory location or change if desired
Backup Destination
  1. Name backup file (.dif extension indicates full differential backup)
  2. Click OK
Locate File
  1. Select 'Media Options' page
  2. Check 'Overwrite all existing backup sets' radio button
  3. Check 'Perform checksum before writing to media' box
  4. Select 'Backup Options' page
MEdia Options
  1. Name backup
  2. Select 'Compress backup' from 'Set backup compression' dropdown
  3. Click OK
Backup Options
  1. Click OK
Backup Complete

Differential Backup - Directly Executing SQL

You will notice here the SQL is almost the same as with a Full Backup with the exception of names and the additional 'DIFFERENTIAL' option.

BACKUP DATABASE [MyDatabase] -- database name
TO  DISK = N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\MyDatabase_DifferentialBackup_1.dif' -- backup file name
WITH DIFFERENTIAL -- tells BACKUP DATABASE you want a differential backup
   , INIT -- overwrite backup file
   , NAME = N'MyDatabase DifferentialBackup_1.dif'  -- backup name
   , COMPRESSION -- compress backup regardless of server default  
   , STATS = 10 -- display backup progress
   , CHECKSUM -- perform checksum before writing to disk
GO
Backup Complete

Transaction Log Backup - SQL Server Management Studio (SSMS)

Taking a Transaction Log backup also looks almost the same as a Full or Differential.

  1. Instead of the default of Full, this time, select 'Transaction Log' from the 'Backup type' dropdown
  2. Add…
General
  1. Leave at default directory location or change if desired
  2. … button
Select Backup Destination
  1. Name backup file (.trn extension indicates transaction log backup)
  2. Click OK
Locate Files
  1. Select 'Media Options' page
  2. Check 'Overwrite all existing backup sets' radio button
  3. Check 'Perform checksum before writing to media' box
  4. Select 'Backup Options' page
Media Options
  1. Name backup
  2. Select 'Compress backup' from 'Set backup compression' dropdown
  3. Click OK
Backup Options
  1. Click OK
Backup Complete

Transaction Log Backup - Directly Executing SQL

The difference you'll see here is in addition to the names is a Transaction Log backup is run with a 'BACKUP LOG' rather than a 'BACKUP DATABASE'.

BACKUP LOG [MyDatabase] -- database name
TO  DISK = N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\MyDatabase_TransactionLogBackup_1.trn' -- file location and backup file name
WITH INIT -- overwrite backup file
   , NAME = N'MyDatabase-Transaction Log Backup 1' -- backup name
   , COMPRESSION -- compress backup regardless of server default
   , STATS = 10 -- display backup progress
   , CHECKSUM -- perform checksum before writing to disk
GO
Backup Complete
Next Steps

In this tip we've seen the three basic SQL Server backup types and an example of how to run each backup operation with SSMS and SQL. While manually backing up databases isn't often done, it's helpful to understand the differences and understand what the automation is doing.

Here are some links to more tips on backups and restores.

Backups

Restores






get scripts

next tip button



About the author
MSSQLTips author Joe Gavin Joe Gavin is from Greater Boston. He has held many roles in IT and is currently a SQL Server Database Administrator.

View all my tips


Article Last Updated: 2021-11-18

Comments For This Article

















get free sql tips
agree to terms