每天保存一个文件,一个3天和一周保存文件并删除其他文件

问题描述 投票:0回答:3

我有一个包含过去15天数据库备份文件的文件夹。我需要删除所有文件,每天只保留一个,一个3天和一周。有人可以帮忙吗?

我尝试了一些脚本但没有达到我的要求

mkdir -p monthly
mkdir -p weekly

ln backup_$NOW.tgz weekly/

# find current month
month=$(date +%Y-%m-)
# find the first file of the current month in the weekly folder
first_monthly=$(ls --sort=time -1 weekly/*$month* 2>/dev/null | tail -1)
# and put it in the monthly folder
ln -f $first_monthly monthly/

# we need only 5 weekly backups
ls --sort=time -1 weekly/* 2>/dev/null | tail -n+6 >> /tmp/deletebackups.txt
# we need only 5 monthly backups
ls --sort=time -1 monthly/* 2>/dev/null | tail -n+6 >> /tmp/deletebackups.txt

# delete the extra files
#rm $(cat /tmp/deletebackups.txt) 2>/dev/null
xargs --arg-file /tmp/deletebackups.txt rm 
bash powershell
3个回答
0
投票

这是确定要保留或删除内容的一种方法。它使用“保持日期的年龄”数组,然后检查文件年龄是否在该集合中。

$DaysOldToKeep = @(
    0
    3
    7
    )

$SourceDir = $env:TEMP
$Today = (Get-Date).Date

$FileList = Get-ChildItem -LiteralPath $SourceDir -File |
    Sort-Object -Property CreationTime

foreach ($FL_Item in $FileList)
    {
    $DaysOld = ($Today - $FL_Item.CreationTime).Days

    if ($DaysOld -notin $DaysOldToKeep)
        {
        Write-Warning ('[ {0} ] is [ {1} ] days old & should be removed.' -f $FL_Item.Name, $DaysOld)
        }
        else
        {
        Write-Host ('    [ {0} ] is [ {1} ] days old & should be KEPT.' -f $FL_Item.Name, $DaysOld)
        }
    }

截断输出...

WARNING: [ testing-making-dir-and-file.txt ] is [ 9 ] days old & should be removed.
WARNING: [ hd4B753.tmp ] is [ 8 ] days old & should be removed.
    [ Itunes_AlbumAutoRating_Disable.ps1_2019-04-08.log ] is [ 7 ] days old & should be KEPT.
    [ vscode-inno-updater-1554768842.log ] is [ 7 ] days old & should be KEPT.
    [ hd464FA.tmp ] is [ 7 ] days old & should be KEPT.
    [ hd4E2F0.tmp ] is [ 7 ] days old & should be KEPT.
WARNING: [ Genre-List_2019-04-09.log ] is [ 6 ] days old & should be removed.
WARNING: [ Grouping-Strings-List_2019-04-10.log ] is [ 5 ] days old & should be removed.
WARNING: [ Itunes_R-PC-SC_Save.ps1_2019-04-11.log ] is [ 4 ] days old & should be removed.
    [ Magenoob_-_Merged_User_Info.csv ] is [ 3 ] days old & should be KEPT.
    [ Itunes_Default-Rating_Set.ps1_2019-04-12.log ] is [ 3 ] days old & should be KEPT.
    [ MagicTheGathering_-_Scryfall-Default-Cards.json ] is [ 3 ] days old & should be KEPT.
WARNING: [ hd4C490.tmp ] is [ 2 ] days old & should be removed.
WARNING: [ hd45A92.tmp ] is [ 1 ] days old & should be removed.
    [ exthost-825471.cpuprofile ] is [ 0 ] days old & should be KEPT.
    [ vscode-inno-updater-1555314279.log ] is [ 0 ] days old & should be KEPT.
    [ npp.7.6.6.Installer.x64.exe ] is [ 0 ] days old & should be KEPT.
    [ hd43E2A.tmp ] is [ 0 ] days old & should be KEPT.
    [ hd44D37.tmp ] is [ 0 ] days old & should be KEPT.
    [ hd4488C.tmp ] is [ 0 ] days old & should be KEPT.
    [ hd45A09.tmp ] is [ 0 ] days old & should be KEPT.
    [ Itunes_AlbumAutoRating_Disable.ps1_2019-04-15.log ] is [ 0 ] days old & should be KEPT.

0
投票

检索第一个最新文件的方法是:

ls -t weekly/ | head -1

并拥有其他最古老的,删除:

ls -t weekly/ | tail +2

如果你只想保持bash,它可能是最简单的方法。


0
投票

尝试这样的事情(如果你想真的删除,删除-whatif):

$File1Founded=$false
$File3Founded=$false
$File7Founded=$false
$CurrentDate1=(get-date).Date
$CurrentDate3=(get-date).AddDays(-3).Date
$CurrentDate7=(get-date).AddDays(-7).Date

Get-ChildItem "C:\temp\test1" -file | sort CreationTime -Descending | %{

$DateFile=$_.CreationTime.Date

if ($DateFile -eq $CurrentDate1)
{
   if ($File1Founded)
   {
      $_
   }
   else
   {
       $File1Founded=$true
   }

}
elseif ($DateFile -eq $CurrentDate3)
{
   if ($File3Founded)
   {
      $_
   }
   else
   {
       $File3Founded=$true
   }
}
elseif ($DateFile -eq $CurrentDate7)
{
   if ($File7Founded)
   {
      $_
   }
   else
   {
       $File7Founded=$true
   }
}
else
{
    $_
}

} | Remove-Item -WhatIf
© www.soinside.com 2019 - 2024. All rights reserved.