Tag Archives: SQL Server DBA

Restore Database with Tail-log Backup

Restore & Recover Database with Tail Backup (SQL Server)

Table of Contents


0. What is a Tail-Log Backup?
1. Create Test Database
2. Create Table & Insert Data
3. Take FULL Backup
4. Insert More Data (Simulate Activity)
5. Take LOG Backup
6. More Transactions
7. Simulate Failure
8. Take Tail-Log Backup (Final Log Backup)
9. Listdown All Backup files
10. Restore FULL Backup
11. Restore LOG Backup
12. Restore TAIL Backup (Final Restore – Last step)
13. Verify Data



0. What is a Tail-Log Backup?

Definition: A tail-log backup captures the “tail” of the transaction log — the portion of log records that have not yet been backed up.

Purpose: It ensures no data loss and maintains the log chain for point-in-time recovery.

Usage: Taken before restoring a database, especially after failure or corruption, to secure the most recent changes.
BACKUP LOG [DatabaseName] TO DISK = 'Path\TailLog.trn' WITH NO_TRUNCATE; (If DB corrupted/Offline/Recovery Pending)

Migration: When moving a database to another server, the tail-log backup ensures no data is lost during the cutover, if planned then use BACKUP LOG [DatabaseName] TO DISK = 'Path\TailLog.trn' WITH NORECOVERY; 

Requirement: The log file (.ldf) must be available, if it’s missing or corrupted, a tail-log backup isn’t possible.


1. Create Test Database

CREATE DATABASE TEST;
GO

ALTER DATABASE TEST SET RECOVERY FULL;
GO



2. Create Table & Insert Data

USE TEST;
GO

CREATE TABLE emp (
    id INT,
    name VARCHAR(50)
);

INSERT INTO emp VALUES (1, 'SUGI');
GO



3. Take FULL Backup

BACKUP DATABASE TEST TO DISK = 'K:\BACKUP\TEST\Test_full.bak';



4. Insert More Data (Simulate Activity)

INSERT INTO emp VALUES (2, 'TEJA');
GO



5. Take LOG Backup

BACKUP LOG TEST TO DISK = 'K:\BACKUP\TEST\Test_log1.trn';

 


6. More Transactions

INSERT INTO emp VALUES (3, 'JANA');
GO



7. Simulate Failure

DISCLAIMER: The commands given below is just for learning purposes and should only be used on testing systems. I will not take any responsibility of any consequences or loss of data caused by these commands.

Now assume: Database issue / corruption / accidental restore requirement

Before starting a restore, you SHOULD take a tail-log backup (final log backup)

1. Insert Data
2. Kill SQL Server
3. Rename or Move Data File (MDF)
4. Start SQL Server
5. Database won't be accesable.
6. Take Tail-log backup (Final backup)
7. Restore Full Backup with NORECOVERY
8. Restore Log backups (All Log backups) with NORECOVERY;
9. Restore (Final restore) Tail-log backup with RECOVERY;
10. Database will come Online.
-- Generate activity

INSERT INTO emp VALUES (4, 'SOMU');
GO

-- Remove MDF (OS level)
SELECT name, physical_name FROM sys.master_files WHERE database_id = DB_ID('TEST');

Get-Service -Name 'MSSQLSERVER'

Stop-Service -Name 'MSSQLSERVER' -Force

Move-Item -Path "D:\MSSQL16.MSSQLSERVER\MSSQL\Data\TEST.mdf" -Destination "T:\SQLDATA\TEST.mdf"

Test-Path "T:\SQLDATA\TEST.mdf"
Test-Path "D:\MSSQL16.MSSQLSERVER\MSSQL\Data\TEST.mdf"

Start-Service -Name 'MSSQLSERVER'
Get-Service -Name 'MSSQLSERVER'

  



8. Take Tail-Log Backup

-- IF Database accesable  
BACKUP LOG TEST TO DISK = 'K:\BACKUP\TEST\Test_tail.trn' WITH NORECOVERY;

