gpt4 book ai didi

powershell - 备份文件删除器

转载 作者:行者123 更新时间:2023-12-04 07:35:28 28 4
gpt4 key购买 nike

我有一个充满备份文件的目录,但需要自动清理该目录,因为我的空间不足。最简单的方法是使用 powershell 脚本。
备份每天创建 2 个备份。我想每天保留 2 个备份,持续 7 天,超过 7 天但不超过 3 个月的备份我想每天保留 1 个,我想每周保留 1 个超过 3 个月的备份。超过 2 年的所有内容都会被删除。
我编写的第一个函数删除了 2 年以上的所有内容:

$checkPath = "C:\demo\"
$list = (Get-ChildItem -Path $checkPath | Where-Object {$_.LastWriteTime -lt (Get-Date).AddYears(-2)}).FullName
$count = $list.Length
for ($i = 0; $i -lt $count; $i += 1)
{
Write-Verbose "[$i] $($list[$i])"
Remove-Item -Path $list[$i]
}
之后,我创建了一个删除每个第二个备份的函数:
$checkPath = "C:\demo\"
$list = (Get-ChildItem -Path $checkPath | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Where-Object {$_.LastWriteTime -lt (Get-Date).AddMonths(-3)}).FullName
$count = $list.Length
for ($i = 0; $i -lt $count; $i += 2)
{
Write-Verbose "[$i] $($list[$i])"
Remove-Item -Path $list[$i]
}
好吧,我觉得有点迷茫,我在这里走对了路吗?有没有人可能有建议做这个“更好”?

最佳答案

同样,除了代码中的内容外,不会做太多细节。希望评论说得足够响亮

$checkPath = 'C:\temp'

$classified = switch ((Get-ChildItem -Path $checkPath -File | Group-Object { $_.LastWriteTime.Date }) ) {
# Last 7 days
{ [datetime]::Parse(($_.Name)) -ge [datetime]::Today.AddDays(-7) } {
$keep = $_.group | Sort-Object LastWriteTime | Select-Object -Last 2
$keep | ForEach-Object {
[pscustomobject]@{Path = $_.fullname; LastWriteTime = $_.LastWriteTime; Action = 'Keep'; Interval = '<= 7 days' }
}
$remove = $_.group | Where-Object { $_ -notin $keep }
$remove | ForEach-Object {
[pscustomobject]@{Path = $_.fullname; LastWriteTime = $_.LastWriteTime; Action = 'Remove'; Interval = '<= 7 days' }
}
}
{ [datetime]::Parse(($_.Name)) -lt [datetime]::Today.AddDays(-7) -and
[datetime]::Parse(($_.Name)) -ge [datetime]::Today.AddMonths(-3) } {
# Between 7 days and 3 months $_.Name -ForegroundColor Green

$keep = $_.group | Sort-Object LastWriteTime | Select-Object -Last 1
$keep | ForEach-Object {
[pscustomobject]@{Path = $_.fullname; LastWriteTime = $_.LastWriteTime; Action = 'Keep'; Interval = 'Between 7 days and 3 months' }
}
$remove = $_.group | Where-Object { $_ -notin $keep }
$remove | ForEach-Object {
[pscustomobject]@{Path = $_.fullname; LastWriteTime = $_.LastWriteTime; Action = 'Remove'; Interval = 'Between 7 days and 3 months' }
}
}
{ [datetime]::Parse(($_.Name)) -lt [datetime]::Today.AddMonths(-3) -and
[datetime]::Parse(($_.Name)) -ge [datetime]::Today.AddYears(-2) } {
# Between 3 months and 2 years $_.Name -ForegroundColor Green
# Set to unknown for now. Group by weekOfYear later to make determination
$_.group | ForEach-Object {
[pscustomobject]@{Path = $_.Fullname; LastWriteTime = $_.LastWriteTime; Action = 'Unknown'; Interval = 'Between 3 months and 2 years' }
}
}
Default {
$_.group | ForEach-Object {
# Everything after 2 years $_.Name -ForegroundColor Green
[pscustomobject]@{Path = $_.fullname; LastWriteTime = $_.LastWriteTime; Action = 'Remove'; Interval = '> 2 years' }
}
}
}

