Published on

December 12, 2011

Understanding Point-in-Time Recovery in SQL Server

Have you ever faced a situation where you needed to restore a database to a specific point in time? Point-in-time recovery is a crucial feature in SQL Server that allows you to restore a database to a specific moment in time, rather than just restoring to the most recent backup. In this article, we will explore the concept of point-in-time recovery and provide a working script that you can use in such scenarios.

Step 1: Set up Script and Backup Database

The first step in performing a point-in-time recovery is to set up the necessary environment and take a full backup of the database. This ensures that you have a starting point from which you can restore the database to a specific point in time. Here is a simple script that demonstrates this:


-- Creating environment
-- Create Database
CREATE DATABASE SQLAuthority;

-- Make sure database is in full recovery
ALTER DATABASE SQLAuthority SET RECOVERY FULL;

USE SQLAuthority;

-- Create Table
CREATE TABLE TestTable (ID INT);

-- Taking full backup
BACKUP DATABASE [SQLAuthority] TO DISK = N'D:\SQLAuthority.bak';

-- Inserting data into TestTable
INSERT INTO TestTable (ID) VALUES (1);

-- Taking log backup
BACKUP LOG [SQLAuthority] TO DISK = N'D:\SQLAuthority1.trn';

-- Inserting more data into TestTable
INSERT INTO TestTable (ID) VALUES (2);

-- Taking another log backup
BACKUP LOG [SQLAuthority] TO DISK = N'D:\SQLAuthority2.trn';

-- Inserting more data into TestTable
INSERT INTO TestTable (ID) VALUES (3);
INSERT INTO TestTable (ID) VALUES (4);

-- Taking one more log backup
BACKUP LOG [SQLAuthority] TO DISK = N'D:\SQLAuthority3.trn';

-- Inserting more data into TestTable
INSERT INTO TestTable (ID) VALUES (5);
INSERT INTO TestTable (ID) VALUES (6);
INSERT INTO TestTable (ID) VALUES (7);
INSERT INTO TestTable (ID) VALUES (8);

-- Truncating the TestTable
TRUNCATE TABLE TestTable;

-- Inserting more data into TestTable
INSERT INTO TestTable (ID) VALUES (9);

-- Taking one final log backup
BACKUP LOG [SQLAuthority] TO DISK = N'D:\SQLAuthority4.trn';

Step 2: Restore the Database in Point-in-Time

Once you have set up the environment and taken the necessary backups, you can proceed with restoring the database to a specific point in time. This involves restoring the full backup and subsequent transaction log backups up to the desired point in time. Here is a script that demonstrates this:


USE [master];

-- Taking a tail log backup
BACKUP LOG [SQLAuthority] TO DISK = N'D:\SQLAuthority5.trn' WITH NORECOVERY;

-- Restoring the full backup
RESTORE DATABASE [SQLAuthority] FROM DISK = N'D:\SQLAuthority.bak' WITH STANDBY = N'D:\SQLAuthority11.bak';

-- Restoring the transaction log backups
RESTORE LOG [SQLAuthority] FROM DISK = N'D:\SQLAuthority1.trn' WITH STANDBY = N'D:\SQLAuthority11.trn';
RESTORE LOG [SQLAuthority] FROM DISK = N'D:\SQLAuthority2.trn' WITH STANDBY = N'D:\SQLAuthority21.trn';
RESTORE LOG [SQLAuthority] FROM DISK = N'D:\SQLAuthority3.trn' WITH STOPAT = '2011-12-21 11:12:18.797', STANDBY = N'D:\SQLAuthority33.trn';

-- Rolling the database forward
RESTORE LOG [SQLAuthority] WITH RECOVERY;

Step 3: Clean up the Database

After performing the point-in-time recovery, it is important to clean up the database and make it operational again. This involves setting the database to single-user mode and then dropping the database if needed. Here is a script that demonstrates this:


USE [master];

-- Setting the database to single-user mode
ALTER DATABASE [SQLAuthority] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

-- Dropping the database
DROP DATABASE [SQLAuthority];

Point-in-time recovery is a powerful feature in SQL Server that allows you to restore a database to a specific moment in time. By following the steps outlined in this article and using the provided script, you can easily perform point-in-time recovery in your SQL Server environment. If you have any suggestions or enhancements for this script, feel free to let us know in the comments.

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.