Skip to content

Commit

Permalink
Install-DbaMaintenanceSolution - fix scheduler, add examples (datapla…
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee authored May 22, 2023
1 parent acf82d8 commit 20ae33e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 34 deletions.
91 changes: 69 additions & 22 deletions public/Install-DbaMaintenanceSolution.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ function Install-DbaMaintenanceSolution {
If this switch is enabled, the corresponding SQL Agent Jobs will be created.
.PARAMETER AutoScheduleJobs
Scheduled jobs during an optimal time.
Scheduled jobs during an optimal time. When AutoScheduleJobs is used, this time will be used as the start time for the jobs unless a schedule already
exists in the same time slot. If so, then it will add an hour until it finds an open time slot. Defaults to 1:15 AM.
WeeklyFull will create weekly full, daily differential and 15 minute log backups.
WeeklyFull will create weekly full, daily differential and 15 minute log backups of _user_ databases.
To skip diffs, specify NoDiff in the values. To perform log backups each hour instead of every
15 minutes, specify HourlyLog in the values.
_System_ databases will always be backed up daily.
When AutoScheduleJobs is used, this time will be used as the start time for the jobs unless a schedule already
exists in the same time slot. If so, then it will add an hour until it finds an open time slot. Defaults to 1:15 AM.
Differentials will be skipped when NoDiff or DailyFull is specified.
To perform log backups each hour instead of every 15 minutes, specify HourlyLog in the values.
Recommendations can be found on Ola's site: https://ola.hallengren.com/frequently-asked-questions.html
Expand Down Expand Up @@ -148,9 +149,44 @@ function Install-DbaMaintenanceSolution {
This will create the Queue and QueueDatabase tables for uses when manually changing jobs to use the @DatabasesInParallel = 'Y' flag
.EXAMPLE
PS C:\> Install-DbaMaintenanceSolution -SqlInstance sql01 -InstallJobs -AutoScheduleJobs WeeklyFull
PS C:\> $params = @{
>> SqlInstance = "localhost"
>> InstallJobs = $true
>> CleanupTime = 720
>> AutoSchedule = "WeeklyFull"
>> }
>> Install-DbaMaintenanceSolution @params
This will create the Ola Hallengren's Solution objects and the SQL Agent Jobs.
WeeklyFull will create weekly full, daily differential and 15 minute log backups of _user_ databases.
_System_ databases will be backed up daily.
Databases will be backed up to the default location for the instance, and backups will be deleted after 720 hours (30 days).
See https://github.com/dataplat/dbatools/pull/8911 for details on job schedules.
.EXAMPLE
PS C:\> $params = @{
>> SqlInstance = "localhost"
>> InstallJobs = $true
>> CleanupTime = 720
>> AutoScheduleJobs = "DailyFull", "HourlyLog"
>> BackupLocation = "\\sql\backups"
>> StartTime = "231500"
>> }
This will create the Ola Hallengren's Solution objects and the SQL Agent Jobs. The jobs will be scheduled to run weekly full, daily differential and 15 minute log backups.
PS C:\> Install-DbaMaintenanceSolution @params
This will create the Ola Hallengren's Solution objects and the SQL Agent Jobs.
The jobs will be scheduled to run daily full user backups at 11:15pm, no differential backups will be created and hourly log backups will be made.
System databases will be backed up at 1:15 am, two hours after the user databases.
Databases will be backed up to a fileshare, and the backups will be deleted after 720 hours (30 days).
See https://blog.netnerds.net/2023/05/install-dbamaintenancesolution-now-supports-auto-scheduling/ for more information.
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Medium")]
Expand All @@ -159,7 +195,7 @@ function Install-DbaMaintenanceSolution {
[parameter(Mandatory, ValueFromPipeline)]
[DbaInstance[]]$SqlInstance,
[PSCredential]$SqlCredential,
[object]$Database = "master",
[string]$Database = "master",
[string]$BackupLocation,
[int]$CleanupTime,
[string]$OutputFileDirectory,
Expand Down Expand Up @@ -463,20 +499,32 @@ function Install-DbaMaintenanceSolution {
}
}

$fullparams = @{
SqlInstance = $server
Job = "DatabaseBackup - USER_DATABASES - FULL"
Schedule = "Weekly Full User Backup"
FrequencyType = "Weekly"
FrequencyInterval = "Sunday" # 1
StartTime = $start
Force = $true
if ("WeeklyFull" -in $AutoScheduleJobs) {
$fullparams = @{
SqlInstance = $server
Job = "DatabaseBackup - USER_DATABASES - FULL"
Schedule = "Weekly Full User Backup"
FrequencyType = "Weekly"
FrequencyInterval = "Sunday" # 1
StartTime = $start
Force = $true
}
} elseif ("DailyFull" -in $AutoScheduleJobs) {
$fullparams = @{
SqlInstance = $server
Job = "DatabaseBackup - USER_DATABASES - FULL"
Schedule = "Daily Full User Backup"
FrequencyType = "Daily"
FrequencyInterval = "EveryDay"
StartTime = $start
Force = $true
}
}

$fullschedule = New-DbaAgentSchedule @fullparams

if ($fullschedule.ActiveStartTimeOfDay) {
$systemdaily = $fullschedule.ActiveStartTimeOfDay.Add($twohours) -replace ":|\-", ""
$systemdaily = $fullschedule.ActiveStartTimeOfDay.Add($twohours) -replace ":|\-|1\.", ""
} else {
$systemdaily = "031500"
}
Expand All @@ -494,7 +542,7 @@ function Install-DbaMaintenanceSolution {
$null = New-DbaAgentSchedule @fullsystemparams

if ($fullschedule.ActiveStartTimeOfDay) {
$integrity = $fullschedule.ActiveStartTimeOfDay.Subtract($twelvehours) -replace ":|\-", ""
$integrity = $fullschedule.ActiveStartTimeOfDay.Subtract($twelvehours) -replace ":|\-|1\.", ""
} else {
$integrity = "044500"
}
Expand All @@ -512,7 +560,7 @@ function Install-DbaMaintenanceSolution {
$null = New-DbaAgentSchedule @integrityparams

if ($fullschedule.ActiveStartTimeOfDay) {
$indexoptimize = $fullschedule.ActiveStartTimeOfDay.Subtract($twentyfourhours) -replace ":|\-", ""
$indexoptimize = $fullschedule.ActiveStartTimeOfDay.Subtract($twentyfourhours) -replace ":|\-|1\.", ""
} else {
$indexoptimize = "224500"
}
Expand All @@ -530,7 +578,7 @@ function Install-DbaMaintenanceSolution {

$null = New-DbaAgentSchedule @integrityparams

if ("NoDiff" -notin $AutoScheduleJobs) {
if ("NoDiff" -notin $AutoScheduleJobs -and "DailyFull" -notin $AutoScheduleJobs) {
$diffparams = @{
SqlInstance = $server
Job = "DatabaseBackup - USER_DATABASES - DIFF"
Expand All @@ -540,7 +588,6 @@ function Install-DbaMaintenanceSolution {
StartTime = $start
Force = $true
}

$null = New-DbaAgentSchedule @diffparams
}

Expand Down
12 changes: 6 additions & 6 deletions public/New-DbaAgentOperator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function New-DbaAgentOperator {
Stop-Function -Message "Please enter Saturday start time or use -Force to use defaults."
return
} elseif ($SaturdayStartTime -notmatch $RegexTime) {
Stop-Function -Message "Start time $SaturdayStartTime needs to match between '000000' and '235959'"
Stop-Function -Message "Start time $SaturdayStartTime needs to match between '000000' and '235959'. Pager Day not set."
return
}

Expand All @@ -184,7 +184,7 @@ function New-DbaAgentOperator {
Stop-Function -Message "Please enter a Saturday end time or use -Force to use defaults."
return
} elseif ($SaturdayEndTime -notmatch $RegexTime) {
Stop-Function -Message "End time $SaturdayEndTime needs to match between '000000' and '235959'"
Stop-Function -Message "End time $SaturdayEndTime needs to match between '000000' and '235959'. Pager Day not set."
return
}
}
Expand All @@ -198,7 +198,7 @@ function New-DbaAgentOperator {
Stop-Function -Message "Please enter a Sunday start time or use -Force to use defaults."
return
} elseif ($SundayStartTime -notmatch $RegexTime) {
Stop-Function -Message "Start time $SundayStartTime needs to match between '000000' and '235959'"
Stop-Function -Message "Start time $SundayStartTime needs to match between '000000' and '235959'. Pager Day not set."
return
}

Expand All @@ -210,7 +210,7 @@ function New-DbaAgentOperator {
Stop-Function -Message "Please enter a Sunday End Time or use -Force to use defaults."
return
} elseif ($SundayEndTime -notmatch $RegexTime) {
Stop-Function -Message "Sunday End time $SundayEndTime needs to match between '000000' and '235959'"
Stop-Function -Message "Sunday End time $SundayEndTime needs to match between '000000' and '235959'. Pager Day not set."
return
}
}
Expand All @@ -224,7 +224,7 @@ function New-DbaAgentOperator {
Stop-Function -Message "Please enter Weekday Start Time or use -Force to use defaults."
return
} elseif ($WeekdayStartTime -notmatch $RegexTime) {
Stop-Function -Message "Weekday Start time $WeekdayStartTime needs to match between '000000' and '235959'"
Stop-Function -Message "Weekday Start time $WeekdayStartTime needs to match between '000000' and '235959'. Pager Day not set."
return
}

Expand All @@ -236,7 +236,7 @@ function New-DbaAgentOperator {
Stop-Function -Message "Please enter a Weekday End Time or use -Force to use defaults."
return
} elseif ($WeekdayEndTime -notmatch $RegexTime) {
Stop-Function -Message "Weekday End time $WeekdayEndTime needs to match between '000000' and '235959'"
Stop-Function -Message "Weekday End time $WeekdayEndTime needs to match between '000000' and '235959'. Pager Day not set."
return
}
}
Expand Down
7 changes: 3 additions & 4 deletions public/New-DbaAgentSchedule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ function New-DbaAgentSchedule {
param (
[parameter(Mandatory, ValueFromPipeline)]
[DbaInstanceParameter[]]$SqlInstance,
[System.Management.Automation.PSCredential]
$SqlCredential,
[System.Management.Automation.PSCredential]$SqlCredential,
[object[]]$Job,
[object]$Schedule,
[switch]$Disabled,
Expand Down Expand Up @@ -412,7 +411,7 @@ function New-DbaAgentSchedule {
Stop-Function -Message "Please enter a start time or use -Force to use defaults." -Target $SqlInstance
return
} elseif ($StartTime -notmatch $RegexTime) {
Stop-Function -Message "Start time $StartTime needs to match between '000000' and '235959'" -Target $SqlInstance
Stop-Function -Message "Start time $StartTime needs to match between '000000' and '235959'. Schedule $Schedule not set." -Target $SqlInstance
return
}

Expand All @@ -424,7 +423,7 @@ function New-DbaAgentSchedule {
Stop-Function -Message "Please enter an end time or use -Force to use defaults." -Target $SqlInstance
return
} elseif ($EndTime -notmatch $RegexTime) {
Stop-Function -Message "End time $EndTime needs to match between '000000' and '235959'" -Target $SqlInstance
Stop-Function -Message "End time $EndTime needs to match between '000000' and '235959'. Schedule $Schedule not set." -Target $SqlInstance
return
}

Expand Down
4 changes: 2 additions & 2 deletions public/Set-DbaAgentSchedule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,13 @@ function Set-DbaAgentSchedule {

# Check the start time
if ($StartTime -and ($StartTime -notmatch $RegexTime)) {
Stop-Function -Message "Start time $StartTime needs to match between '000000' and '235959'" -Target $SqlInstance
Stop-Function -Message "Start time $StartTime needs to match between '000000' and '235959'. Schedule $ScheduleName not set." -Target $SqlInstance
return
}

# Check the end time
if ($EndTime -and ($EndTime -notmatch $RegexTime)) {
Stop-Function -Message "End time $EndTime needs to match between '000000' and '235959'" -Target $SqlInstance
Stop-Function -Message "End time $EndTime needs to match between '000000' and '235959'. Schedule $ScheduleName not set." -Target $SqlInstance
return
}

Expand Down

0 comments on commit 20ae33e

Please sign in to comment.