deleting old files using crontab

五迷三道 提交于 2019-12-17 18:56:10

问题


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

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.

Any thoughts?


回答1:


Just create another 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.




回答2:


find /db_backups/ -mtime +30 -delete

This command would delete DB backups older than 30 days.




回答3:


There is a tool called tmpreaper that securely deletes files matching certain criteria, such as an access or modification date n days in the past.



来源:https://stackoverflow.com/questions/5375409/deleting-old-files-using-crontab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!