Published on

April 28, 2021

Understanding SQL Server Database Backups and Restores

When working with SQL Server, it is crucial to understand the different types of backups and how to restore them. In this article, we will explore the differences between full, transaction log, and differential backups, and learn how to restore a database from each backup type.

Full Database Backup

A full backup is a complete backup of the entire database. It includes all the data and objects in the database and is typically stored in a *.bak file. A full backup can be restored by itself, without the need for any additional backups. It serves as the foundation for restoring transaction log and differential backups.

Transaction Log Backup

A transaction log backup captures all the changes made to the database since the last full backup or transaction log backup. It is typically stored in a *.trn file. Transaction log backups are used to restore the database to a specific point in time or to recover individual data pages. They are applied after a full backup has been restored.

Differential Backup

A differential backup captures only the data that has changed since the last full backup. It is typically stored in a *.dif file. Differential backups are used to reduce the time and storage required for restoring a database. They are applied after a full backup has been restored.

Why Do We Need Different Backup Types?

Having different backup types allows for more flexibility in managing database backups. For example, if you have a large database, taking a full backup every hour would be impractical due to the size of the backup files. Transaction log backups allow you to restore the database to a specific point in time, while differential backups capture only the changes since the last full backup, reducing the restore time.

Backup and Restore Examples

Let’s look at some examples of backing up and restoring a SQL Server database.

Example 1: Restoring from the Latest Full Backup

In this example, we will restore a database named MyDatabaseTest from the latest full backup of MyDatabase. The following T-SQL code can be executed in SQL Server Management Studio (SSMS) to perform the restore:

-- restore from the latest full backup
RESTORE DATABASE [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_000011_4175616.bak' 
WITH MOVE N'MyDatabase' TO N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\MyDatabaseTest.mdf',
MOVE N'MyDatabase_log' TO N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\MyDatabaseTest_log.ldf',
RECOVERY, REPLACE;

This code restores the MyDatabaseTest database from the latest full backup file and makes it available for use.

Example 2: Restoring to the Latest Point in Time with Full and Transaction Log Backups

In this example, we will restore a database to the latest point in time using both full and transaction log backups. The following steps outline the restore process:

  1. Restore the full backup:
  2. RESTORE DATABASE [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_000011_4175616.bak'
    WITH MOVE N'MyDatabase' TO N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\MyDatabaseTest.mdf',
    MOVE N'MyDatabase_log' TO N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\MyDatabaseTest_log.ldf',
    NORECOVERY, REPLACE;
    
  3. Restore the transaction log backups:
  4. RESTORE LOG [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_001502_1580793.trn' WITH NORECOVERY;
    RESTORE LOG [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_011501_4325128.trn' WITH NORECOVERY;
    -- Repeat the above line for each transaction log backup file
    
  5. Make the database available:
  6. RESTORE LOG [MyDatabaseTest] WITH RECOVERY;
    

By restoring the full backup and applying the transaction log backups, the database is restored to the latest point in time.

Example 3: Restoring to an Earlier Point in Time

In this example, we will restore a database to an earlier point in time when a transaction log was backed up. The following steps outline the restore process:

  1. Restore the full backup:
  2. RESTORE DATABASE [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_000011_4175616.bak'
    WITH MOVE N'MyDatabase' TO N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\MyDatabaseTest.mdf',
    MOVE N'MyDatabase_log' TO N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\MyDatabaseTest_log.ldf',
    NORECOVERY, REPLACE;
    
  3. Restore the differential backup:
  4. RESTORE DATABASE [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_013001_9294408.dif' WITH NORECOVERY;
    
  5. Restore the transaction log backups:
  6. RESTORE LOG [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_021501_5352741.trn' WITH NORECOVERY;
    RESTORE LOG [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_031501_5822593.trn' WITH NORECOVERY;
    -- Repeat the above line for each transaction log backup file
    
  7. Make the database available:
  8. RESTORE LOG [MyDatabaseTest] WITH RECOVERY;
    

By restoring the full backup, the differential backup, and the transaction log backups, the database is restored to the desired earlier point in time.

Example 4: Restoring to a Specific Point in Time Between Transaction Log Backups

In this example, we will restore a database to a specific point in time between transaction log backups. The following steps outline the restore process:

  1. Restore the full backup:
  2. RESTORE DATABASE [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_000011_4175616.bak'
    WITH MOVE N'MyDatabase' TO N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\MyDatabaseTest.mdf',
    MOVE N'MyDatabase_log' TO N'D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\MyDatabaseTest_log.ldf',
    NORECOVERY, REPLACE;
    
  3. Restore the differential backup:
  4. RESTORE DATABASE [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_013001_9294408.dif' WITH NORECOVERY;
    
  5. Restore the transaction log backups:
  6. RESTORE LOG [MyDatabaseTest] FROM DISK = N'D:\Backups\MyDatabase_backup_2021_06_01_021501_5352741.trn' WITH NORECOVERY,
    STOPAT = '2021-06-01 02:30:00 AM';
    -- Repeat the above line for each transaction log backup file
    
  7. Make the database available:
  8. RESTORE LOG [MyDatabaseTest] WITH RECOVERY;
    

By using the STOPAT option in the RESTORE LOG statement, the restore process stops at the specified point in time, allowing you to restore the database to a specific moment.

Understanding the different types of SQL Server database backups and how to restore them is essential for database administrators and developers. By mastering these concepts, you can ensure the integrity and availability of your data.

Article Last Updated: 2021-06-21

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.