One of the welcome
improvements in log shipping with SQL Server 2005 is the ability to script the
steps and execute them through code instead of having to use wizards, as was
required in SQL Server 2000. The following statements setup log shipping for
AdventureWorks sample database:
001./*
Execute the following statements at the SOURCE server to configure Log
Shipping
002.-- for
the database [AdventureWorks],
003.-- The
script needs to be run at the SOURCE server in the context of the [msdb]
database. Adding the Log Shipping configuration and
creating transaction log backup job on the source server */
004.DECLARE
@LS_BackupJobId
AS
UNIQUEIDENTIFIER
005.
006.DECLARE
@LS_PrimaryId
AS
UNIQUEIDENTIFIER
007.
008.DECLARE
@SP_Add_RetCode
AS
INT
009.
010.EXEC
@SP_Add_RetCode
=
master.dbo.sp_add_log_shipping_primary_database
024.
025.IF
(@@ERROR
=
0
026.AND
@SP_Add_RetCode
=
0)
027.BEGIN
028.DECLARE
@LS_BackUpScheduleUID
AS
UNIQUEIDENTIFIER
029.
030.DECLARE
@LS_BackUpScheduleID
AS
INT
031.
032.-- Add
schedule for the transaction log backup job:
033.EXEC
msdb.dbo.sp_add_schedule
047.
048.EXEC
msdb.dbo.sp_attach_schedule
051.
052.--
enable the transaction log backup job:
053.EXEC
msdb.dbo.sp_update_job
056.END
057.
058.-- add a
record to MSDB database table for the target database:
059.EXEC
master.dbo.sp_add_log_shipping_primary_secondary
064.
065./*
Execute the following statements at the TARGET server to configure Log Shipping
066.-- for
the database [AdventureWorks],
067.-- the
script needs to be run at the TARGET server in the context of the [msdb]
database. Adding the Log Shipping configuration */
068.DECLARE
@LS_Secondary__CopyJobId
AS
UNIQUEIDENTIFIER
069.
070.DECLARE
@LS_Secondary__RestoreJobId
AS
UNIQUEIDENTIFIER
071.
072.DECLARE
@LS_Secondary__SecondaryId
AS
UNIQUEIDENTIFIER
073.
074.DECLARE
@LS_Add_RetCode
AS
INT
075.
076.--
configure log shipping and create copy / restore jobs on the target:
077./* note:
if you use SQL Server authentication @monitor_security_mode parameter will be
set to 1. Login name and password will NOT be scripted - you will
have to specify them at script execution time */
078.EXEC
@LS_Add_RetCode
=
master.dbo.sp_add_log_shipping_secondary_primary
092.
093.IF
(@@ERROR
=
0
094.AND
@LS_Add_RetCode
=
0)
095.BEGIN
096.DECLARE
@LS_SecondaryCopyJobScheduleUID
AS
UNIQUEIDENTIFIER
097.
098.DECLARE
@LS_SecondaryCopyJobScheduleID
AS
INT
099.
100.--
create and attach schedule for copy job:
101.EXEC
msdb.dbo.sp_add_schedule
115.
116.EXEC
msdb.dbo.sp_attach_schedule
119.
120.DECLARE
@LS_SecondaryRestoreJobScheduleUID
AS
UNIQUEIDENTIFIER
121.
122.DECLARE
@LS_SecondaryRestoreJobScheduleID
AS
INT
123.
124.--
create and attach schedule for restore job:
125.EXEC
msdb.dbo.sp_add_schedule
139.
140.EXEC
msdb.dbo.sp_attach_schedule
143.END
144.
145.DECLARE
@LS_Add_RetCode2
AS
INT
146.
147.IF
(@@ERROR
=
0
148.AND
@LS_Add_RetCode
=
0)
149.BEGIN
150.EXEC
@LS_Add_RetCode2
=
master.dbo.sp_add_log_shipping_secondary_database
161.END
162.
163.IF
(@@ERROR
=
0
164.AND
@LS_Add_RetCode
=
0)
165.BEGIN
166.EXEC
msdb.dbo.sp_update_job
169.
170.EXEC
msdb.dbo.sp_update_job
173.END
You can script log shipping configuration during the initial setup
or at any time after setup is complete. You can also alter log shipping
configuration through database properties' dialog even if it were setup through
Transact-SQL commands.
No comments:
Post a Comment