AlwaysOn Availability Group Failover Guide
AlwaysOn Availability Group Failover Guide
This T-SQL script creates an alert for Always On Failover using SQL Server Agent.
### Notes:
- Notification method:
- 1: Email
- 2: Pager
- 4: NetSend
T-SQL Script:
-----------------------------------------------------
USE [msdb];
GO
EXEC sp_add_alert
@severity = 0,
@enabled = 1,
@delay_between_responses = 0,
@include_event_description_in = 1,
@notification_message = N'An AlwaysOn failover occurred.',
@event_description_keyword = N'failover',
@job_id = NULL;
GO
EXEC msdb.dbo.sp_add_notification
GO
-----------------------------------------------------
2. T-SQL Script to Check Replica Role and Perform Transaction Log Backup:
This script checks whether the current server is the primary replica and, if so, backs up the
transaction logs.
### Notes:
T-SQL Script:
-----------------------------------------------------
USE [msdb];
GO
EXEC sp_add_job
@job_name = N'Transaction Log Backup After Failover';
EXEC sp_add_jobstep
@subsystem = N'TSQL',
@command = N'
SET @IsPrimary = 0;
BEGIN
SET @IsPrimary = 1;
END
IF @IsPrimary = 1
BEGIN
path
STATS = 10;
END
',
@on_success_action = 1,
@on_fail_action = 2;
EXEC sp_add_jobschedule
@active_start_time = 150000;
EXEC sp_add_jobserver
GO
-----------------------------------------------------
Use this PowerShell script to copy backups from the new primary server to the original primary
server.
### Notes:
- Replace **OriginalPrimaryServer** with the network path of the original primary server.
PowerShell Script:
-----------------------------------------------------
-----------------------------------------------------
If you prefer using the GUI instead of T-SQL for setting up the Always On failover alert:
To create the job for checking the primary replica role and performing transaction log backups:
- Add a new step with the provided T-SQL command that checks for the primary replica and