By: Joe Gavin | Updated: 2021-11-18 | Comments (2) | Related: > 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.
- Expand the SQL Server tree
- Expand Databases
- Right click on database to backup
- Select Tasks
- Back Up…
- Select 'Full' in 'Backup type' dropdown
- Select 'Disk' in 'Backup to' dropdown
- Add…
- Leave at default directory location or change if desired
- … button
- Name backup file (.bak extension indicates full backup)
- Click OK
- Click OK
- Select 'Media Options' page
- Check 'Overwrite all existing backup sets' radio button
- Check 'Perform checksum before writing to media' box
- Select 'Backup Options' page
- Name backup
- Select 'Compress backup' from 'Set backup compression' dropdown
- Click OK
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
Differential Backup - SQL Server Management Studio (SSMS)
Taking a Differential Backup looks almost the same as a Full Backup.
- Instead of the default of Full, select 'Differential' from the 'Backup type' dropdown
- Add…
The rest of the steps will look almost exactly the same.
- Leave at default directory location or change if desired
- …
- Name backup file (.dif extension indicates full differential backup)
- Click OK
- Select 'Media Options' page
- Check 'Overwrite all existing backup sets' radio button
- Check 'Perform checksum before writing to media' box
- Select 'Backup Options' page
- Name backup
- Select 'Compress backup' from 'Set backup compression' dropdown
- Click OK
- Click OK
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
Transaction Log Backup - SQL Server Management Studio (SSMS)
Taking a Transaction Log backup also looks almost the same as a Full or Differential.
- Instead of the default of Full, this time, select 'Transaction Log' from the 'Backup type' dropdown
- Add…
- Leave at default directory location or change if desired
- … button
- Name backup file (.trn extension indicates transaction log backup)
- Click OK
- Select 'Media Options' page
- Check 'Overwrite all existing backup sets' radio button
- Check 'Perform checksum before writing to media' box
- Select 'Backup Options' page
- Name backup
- Select 'Compress backup' from 'Set backup compression' dropdown
- Click OK
- Click OK
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
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
- Simple script to backup all SQL Server databases
- Automating Transaction Log Backups for All SQL Server Databases
- SQL Server Backup Options and Commands Tutorial
- Backup Database Task in SQL Server Maintenance Plans
- How to create a simple database backup using SSMS
- Copy Only Backup for SQL Server
- Using PowerShell for SQL Server Database Backups
- Invoking SQL Server Database Backups with PowerShell
Restores
- SQL Server Restore Database Options and Examples
- Getting exclusive access to restore SQL Server database
- SQL Server Restore Options and Commands Tutorial
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips
Article Last Updated: 2021-11-18