11 March 2013

Logshipping configuration using Script




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
011.@database = N'AdventureWorks' ,
012.@backup_directory = N'c:\backup\log_shipping' ,
013.@backup_share = N'\\primary\log_shipping' ,
014.@backup_job_name = N'LSBackup_AdventureWorks' ,
016.@monitor_server = N'PRIMARY' ,
021.@backup_job_id = @LS_BackupJobId OUTPUT ,
022.@primary_id = @LS_PrimaryId OUTPUT ,
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
034.@schedule_name = N'LSBackupSchedule_PRIMARY1' ,
035.@enabled = 1 ,
041.@active_start_date = 20080503 ,
042.@active_end_date = 99991231 ,
044.@active_end_time = 235900 ,
045.@schedule_uid = @LS_BackUpScheduleUID OUTPUT ,
046.@schedule_id = @LS_BackUpScheduleID OUTPUT
047. 
048.EXEC msdb.dbo.sp_attach_schedule
049.@job_id = @LS_BackupJobId ,
050.@schedule_id = @LS_BackUpScheduleID
051. 
052.-- enable the transaction log backup job:
053.EXEC msdb.dbo.sp_update_job
054.@job_id = @LS_BackupJobId ,
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
060.@primary_database = N'AdventureWorks' ,
061.@secondary_server = N'PRIMARY\standby' ,
062.@secondary_database = N'AdventureWorks' ,
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
079.@primary_server = N'PRIMARY' ,
080.@primary_database = N'AdventureWorks' ,
081.@backup_source_directory = N'\\primary\log_shipping' ,
082.@backup_destination_directory = N'c:\backup\log_shipping_dest' ,
083.@copy_job_name = N'LSCopy_PRIMARY_AdventureWorks' ,
084.@restore_job_name = N'LSRestore_PRIMARY_AdventureWorks' ,
086.@monitor_server = N'PRIMARY' ,
089.@copy_job_id = @LS_Secondary__CopyJobId OUTPUT ,
090.@restore_job_id = @LS_Secondary__RestoreJobId OUTPUT ,
091.@secondary_id = @LS_Secondary__SecondaryId OUTPUT
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
102.@schedule_name = N'DefaultCopyJobSchedule' ,
103.@enabled = 1 ,
109.@active_start_date = 20080503 ,
110.@active_end_date = 99991231 ,
112.@active_end_time = 235900 ,
113.@schedule_uid = @LS_SecondaryCopyJobScheduleUID OUTPUT ,
114.@schedule_id = @LS_SecondaryCopyJobScheduleID OUTPUT
115. 
116.EXEC msdb.dbo.sp_attach_schedule
117.@job_id = @LS_Secondary__CopyJobId ,
118.@schedule_id = @LS_SecondaryCopyJobScheduleID
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
126.@schedule_name = N'DefaultRestoreJobSchedule' ,
127.@enabled = 1 ,
133.@active_start_date = 20080503 ,
134.@active_end_date = 99991231 ,
136.@active_end_time = 235900 ,
137.@schedule_uid = @LS_SecondaryRestoreJobScheduleUID OUTPUT ,
138.@schedule_id = @LS_SecondaryRestoreJobScheduleID OUTPUT
139. 
140.EXEC msdb.dbo.sp_attach_schedule
141.@job_id = @LS_Secondary__RestoreJobId ,
142.@schedule_id = @LS_SecondaryRestoreJobScheduleID
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
151.@secondary_database = N'AdventureWorks' ,
152.@primary_server = N'PRIMARY' ,
153.@primary_database = N'AdventureWorks' ,
161.END
162. 
163.IF (@@ERROR = 0
164.AND @LS_Add_RetCode = 0)
165.BEGIN
166.EXEC msdb.dbo.sp_update_job
167.@job_id = @LS_Secondary__CopyJobId ,
169. 
170.EXEC msdb.dbo.sp_update_job
171.@job_id = @LS_Secondary__RestoreJobId ,
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