0% found this document useful (0 votes)
29 views6 pages

AlwaysOn Availability Group Failover Guide

Uploaded by

Abraham Getachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

AlwaysOn Availability Group Failover Guide

Uploaded by

Abraham Getachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Always On Availability Group Failover Guide with Scripts

1. Set Up an Alert for Always On Failover:

This T-SQL script creates an alert for Always On Failover using SQL Server Agent.

### Notes:

- Replace **YourAGDatabaseName** with your Availability Group database name.

- Replace **DBA_Operator** with the SQL Server Agent operator name.

- Notification method:

- 1: Email

- 2: Pager

- 4: NetSend

T-SQL Script:

-----------------------------------------------------

USE [msdb];

GO

EXEC sp_add_alert

@name = N'AlwaysOn Failover Alert',

@message_id = 1480, -- Database Mirroring State Change

@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',

@database_name = N'YourAGDatabaseName', -- Replace with your AG database name

@job_id = NULL;

GO

EXEC msdb.dbo.sp_add_notification

@alert_name = N'AlwaysOn Failover Alert',

@operator_name = N'DBA_Operator', -- Replace with your operator name

@notification_method = 1; -- Replace with desired notification method

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:

- Replace **YourDatabaseName** with your database name.

- Replace **C:\BackupFolder** with your desired backup path.

T-SQL Script:

-----------------------------------------------------

USE [msdb];

GO

EXEC sp_add_job
@job_name = N'Transaction Log Backup After Failover';

EXEC sp_add_jobstep

@job_name = N'Transaction Log Backup After Failover',

@step_name = N'Check Primary Replica and Backup T-Logs',

@subsystem = N'TSQL',

@command = N'

DECLARE @IsPrimary BIT;

SET @IsPrimary = 0;

IF (SELECT role_desc FROM sys.dm_hadr_availability_replica_states ars

JOIN sys.availability_replicas ar ON ars.replica_id = ar.replica_id

WHERE ars.is_local = 1) = ''PRIMARY''

BEGIN

SET @IsPrimary = 1;

END

IF @IsPrimary = 1

BEGIN

BACKUP LOG [YourDatabaseName] -- Replace with your database name

TO DISK = N''C:\BackupFolder\YourDatabaseName_TLog.trn'' -- Replace with your backup

path

WITH NOFORMAT, NOINIT, NAME = N''YourDatabaseName-TLogBackup'',

STATS = 10;

END

',

@database_name = N'YourDatabaseName', -- Replace with your database name

@on_success_action = 1,
@on_fail_action = 2;

EXEC sp_add_jobschedule

@job_name = N'Transaction Log Backup After Failover',

@name = N'Failover Schedule',

@freq_type = 1, -- One-time schedule

@active_start_time = 150000;

EXEC sp_add_jobserver

@job_name = N'Transaction Log Backup After Failover';

GO

-----------------------------------------------------

3. Copy Transaction Log Backups to Original Primary:

Use this PowerShell script to copy backups from the new primary server to the original primary

server.

### Notes:

- Replace **YourDatabaseName** with your database name.

- Replace **OriginalPrimaryServer** with the network path of the original primary server.

PowerShell Script:

-----------------------------------------------------

$source = "C:\BackupFolder\YourDatabaseName_TLog.trn" # Replace with your backup file path

$destination = "\\OriginalPrimaryServer\BackupShare\" # Replace with the network path of your


original primary server

Copy-Item $source -Destination $destination -Recurse

-----------------------------------------------------

4. GUI Instructions for Setting Up Alerts:

If you prefer using the GUI instead of T-SQL for setting up the Always On failover alert:

### Step-by-Step Guide:

1. Open SQL Server Management Studio (SSMS).

2. Navigate to **SQL Server Agent** -> **Alerts**.

3. Right-click on **Alerts** and choose **New Alert**.

4. In the General tab:

- Name the alert (e.g., AlwaysOn Failover Alert).

- Set **Type** to "SQL Server performance condition alert".

- Set **Database Mirroring State Change** as the message ID.

5. In the Response tab:

- Choose to notify the SQL Agent Operator.

6. Save the alert.

5. GUI Instructions for Setting Up SQL Agent Jobs:

To create the job for checking the primary replica role and performing transaction log backups:

### Step-by-Step Guide:


1. Open SQL Server Management Studio (SSMS).

2. Navigate to **SQL Server Agent** -> **Jobs**.

3. Right-click on **Jobs** and choose **New Job**.

4. In the General tab:

- Name the job (e.g., Transaction Log Backup After Failover).

5. In the Steps tab:

- Add a new step with the provided T-SQL command that checks for the primary replica and

performs the backup.

6. In the Schedules tab:

- Set up a one-time schedule after the failover event.

7. Save the job.

You might also like