Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
148 views

VB Script For Making Manual SQL Mirror Failover Automatic

This VB script automatically switches the SQL mirror database to the principal database if communication is lost between the primary and redundant databases. The script runs regularly on both the primary and redundant servers to check the mirroring status and resume or force failover of the mirrored DB_Config database as needed. If a failure is detected, the script alters the database on the current server to become the principal database and replace the failed partner.

Uploaded by

nithinvn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

VB Script For Making Manual SQL Mirror Failover Automatic

This VB script automatically switches the SQL mirror database to the principal database if communication is lost between the primary and redundant databases. The script runs regularly on both the primary and redundant servers to check the mirroring status and resume or force failover of the mirrored DB_Config database as needed. If a failure is detected, the script alters the database on the current server to become the principal database and replace the failed partner.

Uploaded by

nithinvn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

The following vb script will automatically switchover the SQL mirror database as the principal database as soon as there

is a communication break between the two databases. Here USPSV1 is the Primary PC name and USPSV2 is the redundant PC name. USPSV1\SBC and USPSV2\SBC is the name of the database engine in both the servers. And DB_Config is the name of the database which is configured for SQL Mirroring in Manual Failover mode. Run the below script at regular intervals in the Primary server PC i.e. USPSV1.

Option Explicit Function action Dim objConnection1,RETTBL1,objConnection2,RETTBL2,RETTBL3 Dim objRecordset1,objRecordset2 Dim strDBServer1,strDBConnection1,strDBServer2,strDBConnection2 strDBServer1 = "USPSV1\SBC" strDBServer2 = "USPSV2\SBC" on error resume next Set objConnection1 = CreateObject("ADODB.Connection") strDBConnection1 = "PROVIDER= SQLOLEDB;DATA SOURCE = " & strDBServer1 & "; database = 'master' ;TRUSTED_CONNECTION=YES" objConnection1.Open strDBConnection1 Set objConnection2 = CreateObject("ADODB.Connection") strDBConnection2 = "PROVIDER= SQLOLEDB;DATA SOURCE = " & strDBServer2 & "; database = 'master' ;TRUSTED_CONNECTION=YES" objConnection2.Open strDBConnection2 If err.number<>0 Then 'MsgBox Hex(err.number) & vbCrLf & err.description Set RETTBL1 = objConnection1.Execute("SELECT db.name, m.mirroring_role_desc FROM sys.database_mirroring m JOIN sys.databases db ON db.database_id = m.database_id WHERE db.name = 'DB_Config'") If RETTBL1="MIRROR" Then objConnection1.Execute("ALTER DATABASE DB_Config Set PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS") End If else Set RETTBL1 = objConnection1.Execute("SELECT db.name, m.mirroring_role_desc FROM sys.database_mirroring m JOIN sys.databases db ON db.database_id = m.database_id WHERE db.name = 'DB_Config'") Set RETTBL2 = objConnection2.Execute("SELECT db.name, m.mirroring_role_desc FROM sys.database_mirroring m JOIN sys.databases db ON db.database_id = m.database_id WHERE db.name = 'DB_Config'") Set RETTBL3 = objConnection1.Execute("SELECT db.name, m.mirroring_state_desc FROM sys.database_mirroring m JOIN sys.databases db ON db.database_id = m.database_id WHERE db.name = 'DB_Config'") If err.number<>0 Then

'MsgBox Hex(err.number) & vbCrLf & err.description Else If RETTBL3="SUSPENDED" Then objConnection1.Execute("ALTER DATABASE DB_Config SET PARTNER RESUME") End If 'MsgBox "STATUS: " & RETTBL1(1).value RETTBL1.close 'MsgBox "STATUS: " & RETTBL2(1).value RETTBL2close end if Set RETTBL1=Nothing objConnection1.close Set RETTBL2=Nothing objConnection2.close end if on error goto 0 Set objConnection1 = Nothing Set objConnection2 = Nothing End Function

And run the below vb script at regular intervals in the Redundant server PC i.e. USPSV2.

Option Explicit Function action Dim objConnection1,RETTBL1,objConnection2,RETTBL2,RETTBL3 Dim objRecordset1,objRecordset2 Dim strDBServer1,strDBConnection1,strDBServer2,strDBConnection2 strDBServer1 = "USPSV1\SBC" strDBServer2 = "USPSV2\SBC" on error resume next Set objConnection1 = CreateObject("ADODB.Connection") strDBConnection1 = "PROVIDER= SQLOLEDB;DATA SOURCE = " & strDBServer1 & "; database = 'master' ;TRUSTED_CONNECTION=YES" objConnection1.Open strDBConnection1 Set objConnection2 = CreateObject("ADODB.Connection") strDBConnection2 = "PROVIDER= SQLOLEDB;DATA SOURCE = " & strDBServer2 & "; database = 'master' ;TRUSTED_CONNECTION=YES" objConnection2.Open strDBConnection2 If err.number<>0 Then 'MsgBox Hex(err.number) & vbCrLf & err.description

Set RETTBL2 = objConnection2.Execute("SELECT db.name, m.mirroring_role_desc FROM sys.database_mirroring m JOIN sys.databases db ON db.database_id = m.database_id WHERE db.name = 'DB_Config'") If RETTBL2="MIRROR" Then objConnection2.Execute("ALTER DATABASE DB_Config Set PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS") End If else Set RETTBL1 = objConnection1.Execute("SELECT db.name, m.mirroring_role_desc FROM sys.database_mirroring m JOIN sys.databases db ON db.database_id = m.database_id WHERE db.name = 'DB_Config'") Set RETTBL2 = objConnection2.Execute("SELECT db.name, m.mirroring_role_desc FROM sys.database_mirroring m JOIN sys.databases db ON db.database_id = m.database_id WHERE db.name = 'DB_Config'") Set RETTBL3 = objConnection2.Execute("SELECT db.name, m.mirroring_state_desc FROM sys.database_mirroring m JOIN sys.databases db ON db.database_id = m.database_id WHERE db.name = 'DB_Config'") If err.number<>0 Then 'MsgBox Hex(err.number) & vbCrLf & err.description Else If RETTBL3="SUSPENDED" Then objConnection2.Execute("ALTER DATABASE DB_Config SET PARTNER RESUME") End If 'MsgBox "STATUS: " & RETTBL1(1).value RETTBL1.close 'MsgBox "STATUS: " & RETTBL2(1).value RETTBL2close end if Set RETTBL1=Nothing objConnection1.close Set RETTBL2=Nothing objConnection2.close end if on error goto 0 Set objConnection1 = Nothing Set objConnection2 = Nothing End Function

You might also like