Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.3 KiB

  1. ' Script: createBackupSched.vbs
  2. ' Description: Implements backup schedule for UDDI database
  3. ' Author: L. Roger Doherty ([email protected])
  4. ' Date: 10/11/00
  5. ' Changes:
  6. ' 11-13-2000: lrdohert - added WITH INIT option to backup jobs as per bug 712
  7. Option Explicit
  8. Dim oXML
  9. Dim oSQLServer, oBackupDevice, oJobServer, oJob, oJobSchedule, oJobStep
  10. Dim strServer, strDatabase, strBackupDir
  11. Dim strDbBackupDevName, strDbJobName, strBaseLogBackupDevName, strBaseLogJobName
  12. Dim dtNow
  13. strDbBackupDevName = "UDDI_DB_BACKUP_DEV"
  14. strDbJobName = "UDDI_DB_BACKUP_JOB"
  15. strBaseLogBackupDevName = "UDDI_LOG_BACKUP_DEV"
  16. strBaseLogJobName = "UDDI_LOG_BACKUP_JOB"
  17. WScript.Echo "Starting createBackupSched.vbs exection....."
  18. Err.Clear
  19. If WScript.arguments.Count <> 3 Then
  20. WScript.Echo "Msg: Invalid number of arguments passed."
  21. WScript.Quit
  22. End If
  23. strServer = WScript.Arguments(0)
  24. strDatabase = WScript.Arguments(1)
  25. strBackupDir = WScript.Arguments(2)
  26. ' Connect to SQL Server
  27. Set oSQLServer = CreateObject("SQLDMO.SQLServer2")
  28. With oSQLServer
  29. .LoginSecure = True
  30. .Connect strServer
  31. End With
  32. ' Delete old backup devices
  33. For Each oBackupDevice In oSQLServer.BackupDevices
  34. If oBackupDevice.Name = strDbBackupDevName Then
  35. oBackupDevice.Remove
  36. End If
  37. Next
  38. ' Create new database backup device
  39. Set oBackupDevice = CreateObject("SQLDMO.BackupDevice")
  40. With oBackupDevice
  41. .Name = strDbBackupDevName
  42. .Type = 2 ' SQLDMO_Device_DiskDump
  43. .PhysicalLocation = strBackupDir + "\" + strDbBackupDevName + ".bak"
  44. End With
  45. oSQLServer.BackupDevices.Add(oBackupDevice)
  46. ' Create transaction log backup devices
  47. CreateLogDumpDevices strBackupDir, strBaseLogBackupDevName, 23
  48. '
  49. ' Connect to SQL Agent
  50. '
  51. Set oJobServer = oSQLServer.JobServer
  52. ' Check to see if SQL Agent is running
  53. If oJobServer.Status <> 1 Then
  54. WScript.Echo "Msg: SQL Agent is not running. Cannot create backup schedule."
  55. WScript.Quit
  56. End If
  57. ' Delete old jobs if they exist
  58. For Each oJob In oJobServer.Jobs
  59. If oJob.Name = strDbJobName Then
  60. oJob.Remove
  61. End If
  62. Next
  63. ' Create new Database Backup Job
  64. Set oJob = CreateObject("SQLDMO.Job")
  65. With oJob
  66. .Name = strDbJobName
  67. End With
  68. oJobServer.Jobs.Add(oJob)
  69. Set oJob=oJobServer.Jobs(strDbJobName)
  70. Set oJobSchedule = CreateObject("SQLDMO.JobSchedule")
  71. dtNow = Now()
  72. With oJobSchedule
  73. .Name = "Daily"
  74. .Schedule.ActiveStartDate=(DatePart("yyyy",dtNow) * 10000) + (DatePart("m",dtNow) * 100) + DatePart("d",dtNow)
  75. .Schedule.ActiveStartTimeOfDay=000000
  76. .Schedule.FrequencyType=4 ' SQLDMOFreq_Daily
  77. .Schedule.FrequencyInterval=1
  78. End With
  79. oJob.JobSchedules.Add(oJobSchedule)
  80. Set oJobStep = CreateObject("SQLDMO.JobStep")
  81. With oJobStep
  82. .Name="BACKUP DATABASE"
  83. .Command="BACKUP DATABASE " + strDatabase + " TO " + strDbBackupDevName + " WITH INIT"
  84. .StepID=1
  85. End With
  86. oJob.JobSteps.Add(oJobStep)
  87. oJob.ApplyToTargetServer "(local)"
  88. ' Create the backup log jobs
  89. CreateLogBackupJobs strDatabase, strBaseLogJobName, strBaseLogBackupDevName, 23
  90. ' Run the database backup job
  91. Set oJob = oJobServer.Jobs(strDbJobName)
  92. oJob.Invoke
  93. WScript.Echo "Ending createBackupSched.vbs exection......"
  94. Sub CreateLogDumpDevices(strBackupDir, strBaseName, intNumLogs)
  95. Dim i
  96. Dim strI
  97. For i = 1 to intNumLogs
  98. If Len(CStr(i)) = 1 Then
  99. strI = "0" + CStr(i)
  100. Else
  101. strI = Cstr(i)
  102. End If
  103. ' Delete backup device if it exists
  104. For Each oBackupDevice In oSQLServer.BackupDevices
  105. If oBackupDevice.Name = (strBaseName + "_" + strI) Then
  106. oBackupDevice.Remove
  107. End If
  108. Next
  109. Set oBackupDevice = CreateObject("SQLDMO.BackupDevice")
  110. With oBackupDevice
  111. .Name = strBaseName + "_" + strI
  112. .Type = 2 ' SQLDMO_Device_DiskDump
  113. .PhysicalLocation = strBackupDir + "\" + strBaseName + "_" + strI + ".bak"
  114. End With
  115. oSQLServer.BackupDevices.Add(oBackupDevice)
  116. Next
  117. End Sub
  118. Sub CreateLogBackupJobs(strDatabase, strBaseJobName, strBaseDevName, intNumJobs)
  119. Dim i
  120. Dim strJobName, strDevName
  121. Dim dtNow
  122. For i = 1 to intNumJobs
  123. If Len(CStr(i)) = 1 Then
  124. strJobName = strBaseJobName + "_0" + CStr(i)
  125. strDevName = strBaseDevName + "_0" + CStr(i)
  126. Else
  127. strJobName = strBaseJobName + "_" + CStr(i)
  128. strDevName = strBaseDevName + "_" + CStr(i)
  129. End If
  130. ' Delete old jobs if they exist
  131. For Each oJob In oJobServer.Jobs
  132. If oJob.Name = strJobName Then
  133. oJob.Remove
  134. End If
  135. Next
  136. ' Create new Log Backup Job
  137. Set oJob = CreateObject("SQLDMO.Job")
  138. With oJob
  139. .Name = strJobName
  140. End With
  141. oJobServer.Jobs.Add(oJob)
  142. Set oJob=oJobServer.Jobs(strJobName)
  143. Set oJobSchedule = CreateObject("SQLDMO.JobSchedule")
  144. dtNow = Now()
  145. With oJobSchedule
  146. .Name = "Daily"
  147. .Schedule.ActiveStartDate=(DatePart("yyyy",dtNow) * 10000) + (DatePart("m",dtNow) * 100) + DatePart("d",dtNow)
  148. .Schedule.ActiveStartTimeOfDay=(i * 10000)
  149. .Schedule.FrequencyType=4 ' SQLDMOFreq_Daily
  150. .Schedule.FrequencyInterval=1
  151. End With
  152. oJob.JobSchedules.Add(oJobSchedule)
  153. Set oJobStep = CreateObject("SQLDMO.JobStep")
  154. With oJobStep
  155. .Name=strDevName
  156. .Command="BACKUP LOG " + strDatabase + " TO " + strDevName + " WITH INIT"
  157. .StepID=1
  158. End With
  159. oJob.JobSteps.Add(oJobStep)
  160. oJob.ApplyToTargetServer "(local)"
  161. Next
  162. End Sub