-- If DB is damaged (DB in SUSPECT Mode/Offline/Recovery Pending)
BACKUP LOG TEST TO DISK = 'K:\BACKUP\TEST\Test_tail.trn' WITH NO_TRUNCATE;

NORECOVERYNO_TRUNCATE
Difference
  • Puts the database into restoring state.
  • Prevents further transactions.
  • Ensures no more changes happen after backup starts, until restore completes.
  • Typically used when DB is still accessible/online.
  • Use NORECOVERY when you are planning a restore and want to freeze the database.
  • Allows log backup even if database is damaged/offline.
  • Does NOT require database to be online.
  • Skips normal checks and forces log capture.
  • Log file must be accesable, else log backup will fail.


9. Listdown All Backup files

Full Backup file: K:\BACKUP\TEST\Test_full.bak
Log Backup file 1: K:\BACKUP\TEST\Test_log1.trn


10. Restore FULL Backup

RESTORE DATABASE TEST FROM DISK = 'K:\BACKUP\TEST\Test_full.bak' WITH NORECOVERY;


11. Restore LOG Backup

RESTORE LOG TEST FROM DISK = 'K:\BACKUP\TEST\Test_log1.trn' WITH NORECOVERY;


12. Restore TAIL Backup (Final Step)

RESTORE LOG TEST FROM DISK = 'K:\BACKUP\TEST\Test_tail.trn' WITH RECOVERY;


13. Verify Data

USE TEST;
GO
SELECT * FROM emp;
 

Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.

Thank you,
Rajasekhar Amudala
Email: br8dba@gmail.com
Linkedin: https://www.linkedin.com/in/rajasekhar-amudala/

Windows Server 2022 Failover Cluster

Windows Server 2022 Failover Cluster using PowerShell
for SQL Server Always On Availability Groups

Table of Contents



0. Environment

NodeHostnameIP Address
DB Node 1NTICBPSQLSGV01192.168.2.21
DB Node 2NTICBPSQLSGV02192.168.2.22
DB Node 3NTICBPSQLSGV03192.168.2.23
DB Node 4NTICBPSQLSGV04192.168.2.24
Cluster NameNTICBPSQLSGC05192.168.2.25
DCDC01192.168.2.20

1. Prerequisites

  • All four servers must run Windows Server 2022.
  • All servers must be joined to the same Active Directory domain.
  • Ensure proper DNS name resolution between all nodes.
  • Verify network connectivity and configure static IP addresses.
  • Install the latest Windows updates and vendor drivers.

2. Install Failover Clustering Feature

Run the following command on each node as Administrator.

On NTICBPSQLSGV01

