Many a times due to incorrect query being fired from the developers or due to some abnormal behavior of the server, we are required to do a Point in Time Recovery. Point in Time Recovery means that we need to Recover the data to the point where the disaster happened. Let us consider the below case:
Consider we have a table named student present in the database named student on the server, it’s content is as shown below:
With 6 records in the table, at this point of time we have taken a Full Backup of the database. The name of the Full Backup is Student_Full_Backup_20120614.bak and is kept under the location named D:\Satnam on the server.
Once the Full Backup happens successfully, another record gets inserted into the table as shown below:
insert student select 7,'Purvish','Shah'
After this you have a differential backup of the database being performed. The name of the differential backup is Student_Differential_Backup_20120614.bak and it resides under the path named D:\Satnam on the server.
Once the differential backup happens successfully 2 new records get inserted into the table as shown below:
insert student select 8,'Khushboo','Jatav' insert student select 9,'Satish','Ughade'
Just after the above 2 records gets inserted into the table a developer fires a delete statement without a where clause as shown below:
delete from student
Once the above query got fired, the entire content of the table got deleted and at this point of time we need to recover all the data back to its original state.
Understanding the requirement, I decided to implement the below plan to Recover the entire data back.
RESTORE DATABASE Student_Backup FROM DISK='D:\Satnam\Student_Full_Backup_20120614.bak' WITH MOVE 'student' TO 'D:\Satnam\student.mdf', MOVE 'student_log' TO 'D:\Satnam\student_log.ldf', NORECOVERY,STATS=10 GO
RESTORE DATABASE Student_Backup FROM DISK='D:\Satnam\Student_Differential_Backup_20120614.bak' WITH NORECOVERY,STATS=10 GO
RESTORE LOG Student_Backup FROM DISK='D:\Satnam\Student_Transactional_Log_20120614.trn' WITH RECOVERY,STATS=10, STOPAT = 'June 14, 2012 10:31:00 PM' GO
Once the above T-SQL got executed, I decided to check the contents of the student table in the student_backup database and the results obtained were as shown below:
I then transferred these records from the student_backup database to the original student database so that the original database has the Point in Time data with it; please refer the T-SQL below,
insert [student].[dbo].[student] select * from [student_backup].[dbo].[student]
Now lets query the student table in the student database and review the data, please refer the screen capture below:
Isn’t it interesting, such situation can happen in Real World and the above solution will help you to come out of trouble.
Many Thanks to all the viewers for providing their valuable time in reading the article.Please do let us know in case if there is any better solution to this. If you like this article please do like us on facebook as well, www.facebook.com/sqlservergeeks
Good job done...
This is a well known feature, but how about discussing when you don't know the exact time and user says maybe an hour ago I did it? How would someone go about finding when it happened? What if it was corruption, how would you find the time? These are more typical answers that are needed.
Leave a comment