删除一个月内除最后一个之外的所有文件
我们每天都会运行备份作业。
此作业创建一个格式为 yyyyMMdd.7z 的新文件,现在我们需要自动清理备份存储。
我们的备份警察说我们需要保留最近 5 天的文件以及每个月的最后一次备份。第一步很容易,因为我有今天。但我怎样才能保留每个月的最后一个月呢?
Every day we run an backup job.
This job creates a new file in format yyyyMMdd.7z and now we need to cleanup our backup storage automatically.
Our backup police says that we need to keep files from last 5 days, and last backup of each month. First step is easy since I have current day. But how I can keep the last of each month?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们总是在夜间运行备份,因此当月的最后一次备份可以方便地在下个月 1 日的前几个小时内运行,通常是凌晨 3 点 17 分。那么模式就是 *01.7z 。
也许您可以以类似的方式调整备份计划?
否则通过 select-object --last 1 管道传输 YYYYMM*.7z 的目录列表。将其移开,重命名(如 Archive_YYYYYMMDD.7z)。然后他们很容易分开。
We always run our backups overnight, so the last backup of the month conveniently runs on the 1st of the next month in the first hours, typically at 3h17 in the morning. Then the pattern is simply *01.7z .
Maybe you could tweak the backup schedule in a similar way?
otherwise pipe a directory listing of YYYYMM*.7z through select-object --last 1. Move it out of the way, rename it (like Archive_YYYYYMMDD.7z). Then they are easily separated.
为了保留每个月的最后一个,我会浏览文件文件夹,并通过保存 yyyyMMdd.7z 中的 yyyy、mm 和 dd 来记录每个月的最后一个文件。
因此,当您阅读文件夹时,如果您得到一条记录,其中 MM 与您已有的 mm 匹配,那么您将检查 dd。 (如下)如果它与已存储的 mm 不匹配,则添加它。
如果保存的 dd 比新的 dd 更新,则删除您所在的文件。如果当前文件更新,则删除已保存的文件(通过连接 yyyy+MM+dd+".7z" 来查找您保存的文件,并将存储的值替换为当前文件。
To keep the last of each month I would go through the folder of files and for each month keep record of the last file by saving the yyyy, mm and dd from yyyyMMdd.7z.
Thus as you are reading through the folder if you get a record where the MM matches an mm you already have then you check the dd. (below) If it doesn't match a mm already stored then add it.
If the saved dd is more current then the new one then delete the file you are at. If the current file is more current then you delete the one saved (finding by concatenating yyyy+MM+dd+".7z" that you saved and replace the stored values with the current file.