Linux 使用 crontab 删除旧文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5375409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:21:28  来源:igfitidea点击:

deleting old files using crontab

linuxcrontab

提问by Ran

I use the following crontab record in order to daily backup my DB:

我使用以下 crontab 记录来每天备份我的数据库:

0 2 * * * MYSQL_PWD=password mysqldump -u user db_name > $HOME/db_backups/db_name-$(date +\%Y-\%m-\%d-\%H-\%M).sql 2>> $HOME/db_backups/cron.log

I want to add another crontab record that will delete the DB dumps that are older then one month.

我想添加另一个 crontab 记录,该记录将删除超过一个月的数据库转储。

Any thoughts?

有什么想法吗?

采纳答案by dogbane

Just create another cron:

只需创建另一个 cron:

0 3 * * * find $HOME/db_backups -name "db_name*.sql" -mtime +30 -exec rm {} \; >> $HOME/db_backups/purge.log 2>&1

It will find all backups older than 30 days and delete them.

它将找到所有超过 30 天的备份并将其删除。

回答by Shamit Verma

find /db_backups/ -mtime +30 -delete

This command would delete DB backups older than 30 days.

此命令将删除超过 30 天的数据库备份。

回答by Simon Richter

There is a tool called tmpreaperthat securely deletes files matching certain criteria, such as an access or modification date ndays in the past.

有一种称为tmpreaper安全删除符合特定条件的文件的工具,例如过去n天的访问或修改日期。