你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
此脚本可帮助你创建新的备份策略,或修改为受 MARS 代理保护的服务器设置的当前文件和文件夹备份策略。
示例脚本
<#
.SYNOPSIS
Modify file folder policy
.DESCRIPTION
Modify file folder policy
.ROLE
Administrators
#>
param (
[Parameter(Mandatory = $true)]
[string[]]
$filePath,
[Parameter(Mandatory = $true)]
[string[]]
$daysOfWeek,
[Parameter(Mandatory = $true)]
[string[]]
$timesOfDay,
[Parameter(Mandatory = $true)]
[int]
$weeklyFrequency,
[Parameter(Mandatory = $false)]
[int]
$retentionDays,
[Parameter(Mandatory = $false)]
[Boolean]
$retentionWeeklyPolicy,
[Parameter(Mandatory = $false)]
[int]
$retentionWeeks,
[Parameter(Mandatory = $false)]
[Boolean]
$retentionMonthlyPolicy,
[Parameter(Mandatory = $false)]
[int]
$retentionMonths,
[Parameter(Mandatory = $false)]
[Boolean]
$retentionYearlyPolicy,
[Parameter(Mandatory = $false)]
[int]
$retentionYears
)
Set-StrictMode -Version 5.0
$env:PSModulePath = (Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PSModulePath).PSModulePath
Import-Module MSOnlineBackup
$ErrorActionPreference = "Stop"
Try {
$timesOfDaySchedule = @()
foreach ($time in $timesOfDay) {
$timesOfDaySchedule += ([TimeSpan]$time)
}
$daysOfWeekSchedule = @()
foreach ($day in $daysOfWeek) {
$daysOfWeekSchedule += ([System.DayOfWeek]$day)
}
$schedule = New-OBSchedule -DaysOfWeek $daysOfWeekSchedule -TimesOfDay $timesOfDaySchedule -WeeklyFrequency $weeklyFrequency
if ($daysOfWeekSchedule.Count -eq 7) {
if ($retentionWeeklyPolicy -and $retentionMonthlyPolicy -and $retentionYearlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths -RetentionYearlyPolicy:$true -YearDaysOfWeek $daysOfWeekSchedule -YearTimesOfDay $timesOfDaySchedule -RetentionYears $retentionYears
}
elseif ($retentionWeeklyPolicy -and $retentionMonthlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths
}
elseif ($retentionWeeklyPolicy -and $retentionYearlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks -RetentionYearlyPolicy:$true -YearDaysOfWeek $daysOfWeekSchedule -YearTimesOfDay $timesOfDaySchedule -RetentionYears $retentionYears
}
elseif ($retentionYearlyPolicy -and $retentionMonthlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths -RetentionYearlyPolicy:$true -YearDaysOfWeek $daysOfWeekSchedule -YearTimesOfDay $timesOfDaySchedule -RetentionYears $retentionYears
}
elseif ($retentionWeeklyPolicy) {
$retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks
}
elseif ($retentionMonthlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths
}
elseif ($retentionYearlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionYearlyPolicy:$true -YearDaysOfWeek $daysOfWeekSchedule -YearTimesOfDay $timesOfDaySchedule -RetentionYears $retentionYears
}
else {
$retention = New-OBRetentionPolicy -RetentionDays $retentionDays
}
}
else {
if ($retentionWeeklyPolicy -and $retentionMonthlyPolicy -and $retentionYearlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths -RetentionYearlyPolicy:$true -YearDaysOfWeek $daysOfWeekSchedule -YearTimesOfDay $timesOfDaySchedule -RetentionYears $retentionYears
}
elseif ($retentionWeeklyPolicy -and $retentionMonthlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths
}
elseif ($retentionWeeklyPolicy -and $retentionYearlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks -RetentionYearlyPolicy:$true -YearDaysOfWeek $daysOfWeekSchedule -YearTimesOfDay $timesOfDaySchedule -RetentionYears $retentionYears
}
elseif ($retentionYearlyPolicy -and $retentionMonthlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths -RetentionYearlyPolicy:$true -YearDaysOfWeek $daysOfWeekSchedule -YearTimesOfDay $timesOfDaySchedule -RetentionYears $retentionYears
}
elseif ($retentionWeeklyPolicy) {
$retention = New-OBRetentionPolicy -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks
}
elseif ($retentionMonthlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths
}
elseif ($retentionYearlyPolicy) {
$retention = New-OBRetentionPolicy -RetentionYearlyPolicy:$true -YearDaysOfWeek $daysOfWeekSchedule -YearTimesOfDay $timesOfDaySchedule -RetentionYears $retentionYears
}
}
$oldPolicy = Get-OBPolicy
if ($oldPolicy) {
$ospec = Get-OBFileSpec $oldPolicy
$p = Remove-OBFileSpec -FileSpec $ospec -Policy $oldPolicy -Confirm:$false
$fileSpec = New-OBFileSpec -FileSpec $filePath
Add-OBFileSpec -Policy $p -FileSpec $fileSpec -Confirm:$false
Set-OBSchedule -Policy $p -Schedule $schedule -Confirm:$false
Set-OBRetentionPolicy -Policy $p -RetentionPolicy $retention -Confirm:$false
Set-OBPolicy -Policy $p -Confirm:$false
$p
}
else {
$policy = New-OBPolicy
$fileSpec = New-OBFileSpec -FileSpec $filePath
Add-OBFileSpec -Policy $policy -FileSpec $fileSpec
Set-OBSchedule -Policy $policy -Schedule $schedule
Set-OBRetentionPolicy -Policy $policy -RetentionPolicy $retention
Set-OBPolicy -Policy $policy -Confirm:$false
}
}
Catch {
if ($error[0].ErrorDetails) {
throw $error[0].ErrorDetails
}
throw $error[0]
}
运行脚本
若要运行 PowerShell 脚本以创建新的或修改当前文件和文件夹备份策略,请执行以下步骤:
- 使用所选
.ps1
名称和扩展在计算机上保存上述脚本。 - 通过提供以下参数运行脚本:
- 备份计划和备份需要保留的天数/周/月数或年份。
- -filePath - 应在备份中包含或排除的文件和文件夹。
后续步骤
详细了解 如何使用 PowerShell 通过 MARS 代理部署和管理本地备份。