# Needed to determine and group by week of year
$cultureInfo = [cultureinfo]::new('en-US')
$calendar = $cultureInfo.Calendar;
$calendarRule = $cultureInfo.DateTimeFormat.CalendarWeekRule
$firstDayOfWeek = $cultureInfo.DateTimeFormat.FirstDayOfWeek


$classified | Where-Object Interval -EQ 'Between 3 months and 2 years' |
# Group all files between 3 months and 2 years by weekOfYear and Year and set latest one to keep, others to remove
Group-Object { $calendar.GetWeekOfYear($_.LastWriteTime, $calendarRule, $firstDayOfWeek) }, { $_.LastWriteTime.Year } |
ForEach-Object {
$keep = $_.group | Sort-Object LastWriteTime | Select-Object -Last 1
$keep | ForEach-Object { $_.Action = 'Keep' }

$remove = $_.group | Where-Object { $_ -notin $keep }
$remove | ForEach-Object { $_.Action = 'Remove' }
}

$classified | Sort-Object LastWriteTime | Out-Host

$classified | Where-Object Action -EQ 'Remove' | Remove-Item -WhatIf
输出
Path                                                      LastWriteTime       Action Interval
---- ------------- ------ --------
C:\temp\20210101 - 20210108.pdf 13.01.2021 09:15:22 Keep Between 3 months and 2 years
C:\temp\20210115 - 20210122.pdf 26.01.2021 15:57:47 Keep Between 3 months and 2 years
C:\temp\rm_50117.pbl 14.05.2021 15:26:41 Keep Between 7 days and 3 months
C:\temp\zipped.zip 19.05.2021 18:35:12 Remove Between 7 days and 3 months
C:\temp\zipped2.zip 19.05.2021 18:40:54 Remove Between 7 days and 3 months
C:\temp\zipped3.zip 19.05.2021 18:46:05 Keep Between 7 days and 3 months
C:\temp\xxx_Some Doc ID 2333 _other stuff_more stuff.junk 20.05.2021 23:32:43 Keep Between 7 days and 3 months
C:\temp\testlksj.ps1 21.05.2021 22:28:03 Keep Between 7 days and 3 months
C:\temp\skdljfsdkf.ps1 22.05.2021 09:04:29 Remove Between 7 days and 3 months
C:\temp\testparam.ps1 22.05.2021 17:08:33 Remove Between 7 days and 3 months
C:\temp\gitignorepath.txt 22.05.2021 21:03:06 Keep Between 7 days and 3 months
C:\temp\lslkdfjl.ps1 23.05.2021 00:56:13 Remove Between 7 days and 3 months
C:\temp\test2.txt 23.05.2021 10:17:24 Remove Between 7 days and 3 months
C:\temp\test3.txt 23.05.2021 10:17:38 Remove Between 7 days and 3 months
C:\temp\test4.txt 23.05.2021 10:17:50 Keep Between 7 days and 3 months
C:\temp\fromVS.txt 24.05.2021 22:57:44 Keep <= 7 days
C:\temp\testlksfsdfsdj.ps1 25.05.2021 16:55:45 Keep <= 7 days
C:\temp\dslkfjlds.ps1 26.05.2021 23:35:23 Keep <= 7 days
C:\temp\slkfjdslfj.ps1 27.05.2021 00:35:09 Keep <= 7 days
C:\temp\testgit.log 31.05.2021 13:28:19 Keep <= 7 days
C:\temp\out.log 31.05.2021 13:51:37 Keep <= 7 days

What if: Performing the operation "Remove File" on target "C:\temp\zipped.zip".
What if: Performing the operation "Remove File" on target "C:\temp\zipped2.zip".
What if: Performing the operation "Remove File" on target "C:\temp\skdljfsdkf.ps1".
What if: Performing the operation "Remove File" on target "C:\temp\testparam.ps1".
What if: Performing the operation "Remove File" on target "C:\temp\lslkdfjl.ps1".
What if: Performing the operation "Remove File" on target "C:\temp\test2.txt".
What if: Performing the operation "Remove File" on target "C:\temp\test3.txt".

关于powershell - 备份文件删除器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67774275/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com