What is Transaction Log Backup in Sql Server?

What is Transaction Log Backup in Sql Server?

The first thing you need to know is that when your database recovery model is either Full or Bulked Logged, you can back up the transaction log of the database. So the first question that arises is why do we actually need to back up the transaction log? Probably the most important reason is that we want to prevent heart attacks when data loss occurs. It somehow empowers us to minimize data loss and also enables us to truncate log files. what does it mean to truncate log files? Okay to answer this question you need to know that with the Full and Bulked logged recovery model, the Transaction Log file will be marked as inactive after committing the transaction, without being truncated automatically, as it will be?waiting for a Transaction Log backup?to be performed. So we need the Backup Log in order to make log files reusable and available again. Imagine if we don't get a backup log, the log files would grow continuously until it runs out of space. When the recovery model is simple Transaction Log will be marked as inactive and truncated automatically after committing the active transaction. So you can now realize why we don't need a backup log when the recovery model is simple.

It is important to know that before getting a transaction log backup, you need to create at least one Full Backup. The?first?Transaction Log backup will take a backup for all the transactions that occurred in the database since the last Full backup.

Just know that when you get a Transaction Log Backup it would append to your full backup file.

The command for Transaction Log Backup is shown below:

BACKUP LOG [AdventureWorks2019] TO? DISK = N'D:\Work\Databases\AdventureWorks2019.bak' WITH NOFORMAT, NOINIT
NAME = N'AdventureWorks2019-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,? STATS = 10
GO;        

So as soon as you finished the first Backup log, every transaction that occurs in the database would be captured by the next Log Backup. The Full backup and all following Transaction Log Backups until a new Full backup is taken is called?Backup Chain. In the case of data loss, this Backup Chain can help us recover the database to a specific point in time. The number of Log Backups is highly pertinent to how important your data is, the size of your database and so many other factors that need to be considered. Remember the more Log Backups, the more likely you would be able to minimize data loss.

For the purpose of this article, I have created a full backup along with 2 transaction log backups.

Imagine you performed a Delete statement on a table and forgot to add a Where clause. So what can you do to make it all go away back when your data wasn't got deleted? You can revert it back via restoring the database back to the time when the deletion never happened.

No alt text provided for this image
Restoring the database

As you can see I unchecked the last Log backup since I wanted to go back to the first Transaction Log Backup.

What is a tail-log backup? A?tail-log backup?captures any log records that have not yet been backed up (the?tail of the log) to prevent work loss and to keep the log chain intact. Before you can recover a SQL Server database to its latest point in time, you must back up the tail of its transaction log. The tail-log backup will be the last backup of interest in the recovery plan for the database.

If you know exactly when the deletion process occurred, you can then set this timeline to restore the database back to that specific point in time before the DELETE statement happened:

No alt text provided for this image
Timeline
No alt text provided for this image
Timeline

Now when the process of restoring finishes, you have the whole table records back like it never actually happened. :)

I hope this article was helpful to you. Thank you for reading it.


要查看或添加评论,请登录

Sky Yaghoobi的更多文章

  • Best Practices For Sql Server Indexes

    Best Practices For Sql Server Indexes

    Most Sql Server professionals allege that creating indexes is a double-edged sword. In other words, if only we use best…

  • What are Trace Flags 1117 and 1118 In Sql Server?

    What are Trace Flags 1117 and 1118 In Sql Server?

    Trace Flag 1117 First of all, what is a trace flag? A trace flag is an advanced property with which you can set and…

    1 条评论
  • FOR XML PATH in Dynamic Pivot Table

    FOR XML PATH in Dynamic Pivot Table

    The most useful functionality of this clause is to concatenate multiple columns into a single row. You may wonder when…

社区洞察