我如何保留最新的8个备份文件并删除较旧的文件

发布于 2025-02-07 10:14:32 字数 363 浏览 1 评论 0原文

我如何保留最新的8个备份文件并删除较旧的文件,

backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
.
.
backup-Y-M-D.zip

大约有80个文件具有.zip扩展名。我要做的就是根据创建的日期保留最新的8个文件。我还尝试了logrotate,但没有旋转日志,因为它没有做任何事情。下面是logrotate的配置文件。

/root/test/*.zip {
    daily
    missingok
    extension .zip
    rotate 4
    nocompress 
}

How do I keep the latest 8 backup files and delete the older one

backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
.
.
backup-Y-M-D.zip

There are about 80 files having .zip extension all I wanted to do is to keep latest 8 files according to the date on which created. I also tried logrotate but failed to rotate logs as it is not doing anything. Below down is the config file of logrotate.

/root/test/*.zip {
    daily
    missingok
    extension .zip
    rotate 4
    nocompress 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2025-02-14 10:14:32

如果保证了命名约定,则可以在扩展Glot模式以获取最古老或最新文件时仅依靠文件的字母顺序排序。根据 fileName扩展

单词拆分后,除非已设置-f选项(请参阅set indenin),bash将每个单词扫描为字符'*','?''和'[''。如果这些字符之一出现并且没有引用,则该单词被视为模式,并由字母顺序排序的文件名列表匹配该模式(请参阅模式匹配)。

演示:

[user@hostname]$ touch backup-2022-06-14.zip backup-2022-06-13.zip backup-2021-07-04.zip
[user@hostname]$ echo *
backup-2021-07-04.zip backup-2022-06-13.zip backup-2022-06-14.zip

您可以利用它以获取除最后一个n元素以外的文件列表:

[user@hostname]$ all_files=(*)
[user@hostname]$ old_files=( "${all_files[@]:0:${#all_files[@]}-1}" ) #change -1 to -8 if you want to keep the 8 newest
[user@hostname]$ echo "${old_files[@]}"
backup-2021-07-04.zip backup-2022-06-13.zip

然后在该列表中进行任何您想做的任何您想做的事情,例如使用rm删除它>。

If the naming convention is guaranteed you could just rely on the alphabetical ordering of the files when expanding a glob pattern to get the oldest or newest files. According to Filename Expansion:

After word splitting, unless the -f option has been set (see The Set Builtin), Bash scans each word for the characters ‘*’, ‘?’, and ‘[’. If one of these characters appears, and is not quoted, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of filenames matching the pattern (see Pattern Matching).

Demo:

[user@hostname]$ touch backup-2022-06-14.zip backup-2022-06-13.zip backup-2021-07-04.zip
[user@hostname]$ echo *
backup-2021-07-04.zip backup-2022-06-13.zip backup-2022-06-14.zip

You can leverage this to get a list of files other than the last N elements:

[user@hostname]$ all_files=(*)
[user@hostname]$ old_files=( "${all_files[@]:0:${#all_files[@]}-1}" ) #change -1 to -8 if you want to keep the 8 newest
[user@hostname]$ echo "${old_files[@]}"
backup-2021-07-04.zip backup-2022-06-13.zip

And then do whatever you want with that list, such as remove it with rm "${old_files[@]}".

网白 2025-02-14 10:14:32

一种方法是使用以下单线仪,从日志所在的目录中运行:

ls -t | head -n -8 | xargs --no-run-if-empty rm

说明:

  • ls -t -t - 列出所有文件,以从最年轻到最古老的
  • hear -n -8 - 获取所有文件,除了第一个8
  • xargs -no -run -if -if -if -if -if -if -if -if -if -if -if -if-code - 如果有的话,请删除所选文件,如果您曾经,请阻止错误 如果您想设置此数字少于8个日志

,以便每天自动运行,以防您的服务器在周期的第七天离线并错过一周标记,运行crontab -e并将以下内容添加到您的工作中:

0 0 * * * cd yourDirNameHere && ls -t | head -n -8 | xargs --no-run-if-empty rm

然后,日志清洁器每晚都会在午夜运行。

One way to do this is with the following one-liner, ran from the directory where the logs are located:

ls -t | head -n -8 | xargs --no-run-if-empty rm

Explanation:

  • ls -t - lists all the files in order from youngest to oldest
  • head -n -8 - gets all the files except the first 8
  • xargs --no-run-if-empty rm - deletes the selected files if there are any, preventing errors if you ever have fewer than 8 logs

If you want to set this up to run automatically every day, giving you peace of mind in case your server is offline on the 7th day of a cycle and misses the one week mark, run crontab -e and add the following to your jobs:

0 0 * * * cd yourDirNameHere && ls -t | head -n -8 | xargs --no-run-if-empty rm

Then the log cleaner will be ran every night at midnight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文