从查找和获取子集(bash)中排序结果

时间:2010-02-19 23:46:37

标签: bash find purge

我有一对 bash脚本,1个转储mysql dbs,第二个清除旧备份

我一直依赖日期,因此清除所有超过7天的文件,并每天创建新备份。结果是7天前的一组备份。

现在我已经有20多个dbs 我已将每日工作分成每日和每周工作。如果我继续使用查找和日期方法,我将失去过去一周。

问题

如何按日期排序查找的输出,然后清除除最近7之外的所有输出。(所以即使最近的是2个月,它仍然存在)

这是我所得到的,但并不像预期的那样。也许使用ls可能是更好的选择9find是使用日期作为清除的唯一标准的残余物。

OLDFILES=`find -regex .*sql.*`

set -- $OLDFILES

if [ -z $1 ]
then
    printf "\tNo files matching purge criteria\n" | tee -a $LOGFILE
else
    printf "\tSQL Files being Delete from $HERE\n" | tee -a $LOGFILE
    printf "\t\t%s\n" $OLDFILES  | tee -a $LOGFILE
fi
# $RETAIN is the # of recent to keep
# so I want to grab a subset from 0 to (total - retain), and purge them
TOBURN=$(( $# - $RETAIN ))
printf " grab first %s files. %s  - %s" $TOBURN $# $RETAIN
if [ $TOBURN -gt 0 ]
then
    TOPURGE=("$@:0:$TOBURN")
    printf "\n\nREMOVING..\n\t\t%s\n" $TOPURGE
fi

现有文件

            ./webbmaster_bellarose_joomla_01-09-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-25-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-10-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-04-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-25-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-29-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-30-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-28-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-06-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-13-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-24-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-21-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-24-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-27-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-26-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-17-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-03-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-21-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-20-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-16-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-31-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-11-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-05-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-14-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-22-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-12-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-17-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-18-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-20-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-08-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-22-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-27-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-23-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-07-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-26-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-19-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-29-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-15-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-28-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-01-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-18-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-02-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-23-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-19-2009.sql.gz

清除目标(我想在简单rm -f $ var的变量中设置)

            ./webbmaster_bellarose_joomla_01-09-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-25-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-10-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-04-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-30-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-28-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-06-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-13-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-21-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-24-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-27-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-17-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-03-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-21-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-20-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-16-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-31-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-11-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-05-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-14-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-22-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-12-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-17-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-18-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-20-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-08-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-22-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-23-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-07-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-26-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-19-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-29-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-15-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-01-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-18-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-02-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-19-2009.sql.gz

(日期1/23 - 1/29保持不变)

4 个答案:

答案 0 :(得分:4)

请自己(以及其他所有人)帮忙,并开始以ISO-8601格式(即YYYY-MM-DD)撰写日期。

E.g。而不是

./webbmaster_bellarose_joomla_12-29-2009.sql.gz

你会有

./webbmaster_bellarose_joomla_2009-12-29.sql.gz

这样做many benefits,最重要的是,按时间顺序和字母顺序排列相同。无需查找,只需列出文件,逆序,删除前7行并删除任何剩余文件:

ls *sql* | tac | sed 1,7d | tr '\012' '\000' | xargs -0 --no-run-if-empty rm

更新:“前进”是什么意思?我可以告诉我的解决方案100%解决您的问题:

/tmp/so>cat existing_files
./webbmaster_bellarose_joomla-2009-12-17.sql.gz
./webbmaster_bellarose_joomla-2009-12-18.sql.gz
./webbmaster_bellarose_joomla-2009-12-19.sql.gz
./webbmaster_bellarose_joomla-2009-12-20.sql.gz
./webbmaster_bellarose_joomla-2009-12-21.sql.gz
./webbmaster_bellarose_joomla-2009-12-22.sql.gz
./webbmaster_bellarose_joomla-2009-12-23.sql.gz
./webbmaster_bellarose_joomla-2009-12-24.sql.gz
./webbmaster_bellarose_joomla-2009-12-25.sql.gz
./webbmaster_bellarose_joomla-2009-12-26.sql.gz
./webbmaster_bellarose_joomla-2009-12-27.sql.gz
./webbmaster_bellarose_joomla-2009-12-28.sql.gz
./webbmaster_bellarose_joomla-2009-12-29.sql.gz
./webbmaster_bellarose_joomla-2009-12-30.sql.gz
./webbmaster_bellarose_joomla-2009-12-31.sql.gz
./webbmaster_bellarose_joomla-2010-01-01.sql.gz
./webbmaster_bellarose_joomla-2010-01-02.sql.gz
./webbmaster_bellarose_joomla-2010-01-03.sql.gz
./webbmaster_bellarose_joomla-2010-01-04.sql.gz
./webbmaster_bellarose_joomla-2010-01-05.sql.gz
./webbmaster_bellarose_joomla-2010-01-06.sql.gz
./webbmaster_bellarose_joomla-2010-01-07.sql.gz
./webbmaster_bellarose_joomla-2010-01-08.sql.gz
./webbmaster_bellarose_joomla-2010-01-09.sql.gz
./webbmaster_bellarose_joomla-2010-01-10.sql.gz
./webbmaster_bellarose_joomla-2010-01-11.sql.gz
./webbmaster_bellarose_joomla-2010-01-12.sql.gz
./webbmaster_bellarose_joomla-2010-01-13.sql.gz
./webbmaster_bellarose_joomla-2010-01-14.sql.gz
./webbmaster_bellarose_joomla-2010-01-15.sql.gz
./webbmaster_bellarose_joomla-2010-01-16.sql.gz
./webbmaster_bellarose_joomla-2010-01-17.sql.gz
./webbmaster_bellarose_joomla-2010-01-18.sql.gz
./webbmaster_bellarose_joomla-2010-01-19.sql.gz
./webbmaster_bellarose_joomla-2010-01-20.sql.gz
./webbmaster_bellarose_joomla-2010-01-21.sql.gz
./webbmaster_bellarose_joomla-2010-01-22.sql.gz
./webbmaster_bellarose_joomla-2010-01-23.sql.gz
./webbmaster_bellarose_joomla-2010-01-24.sql.gz
./webbmaster_bellarose_joomla-2010-01-25.sql.gz
./webbmaster_bellarose_joomla-2010-01-26.sql.gz
./webbmaster_bellarose_joomla-2010-01-27.sql.gz
./webbmaster_bellarose_joomla-2010-01-28.sql.gz
./webbmaster_bellarose_joomla-2010-01-29.sql.gz
/tmp/so>cat targets_for_purging
./webbmaster_bellarose_joomla-2009-12-17.sql.gz
./webbmaster_bellarose_joomla-2009-12-18.sql.gz
./webbmaster_bellarose_joomla-2009-12-19.sql.gz
./webbmaster_bellarose_joomla-2009-12-20.sql.gz
./webbmaster_bellarose_joomla-2009-12-21.sql.gz
./webbmaster_bellarose_joomla-2009-12-22.sql.gz
./webbmaster_bellarose_joomla-2009-12-23.sql.gz
./webbmaster_bellarose_joomla-2009-12-24.sql.gz
./webbmaster_bellarose_joomla-2009-12-25.sql.gz
./webbmaster_bellarose_joomla-2009-12-26.sql.gz
./webbmaster_bellarose_joomla-2009-12-27.sql.gz
./webbmaster_bellarose_joomla-2009-12-28.sql.gz
./webbmaster_bellarose_joomla-2009-12-29.sql.gz
./webbmaster_bellarose_joomla-2009-12-30.sql.gz
./webbmaster_bellarose_joomla-2009-12-31.sql.gz
./webbmaster_bellarose_joomla-2010-01-01.sql.gz
./webbmaster_bellarose_joomla-2010-01-02.sql.gz
./webbmaster_bellarose_joomla-2010-01-03.sql.gz
./webbmaster_bellarose_joomla-2010-01-04.sql.gz
./webbmaster_bellarose_joomla-2010-01-05.sql.gz
./webbmaster_bellarose_joomla-2010-01-06.sql.gz
./webbmaster_bellarose_joomla-2010-01-07.sql.gz
./webbmaster_bellarose_joomla-2010-01-08.sql.gz
./webbmaster_bellarose_joomla-2010-01-09.sql.gz
./webbmaster_bellarose_joomla-2010-01-10.sql.gz
./webbmaster_bellarose_joomla-2010-01-11.sql.gz
./webbmaster_bellarose_joomla-2010-01-12.sql.gz
./webbmaster_bellarose_joomla-2010-01-13.sql.gz
./webbmaster_bellarose_joomla-2010-01-14.sql.gz
./webbmaster_bellarose_joomla-2010-01-15.sql.gz
./webbmaster_bellarose_joomla-2010-01-16.sql.gz
./webbmaster_bellarose_joomla-2010-01-17.sql.gz
./webbmaster_bellarose_joomla-2010-01-18.sql.gz
./webbmaster_bellarose_joomla-2010-01-19.sql.gz
./webbmaster_bellarose_joomla-2010-01-20.sql.gz
./webbmaster_bellarose_joomla-2010-01-21.sql.gz
./webbmaster_bellarose_joomla-2010-01-22.sql.gz
/tmp/so>sed 's/\(.\{29\}\)_\(..-..\)-\(....\)/\1-\3-\2/' existing_files | sort > existing_files.8601
/tmp/so>sed 's/\(.\{29\}\)_\(..-..\)-\(....\)/\1-\3-\2/' targets_for_purging | sort > targets_for_purging.8601
/tmp/so>mkdir files
/tmp/so>cd files
/tmp/so/files>xargs touch < ../existing_files.8601
/tmp/so/files>ls -1
webbmaster_bellarose_joomla-2009-12-17.sql.gz
webbmaster_bellarose_joomla-2009-12-18.sql.gz
webbmaster_bellarose_joomla-2009-12-19.sql.gz
webbmaster_bellarose_joomla-2009-12-20.sql.gz
webbmaster_bellarose_joomla-2009-12-21.sql.gz
webbmaster_bellarose_joomla-2009-12-22.sql.gz
webbmaster_bellarose_joomla-2009-12-23.sql.gz
webbmaster_bellarose_joomla-2009-12-24.sql.gz
webbmaster_bellarose_joomla-2009-12-25.sql.gz
webbmaster_bellarose_joomla-2009-12-26.sql.gz
webbmaster_bellarose_joomla-2009-12-27.sql.gz
webbmaster_bellarose_joomla-2009-12-28.sql.gz
webbmaster_bellarose_joomla-2009-12-29.sql.gz
webbmaster_bellarose_joomla-2009-12-30.sql.gz
webbmaster_bellarose_joomla-2009-12-31.sql.gz
webbmaster_bellarose_joomla-2010-01-01.sql.gz
webbmaster_bellarose_joomla-2010-01-02.sql.gz
webbmaster_bellarose_joomla-2010-01-03.sql.gz
webbmaster_bellarose_joomla-2010-01-04.sql.gz
webbmaster_bellarose_joomla-2010-01-05.sql.gz
webbmaster_bellarose_joomla-2010-01-06.sql.gz
webbmaster_bellarose_joomla-2010-01-07.sql.gz
webbmaster_bellarose_joomla-2010-01-08.sql.gz
webbmaster_bellarose_joomla-2010-01-09.sql.gz
webbmaster_bellarose_joomla-2010-01-10.sql.gz
webbmaster_bellarose_joomla-2010-01-11.sql.gz
webbmaster_bellarose_joomla-2010-01-12.sql.gz
webbmaster_bellarose_joomla-2010-01-13.sql.gz
webbmaster_bellarose_joomla-2010-01-14.sql.gz
webbmaster_bellarose_joomla-2010-01-15.sql.gz
webbmaster_bellarose_joomla-2010-01-16.sql.gz
webbmaster_bellarose_joomla-2010-01-17.sql.gz
webbmaster_bellarose_joomla-2010-01-18.sql.gz
webbmaster_bellarose_joomla-2010-01-19.sql.gz
webbmaster_bellarose_joomla-2010-01-20.sql.gz
webbmaster_bellarose_joomla-2010-01-21.sql.gz
webbmaster_bellarose_joomla-2010-01-22.sql.gz
webbmaster_bellarose_joomla-2010-01-23.sql.gz
webbmaster_bellarose_joomla-2010-01-24.sql.gz
webbmaster_bellarose_joomla-2010-01-25.sql.gz
webbmaster_bellarose_joomla-2010-01-26.sql.gz
webbmaster_bellarose_joomla-2010-01-27.sql.gz
webbmaster_bellarose_joomla-2010-01-28.sql.gz
webbmaster_bellarose_joomla-2010-01-29.sql.gz
/tmp/so/files>ls *sql* | tac | sed 1,7d | tr '\012' '\000' | xargs -0 --no-run-if-empty rm
/tmp/so/files>ls -1 ./*
./webbmaster_bellarose_joomla-2010-01-23.sql.gz
./webbmaster_bellarose_joomla-2010-01-24.sql.gz
./webbmaster_bellarose_joomla-2010-01-25.sql.gz
./webbmaster_bellarose_joomla-2010-01-26.sql.gz
./webbmaster_bellarose_joomla-2010-01-27.sql.gz
./webbmaster_bellarose_joomla-2010-01-28.sql.gz
./webbmaster_bellarose_joomla-2010-01-29.sql.gz
/tmp/so/files>comm -3 ../existing_files.8601 ../targets_for_purging.8601
./webbmaster_bellarose_joomla-2010-01-23.sql.gz
./webbmaster_bellarose_joomla-2010-01-24.sql.gz
./webbmaster_bellarose_joomla-2010-01-25.sql.gz
./webbmaster_bellarose_joomla-2010-01-26.sql.gz
./webbmaster_bellarose_joomla-2010-01-27.sql.gz
./webbmaster_bellarose_joomla-2010-01-28.sql.gz
./webbmaster_bellarose_joomla-2010-01-29.sql.gz
/tmp/so/files>

答案 1 :(得分:2)

如果文件都在一个目录中(即你不需要使用find来递归子目录),你可以使用ls(1)的-t参数按时间排序文件列表然后删除第一个带尾部的7个条目(1)。

rm -f $(ls -t *sql* | tail -n +8)

[编辑:我添加了-f以防替换导致空列表]

或循环......

ls -t *sql* | tail -n +8 | while read file
    echo "Purging: $file"
    rm "$file"
done

tail的参数是+8,因为你希望它从第8行开始拖尾(即删除前7行)。

删除文件后,您可以刻录剩下的文件。

答案 2 :(得分:0)

OLDIFS="$IFS"
IFS=$'\n'
topurge=($(find -mindepth 1 -printf "%T@ %P\n" | sort -n -r | cut -d' ' -f 2- | tail -n +7))
IFS="$OLDIFS"

答案 3 :(得分:0)

这是一个想法:

oldest_to_keep=`find . -name \*.sql | xargs \ls -1rc | tail -7 | head -1`
find . -name \*.sql -not -samefile $oldest_to_keep -not -newer $oldest_to_keep

如果您确信自己这是理智的,那么第二个发现应该-exec rm \{\} ;。 :)