【发布时间】:2019-08-27 13:31:05
【问题描述】:
我有一个 powershell 脚本,旨在检查一些备份文件,存档月末备份(如果是一个月的第一天),然后删除超过 9 天的任何内容。我试图告诉它排除存档文件夹,但它似乎忽略了这一点,我不确定为什么.. 有点像 powershell 的菜鸟。
我尝试过使用 -notlike $Exlcude+% 我尝试过 -notmatch -NE .. 我什至尝试过 notcontains 即使我知道这不起作用.. 我尝试将 $exclude 应用于代码的每个部分可以访问该文件夹的位置,并且它仍在制作它的副本。
#If first of the month copy latest backups to Monthly Backup Folder - will copy full folder path
PARAM($BackupPath="\\SomeServer\sqltestbackups\",
$Exclude= "\\SomeServer\sqltestbackups\Archive")
#Grab a recursive list of all subfolders
$SubFolders = dir $BackupPath -Recurse | Where-Object {$_.PSIsContainer} | ForEach-Object -Process {$_.FullName}
#Iterate through the list of subfolders and grab the first file in each
$Year = (get-date).Year
$Month = (get-date).Month
$StartOfMonth = Get-Date -Year $Year -Month $Month -Day 1
$Today = (Get-Date).day
$Date = (GET-DATE).AddDays(-9)
$PrevMonth = (GET-DATE).year.ToString()+(Get-Culture).DateTimeFormat.GetMonthName((Get-Date).AddMonths(-1).month)
$Destination=$Exclude + "\" + $Prevmonth +"\"
IF (!(Test-Path -path $destination)) {New-Item $destination -Type Directory}
IF($Today -eq '5')
{
$Path = $BackupPath #Root path to look for files
$DestinationPath = $Destination #Remote destination for file copy
#Grab a recursive list of all subfolders
$SubFolders = dir $Path -Recurse | Where-Object {$_.PSIsContainer -and $_.DirectoryName -notmatch $Exclude} | ForEach-Object -Process {$_.FullName}
#Iterate through the list of subfolders and grab the first file in each
ForEach ($Folder in $SubFolders)
{
$FullFileName = dir $Folder | Where-Object {!$_.PSIsContainer -and $_.DirectoryName -notmatch $Exclude} | Sort-Object {$_.LastWriteTime} -Descending | Select-Object -First 1
#For every file grab it's location and output the robocopy command ready for use
ForEach ($File in $FullFileName)
{
$FilePath = $File.DirectoryName
$ArchFolder = $File.DirectoryName
$ArchFolder=$ArchFolder.Replace($BackupPath,"")+"\"
$FinalPath=$destinationPath+$ArchFolder
$FileName = $File.Name
robocopy $FilePath $FinalPath $FileName /A-:SH /R:6 /W:30 /Z
}
}
}
# Delete files older than 9 days that are not contained within the month end folder
Get-ChildItem $BackupPath -Recurse |
Where-Object { !($_.PSIsContainer) -and
$_.LastWriteTime -lt $Date -and
$_.Directory -notlike $Exclude+"%" } |
Remove-Item
除了复制月末部分之外,该代码都可以工作。在这部分中,它包括存档文件夹,我最终复制了上个月。该脚本旨在将文件放入存档/YM/FullPath备份所以正在发生的事情是它正在存档/YM/ARchive/YM/FullPath,即使我正在努力从文件夹中排除这个路径。
robocopy 出现问题的示例
来源: \SomeServer\sqltestbackups\ARCHIVE\2019March\SomeOtherServer\SQLBackups\SomeDatabase\master\
目标: \SomeServer\sqltestbackups\Archive\2019March\ARCHIVE\2019March\SomeOtherServer\SQLBackups\SomeDatabase\master\
【问题讨论】:
-
如果您只是使用默认的
$exclude,您似乎在字符串$Destination=$Exclude + "\" + $Prevmonth +"\"中添加了一个额外的斜杠。在构建路径时考虑使用[io.path]::combine()。它优于弦乐建筑。-match还支持正则表达式,斜杠是一个控制字符,所以你的比较不会一样。 -
@Matt 看起来你用那个控制字符钉住了它,我现在实际上正在返回错误;我一点也不熟悉如何使用 [io.path]::combine() 但我会解决我现在遇到的错误,稍后我会发回报告。谢谢!
-
想通了..我需要将 \s 加倍以使其与 -notmatch 一起使用..现在一切都很好:)
标签: powershell