:CONNECT SQL2K12-SVR1

USE master;
GO
CREATE PROCEDURE BackupTransactionLogs (@dbname sysname, @backuppath nvarchar(256))
AS
BEGIN

IF sys.fn_hadr_backup_is_preferred_replica( @dbname ) <> 1 
BEGIN
	GOTO Quit;
END
-- If this is the preferred replica, continue to do the backup.
DECLARE @Filename nvarchar(512) = @backuppath+@DBname+'_LOG_'+ REPLACE(CONVERT(VARCHAR(10), GETDATE(), 112), '/', '') + REPLACE(CONVERT(VARCHAR(10), GETDATE(), 108) , ':', '') + '.trn'

DECLARE @SQLcmd NVARCHAR(MAX) = 'BACKUP LOG ' + QUOTENAME(@dbname) + ' TO DISK = ''' + @FileName + ''' ;'

EXECUTE(@SQLcmd);

Quit:

END
GO
USE [msdb]
GO
EXEC  msdb.dbo.sp_add_job @job_name=N'ConfigDB Log Backup', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=2, 
		@notify_level_netsend=2, 
		@notify_level_page=2, 
		@delete_level=0, 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'SQLSKILLSDEMOS\administrator';
GO
EXEC msdb.dbo.sp_add_jobserver @job_name=N'ConfigDB Log Backup', @server_name = @@SERVERNAME
GO
EXEC msdb.dbo.sp_add_jobstep @job_name=N'ConfigDB Log Backup', @step_name=N'ConfigDB Log Backup', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_fail_action=2, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'EXECUTE dbo.BackupTransactionLogs ''ConfigDB'', ''\\SQL2K12-SVR1\Backup\''', 
		@database_name=N'master', 
		@flags=0
GO
EXEC msdb.dbo.sp_update_job @job_name=N'ConfigDB Log Backup', 
		@enabled=1, 
		@start_step_id=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=2, 
		@notify_level_netsend=2, 
		@notify_level_page=2, 
		@delete_level=0, 
		@description=N'', 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'SQLSKILLSDEMOS\administrator', 
		@notify_email_operator_name=N'', 
		@notify_netsend_operator_name=N'', 
		@notify_page_operator_name=N''
GO
EXEC msdb.dbo.sp_add_jobschedule @job_name=N'ConfigDB Log Backup', @name=N'ConfigDB Transaction Log Backup', 
		@enabled=1, 
		@freq_type=4, 
		@freq_interval=1, 
		@freq_subday_type=4, 
		@freq_subday_interval=2, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=1, 
		@active_start_date=20120324, 
		@active_end_date=99991231, 
		@active_start_time=0, 
		@active_end_time=235959
GO

:CONNECT SQL2K12-SVR2

USE master;
GO
CREATE PROCEDURE BackupTransactionLogs (@dbname sysname, @backuppath nvarchar(256))
AS
BEGIN

IF sys.fn_hadr_backup_is_preferred_replica( @dbname ) <> 1 
BEGIN
	GOTO Quit;
END
-- If this is the preferred replica, continue to do the backup.
DECLARE @Filename nvarchar(512) = @backuppath+@DBname+'_LOG_'+ REPLACE(CONVERT(VARCHAR(10), GETDATE(), 112), '/', '') + REPLACE(CONVERT(VARCHAR(10), GETDATE(), 108) , ':', '') + '.trn'

DECLARE @SQLcmd NVARCHAR(MAX) = 'BACKUP LOG ' + QUOTENAME(@dbname) + ' TO DISK = ''' + @FileName + ''' ;'

EXECUTE(@SQLcmd);

Quit:

END
GO
USE [msdb]
GO
EXEC  msdb.dbo.sp_add_job @job_name=N'ConfigDB Log Backup', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=2, 
		@notify_level_netsend=2, 
		@notify_level_page=2, 
		@delete_level=0, 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'SQLSKILLSDEMOS\administrator';
GO
EXEC msdb.dbo.sp_add_jobserver @job_name=N'ConfigDB Log Backup', @server_name = @@SERVERNAME
GO
EXEC msdb.dbo.sp_add_jobstep @job_name=N'ConfigDB Log Backup', @step_name=N'ConfigDB Log Backup', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_fail_action=2, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'EXECUTE dbo.BackupTransactionLogs ''ConfigDB'', ''\\SQL2K12-SVR1\Backup\''', 
		@database_name=N'master', 
		@flags=0
GO
EXEC msdb.dbo.sp_update_job @job_name=N'ConfigDB Log Backup', 
		@enabled=1, 
		@start_step_id=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=2, 
		@notify_level_netsend=2, 
		@notify_level_page=2, 
		@delete_level=0, 
		@description=N'', 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'SQLSKILLSDEMOS\administrator', 
		@notify_email_operator_name=N'', 
		@notify_netsend_operator_name=N'', 
		@notify_page_operator_name=N''
GO
EXEC msdb.dbo.sp_add_jobschedule @job_name=N'ConfigDB Log Backup', @name=N'ConfigDB Transaction Log Backup', 
		@enabled=1, 
		@freq_type=4, 
		@freq_interval=1, 
		@freq_subday_type=4, 
		@freq_subday_interval=2, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=1, 
		@active_start_date=20120324, 
		@active_end_date=99991231, 
		@active_start_time=0, 
		@active_end_time=235959
GO

:CONNECT SQL2K12-SVR3

USE master;
GO
CREATE PROCEDURE BackupTransactionLogs (@dbname sysname, @backuppath nvarchar(256))
AS
BEGIN

IF sys.fn_hadr_backup_is_preferred_replica( @dbname ) <> 1 
BEGIN
	GOTO Quit;
END
-- If this is the preferred replica, continue to do the backup.
DECLARE @Filename nvarchar(512) = @backuppath+@DBname+'_LOG_'+ REPLACE(CONVERT(VARCHAR(10), GETDATE(), 112), '/', '') + REPLACE(CONVERT(VARCHAR(10), GETDATE(), 108) , ':', '') + '.trn'

DECLARE @SQLcmd NVARCHAR(MAX) = 'BACKUP LOG ' + QUOTENAME(@dbname) + ' TO DISK = ''' + @FileName + ''' ;'

EXECUTE(@SQLcmd);

Quit:

END
GO
USE [msdb]
GO
EXEC  msdb.dbo.sp_add_job @job_name=N'ConfigDB Log Backup', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=2, 
		@notify_level_netsend=2, 
		@notify_level_page=2, 
		@delete_level=0, 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'SQLSKILLSDEMOS\administrator';
GO
EXEC msdb.dbo.sp_add_jobserver @job_name=N'ConfigDB Log Backup', @server_name = @@SERVERNAME
GO
EXEC msdb.dbo.sp_add_jobstep @job_name=N'ConfigDB Log Backup', @step_name=N'ConfigDB Log Backup', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_fail_action=2, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'EXECUTE dbo.BackupTransactionLogs ''ConfigDB'', ''\\SQL2K12-SVR1\Backup\''', 
		@database_name=N'master', 
		@flags=0
GO
EXEC msdb.dbo.sp_update_job @job_name=N'ConfigDB Log Backup', 
		@enabled=1, 
		@start_step_id=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=2, 
		@notify_level_netsend=2, 
		@notify_level_page=2, 
		@delete_level=0, 
		@description=N'', 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'SQLSKILLSDEMOS\administrator', 
		@notify_email_operator_name=N'', 
		@notify_netsend_operator_name=N'', 
		@notify_page_operator_name=N''
GO
EXEC msdb.dbo.sp_add_jobschedule @job_name=N'ConfigDB Log Backup', @name=N'ConfigDB Transaction Log Backup', 
		@enabled=1, 
		@freq_type=4, 
		@freq_interval=1, 
		@freq_subday_type=4, 
		@freq_subday_interval=2, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=1, 
		@active_start_date=20120324, 
		@active_end_date=99991231, 
		@active_start_time=0, 
		@active_end_time=235959
GO
