2017-08-08 66 views
0

保留策略是「保留最近7次備份(上週),然後保留星期五備份3個月,然後保持每月上個星期五備份一年」。根據自定義日期過濾器刪除舊文件

以下是涵蓋兩個保留條件的腳本,但無法完成第三個條件,即「每個月的上個星期五備份一年」。需要幫助使IF塊相同。

編輯:解決方案將文件移動到不同的文件夾中,然後將單獨的保留腳本應用到單獨的文件夾將不可能,因爲裏面有很多子文件夾。由於這全部用於備份文件,因此移動文件會使我們的依賴恢復過程更加複雜,否則代碼將需要在所有服務器上進行調整。

#----- define folder where files are located ----# 
$TargetFolder = "C:\Users\chsa\Desktop\Retention" 

#----- define extension ----# 
$Extension = "*.txt" 

#----- define LastWriteTime parameter based on $Days ---# 
$LastWrite = $Now.AddDays(-$Days) 

#----- get files based on lastwrite filter and specified folder ---# 
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL -and $File.LastWriteTime -lt (Get-Date).AddYears(-1)) 
    { 
    Remove-Item $File.FullName | Out-Null 
    Write-Host $File 
    } 

    if ($File -ne $NULL -and $File.LastWriteTime -ge (Get-Date).AddMonths(-3) -and $File.LastWriteTime -lt (Get-Date).AddDays(-7) -and $File.LastWriteTime.DayOfWeek -ne "Friday") 
    { 
    Remove-Item $File.FullName | Out-Null 
    Write-Host $File 
    } 
} 
+0

'「$ LastWrite」' - >'$ LastWrite' –

+0

作爲實現您的保留策略:移動備份()你想清理之前保留到不同的文件夾,即移動從週五備份將「每日」文件夾設置爲「每週」文件夾並帶有單獨的清理任務。爲每月備份做同樣的事情。 –

+0

謝謝Ansgar,但這是不可能的,因爲有很多子文件夾,以這種方式設置可能會有風險。 –

回答

0

下面的最終腳本。

該解決方案並不理想,因爲該腳本使用了多個循環,這聽起來沒有必要,但是需求似乎已經完成;並且腳本測試正常。

此外,該腳本還有其他規定,如果以防萬一,Last-Friday-Of-Month的備份文件不可用,那麼該腳本將保留第二個最後一個星期五的備份文件。同樣的道理,如果倒數第二也不可用,那麼它將保持倒數第三;第四周和第五週相同。非常感謝你們所有人。

#----- calculate start and end of current month ----# 
$startofmonth = Get-Date -day 1 -hour 0 -minute 0 -second 0 
$endofmonth = (($startofmonth).AddMonths(1).AddSeconds(-1)) 

#----- define folder where files are located ----# 
$TargetFolder = "C:\Users\chsa\Desktop\Retention" 

#----- define extension ----# 
$Extension = "*.txt" 

#----- get files based on lastwrite filter and specified folder ---# 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -lt $startofmonth.AddMonths(-13) } 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-13) -and $_.LastWriteTime -le $endofmonth.AddMonths(-13) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-13) -and $_.LastWriteTime -le $endofmonth.AddMonths(-13) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-12) -and $_.LastWriteTime -le $endofmonth.AddMonths(-12) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-12) -and $_.LastWriteTime -le $endofmonth.AddMonths(-12) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-11) -and $_.LastWriteTime -le $endofmonth.AddMonths(-11) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-11) -and $_.LastWriteTime -le $endofmonth.AddMonths(-11) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-10) -and $_.LastWriteTime -le $endofmonth.AddMonths(-10) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-10) -and $_.LastWriteTime -le $endofmonth.AddMonths(-10) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-9) -and $_.LastWriteTime -le $endofmonth.AddMonths(-9) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-9) -and $_.LastWriteTime -le $endofmonth.AddMonths(-9) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-8) -and $_.LastWriteTime -le $endofmonth.AddMonths(-8) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-8) -and $_.LastWriteTime -le $endofmonth.AddMonths(-8) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-7) -and $_.LastWriteTime -le $endofmonth.AddMonths(-7) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-7) -and $_.LastWriteTime -le $endofmonth.AddMonths(-7) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-6) -and $_.LastWriteTime -le $endofmonth.AddMonths(-6) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-6) -and $_.LastWriteTime -le $endofmonth.AddMonths(-6) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-5) -and $_.LastWriteTime -le $endofmonth.AddMonths(-5) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-5) -and $_.LastWriteTime -le $endofmonth.AddMonths(-5) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-4) -and $_.LastWriteTime -le $endofmonth.AddMonths(-4) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-4) -and $_.LastWriteTime -le $endofmonth.AddMonths(-4) -and $_.LastWriteTime.DayofWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-3) -and $_.LastWriteTime -le $endofmonth.AddMonths(-3) -and $_.LastWriteTime.DayOfWeek -ne "Friday"} 


foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-2) -and $_.LastWriteTime -le $endofmonth.AddMonths(-2) -and $_.LastWriteTime.DayOfWeek -ne "Friday"} 


foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-1) -and $_.LastWriteTime -le $endofmonth.AddMonths(-1) -and $_.LastWriteTime.DayOfWeek -ne "Friday"} 


foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth -and $_.LastWriteTime -lt (Get-Date).AddDays(-8) -and $_.LastWriteTime.DayOfWeek -ne "Friday"} 

foreach ($File in $Files) 
{ 
    if ($File -ne $NULL) 
    { 
    Remove-Item $File.FullName | out-null 
    #write-host $File 
    } 

} 
+0

腳本部署在生產中並按預期工作。謝謝你們。 –

0

會添加評論,但我沒有它的聲譽。

首先,我定義了一個函數來獲得「Last of Friday of month」片段,這似乎不可能在沒有混亂的混亂的情況下在單行上完成。

function LastFridayOfMonth([DateTime] $DateIn) 
{ 
    $numDaysArr = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} 
    return ($numDaysArr[$DateIn.Month] - $DateIn.Day) -lt 7 -and $DateIn.DayOfWeek -eq [System.DayOfWeek]::Friday 
} 

然後,它是容易,我認爲這與邏輯倒置,DeMorgans定理或許可以簡化邏輯在我的if語句,但它應該工作。

if (-not ($file.LastWriteTime -lt (Get-Date).AddYears(-1) -and $file.LastWriteTime -gt (Get-Date).AddMonths(-3) -and (LastFridayOfMonth $file.LastWriteTime))) 
{ 
    Remove-Item $File.FullName | Out-Null 
    Write-Host $File 
} 

注意,它不考慮閏年,我只測試了LastFridayOfMonth功能,而不是if語句的邏輯。在開始刪除文件之前檢查它...儘管如此,總體思路仍然適合您。

相關問題