PS C:\Users\Administrator> Install-WindowsFeature -Name Failover-Clustering -IncludeManagementTools                     
Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {Failover Clustering, Remote Server Admini...


PS C:\Users\Administrator> hostname
NTICBPSQLSGV01
PS C:\Users\Administrator>
PS C:\Users\Administrator> Get-WindowsFeature -Name Failover-Clustering

Display Name                                            Name                       Install State
------------                                            ----                       -------------
[X] Failover Clustering                                 Failover-Clustering            Installed

PS C:\Users\Administrator>

On NTICBPSQLSGV02

PS C:\Users\Administrator> Install-WindowsFeature -Name Failover-Clustering -IncludeManagementTools                     
Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {Failover Clustering, Remote Server Admini...


PS C:\Users\Administrator> hostname
NTICBPSQLSGV02
PS C:\Users\Administrator> Get-WindowsFeature -Name Failover-Clustering

Display Name                                            Name                       Install State
------------                                            ----                       -------------
[X] Failover Clustering                                 Failover-Clustering            Installed


PS C:\Users\Administrator>

On NTICBPSQLSGV03

PS C:\Users\Administrator> Install-WindowsFeature -Name Failover-Clustering -IncludeManagementTools                     
Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {Failover Clustering, Remote Server Admini...


PS C:\Users\Administrator> hostname
NTICBPSQLSGV03
PS C:\Users\Administrator> Get-WindowsFeature -Name Failover-Clustering

Display Name                                            Name                       Install State
------------                                            ----                       -------------
[X] Failover Clustering                                 Failover-Clustering            Installed

PS C:\Users\Administrator>

On NTICBPSQLSGV04

PS C:\Users\Administrator> Install-WindowsFeature -Name Failover-Clustering -IncludeManagementTools                     
Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {Failover Clustering, Remote Server Admini...


PS C:\Users\Administrator> hostname
NTICBPSQLSGV04
PS C:\Users\Administrator> Get-WindowsFeature -Name Failover-Clustering

Display Name                                            Name                       Install State
------------                                            ----                       -------------
[X] Failover Clustering                                 Failover-Clustering            Installed


PS C:\Users\Administrator>
✔ Failover Clustering feature installed successfully on all nodes.

 

3. Import Cluster Module and Validate Cluster Configuration

On NTICBPSQLSGV01

Load the Windows Failover Clustering PowerShell module into your current PowerShell session.
Run validation before creating the cluster:

On NTICBPSQLSGV01:

PS C:\Users\Administrator> hostname
NTICBPSQLSGV01
PS C:\Users\Administrator> Import-Module FailoverClusters
PS C:\Users\Administrator>

PS C:\Users\Administrator> Test-Cluster -Node NTICBPSQLSGV01,NTICBPSQLSGV02,NTICBPSQLSGV03,NTICBPSQLSGV04
WARNING: System Configuration - Validate Software Update Levels: The test reported some warnings..
WARNING: Network - Validate Network Communication: The test reported some warnings..
WARNING:
Test Result:
HadUnselectedTests, ClusterConditionallyApproved
Testing has completed for the tests you selected. You should review the warnings in the Report.  A cluster solution is
supported by Microsoft only if you run all cluster validation tests, and all tests succeed (with or without warnings).
Test report file path: C:\Users\Administrator\AppData\Local\Temp\2\Validation Report 2025.12.29 At 18.28.44.htm

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        12/29/2025   6:34 PM        1346352 Validation Report 2025.12.29 At 18.28.44.htm


PS C:\Users\Administrator>

Note: Warnings are acceptable, but errors must be resolved.
Always review the cluster validation report.

 

4. Create the Cluster

New-Cluster `
 -Name NTICBPSQLSGC05 `
 -Node NTICBPSQLSGV01,NTICBPSQLSGV02,NTICBPSQLSGV03,NTICBPSQLSGV04 `
 -StaticAddress 192.168.2.25 `
 -NoStorage

PS C:\Users\Administrator> New-Cluster `
>>  -Name NTICBPSQLSGC05 `
>>  -Node NTICBPSQLSGV01,NTICBPSQLSGV02,NTICBPSQLSGV03,NTICBPSQLSGV04 `
>>  -StaticAddress 192.168.2.25 `
>>  -NoStorage

Name
----
NTICBPSQLSGC05


PS C:\Users\Administrator>
✔ Cluster NTICBPSQLSGC05 created successfully.

 

5. Verify Cluster Configuration

Cluster Name

PS C:\Users\Administrator> Get-Cluster

Name
----
NTICBPSQLSGC05


PS C:\Users\Administrator>

Cluster Nodes

PS C:\Users\Administrator> Get-ClusterNode

Name           State Type
----           ----- ----
NTICBPSQLSGV01 Up    Node
NTICBPSQLSGV02 Up    Node
NTICBPSQLSGV03 Up    Node
NTICBPSQLSGV04 Up    Node


PS C:\Users\Administrator>

Cluster Resources

PS C:\Users\Administrator> Get-ClusterResource

Name               State  OwnerGroup    ResourceType
----               -----  ----------    ------------
Cluster IP Address Online Cluster Group IP Address
Cluster Name       Online Cluster Group Network Name


PS C:\Users\Administrator>

Cluster Network

PS C:\Users\Administrator> Get-ClusterNetwork | Format-Table Name, Address, Role, State

Name              Address                 Role State
----              -------                 ---- -----
Cluster Network 1 192.168.2.0 ClusterAndClient    Up


PS C:\Users\Administrator>
✔ All cluster components are online and healthy.

6. Add Witness

In this test lab environment, the File Share Witness (FSW) is hosted on the Domain Controller (DC).

Using a Domain Controller as File Share Witness is NOT recommended for production.

  • In production, the File Share Witness should be hosted on:
    • A dedicated file server
    • Azure Cloud Witness (recommended)
  • The witness folder must have Full Control for:
    • The Windows Failover Cluster computer object
    • SQL Server Always On cluster name object
       

Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.

Thank you,
Rajasekhar Amudala
Email: br8dba@gmail.com
Linkedin: https://www.linkedin.com/in/rajasekhar-amudala/

Log Shipping (SQL Server 2022)

Log Shipping – SQL Server 2022 (Complete Guide)

Table of Contents


  1. Overview
  2. Oracle Data Guard vs SQL Server Log Shipping
  3. Log Shipping Architecture
  4. Environment Details
  5. Prerequisites
  6. Step-by-Step Configuration
  7. Verification & Testing

Log Shipping (SQL Server 2022)

Overview

Log Shipping copies transaction log backups from the Primary server to one or more Secondary servers and restores them, keeping databases synchronized for Disaster Recovery (DR) or Read-Only Reporting scenarios.

Oracle Data Guard vs SQL Server Log Shipping

Oracle Data Guard
Redo transport is internal, continuous, and process-driven (LGWR → RFS → MRP).

SQL Server Log Shipping
Log transport is external, file-based, and job-driven (Backup → Copy → Restore).

Log Shipping Architecture

Log Shipping is a disaster recovery (DR) solution where:

  • Primary Server takes transaction log backups and saves them to a shared network path.
  • Secondary Server copies and restores those logs (STANDBY or NORECOVERY mode).
  • No shared storage is required — only T-Log backups over a network share.

Environment Details

ItemPrimarySecondary
HostnameSGMSQL1SGMSQL2
SQL Server VersionSQL Server 2022SQL Server 2022
SQL EditionEnterprise / StandardEnterprise / Standard
SQL Server Log on AsRAJASEKHAR\MSAPGEBSGSQL$RAJASEKHAR\MSAPGEBSGSQL$
Instance NameMSSQLSERVERMSSQLSERVER
Database NameSALESSALES
Recovery ModelFULLFULL
DR LocationDC / Region 1DR / Region 2
T-Log Backup Path\\SGMSQL1\Backup_LogShip
Copy Destination PathN/AD:\LogShipping\Copy

Prerequisites

  • SQL Server 2022 Standard or Enterprise
  • Same database name (collation recommended to be same)
  • Database must be in FULL recovery model
  • SQL Server Agent must be running on Primary & Secondary
  • Service accounts must have Read/Write access to the shared folder

Step-by-Step Configuration

Step 1: Prepare the Primary Database

Right-click database → Properties → Options → Set Recovery Model = FULL.

Recovery Model

Take a full database backup.

Full Backup

Step 2: Enable Log Shipping

Database → Properties → Transaction Log Shipping →
Enable this as a primary database.

Enable Log Shipping

Step 3: Configure Backup Settings

  • Network path
  • Local backup folder (if you are storing Tlog Backups in local drive )
  • Backup job schedule (e.g. every 5 minutes)

Backup Settings

Step 4: Add Secondary Database

Initialize Secondary → Restore from backup (recommended).

Initialize Secondary

Configure Copy and Restore jobs.

Copy Job
Restore Job

Step 5: Finish Configuration

Click OK. SQL Server will automatically create all required jobs.

Finish

Verification & Testing

Jobs will be created automatically:

  • Primary: LSBackup_SALES, LSAlert_SGMSQL1
  • Secondary: LSCopy, LSRestore, LSAlert_SGMSQL2

Secondary database should be in Standby / Read-Only mode.

Standby Mode

Testing

Create a table on Primary and verify it appears on Secondary after restore.

On Primary :

On Standby : (After 5 Minutes)

Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.

Thank you,
Rajasekhar Amudala
Email: br8dba@gmail.com
Linkedin: https://www.linkedin.com/in/rajasekhar-amudala/