我有一个脚本可以删除比备份目录X = 21天更早的数据库转储:
DB_DUMP_DIR=/var/backups/dbs RETENTION=$((21*24*60)) # 3 weeks find ${DB_DUMP_DIR} -type f -mmin +${RETENTION} -delete
但是,如果由于某种原因数据库转储作业暂时无法完成,则所有转储最终都将被丢弃.因此,作为一种保障,我想保留至少最年轻的Y = 7转储,即使它们全部或部分转存超过21天.
我寻找比这意大利面条更优雅的东西:
DB_DUMP_DIR=/var/backups/dbs RETENTION=$((21*24*60)) # 3 weeks KEEP=7 find ${DB_DUMP_DIR} -type f -printf '%T@ %p\n' | \ # list all dumps with epoch sort -n | \ # sort by epoch, oldest 1st head --lines=-${KEEP} |\ # Remove youngest/bottom 7 dumps while read date filename ; do # loop through the rest find $filename -mmin +${RETENTION} -delete # delete if older than 21 days done
(本段可能有小错误 - 忽略他们这是为了说明我能想出自己,为什么我不喜欢它.)
编辑:查找选项"-mtime"是一次性的:" - mtime +21"表示实际上"至少22天".那总是让我困惑,所以我用-mmin代替.还是一次性,但只有一分钟.