简体   繁体   English

用于删除旧备份文件的现有 bash 脚本 - 它是如何工作的?

[英]existing bash script for deleting old backup files - how does it work?

I'm a total beginner with scripts.我是一个脚本初学者。 I have some questings regarding an old script, which should delete old backup files.我有一些关于旧脚本的问题,它应该删除旧的备份文件。

KEEP_FULL=7
KEEP_DAILY=14
KEEP_WEEKLY=30

DB_FULL_PATH=/Volumes/path

LAST_DAY=0
LAST_WEEK=0
LAST_MONTH=0
find $DB_FULL_PATH -type f| while read f; do
    < <(stat -f %Sm -t "%m %V %d" $f) read -s MONTH WEEK DAY 
    if [ $DAY -eq $LAST_DAY ]; then
        find $f -mtime +$KEEP_FULL | xargs rm 
    else if [ $WEEK -eq $LAST_WEEK ]; then
        find $f -mtime +$KEEP_DAILY | xargs rm 
    else if [ $MONTH -eq $LAST_MONTH ]; then
        find $f -mtime +$KEEP_WEEKLY | xargs rm 
    fi fi fi
    export LAST_DAY=$DAY
    export LAST_WEEK=$WEEK
    export LAST_MONTH=$MONTH
done

Could someone explain (for dummies) what happens within the while-loop?有人可以解释(对于傻瓜)while 循环中发生了什么? I understand that for each file within the folder the information (day, week, month of creation) is written into $MONTH $WEEK $DAY.我知道对于文件夹中的每个文件,信息(创建日、周、月)都写入 $MONTH $WEEK $DAY。 But the following logic I don't understand correctly.但是下面的逻辑我没有正确理解。

This answer won't be complete because I lack informations.这个答案不会完整,因为我缺乏信息。 Also It seems overly complex for the task it should do.此外,它应该完成的任务似乎过于复杂。


The loop below process all files under /Volumes/path and stores them in the variable f .下面的循环处理 /Volumes/path 下的所有文件并将它们存储在变量f

find $DB_FULL_PATH -type f| while read f; do
    # [...]
done

For each file f , this command is first performed:对于每个文件f ,首先执行此命令:

< <(stat -f %Sm -t "%m %V %d" $f) read -s MONTH WEEK DAY 
  1. stat is invoked on the file f对文件f调用stat

    -f format Display information using the specified format. -f format使用指定的格式显示信息。 See the FORMATS section for a description of valid formats.有关有效格式的说明,请参阅 FORMATS 部分。

    -t timefmt Display timestamps using the specified format. -t timefmt使用指定格式显示时间戳。 This format is passed directly to strftime (3).这种格式直接传递给strftime (3)。

    • According to the FORMATS section of the manual page, %Sm is the combination of S and m , which stand respectively for 1) "the [last] time file was [...] modified " ( m ) and 2) formatted, "if applicable, should be in string format" ( S , it should be the case here).根据手册页的格式部分, %SmSm的组合,它们分别代表 1) “[上次] 时间文件被 [...]修改” ( m ) 和 2) 格式化,“如果适用,应该是字符串格式”( S ,这里应该是这种情况)。
    • According to the manual page of strftime , %m %V %d stand for the month , the week number of the year and the day of the month as a decimal number根据strftime的手册页, %m %V %d代表月份年份中周数和月份中的几天作为十进制数
  2. The output is read and stored in variables MONTH WEEK and DAY ( -s stands for silent mode )输出被read并存储在变量MONTH WEEKDAY-s代表静默模式

At this point, you have the month , the week number of the year and the day of the month of the time the file was modifed.在这一点上,你有一个月一年的周数和文件被修饰的时候一个月的日子

Next the hardest part:接下来是最难的部分:

  • If the day of modification of the previously processed file equals the current processed file one, then check if it was modified ( -mtime ) one week ago ( KEEP_FULL =7) ;如果先前处理的文件的修改日期等于当前处理的文件的日期,则检查它是否在一周前被修改( -mtime )( KEEP_FULL =7); if so, delete it ( f is passed to rm with xargs ):如果是这样,请删除它( f使用xargs传递给rm ):

     if [ $DAY -eq $LAST_DAY ]; then find $f -mtime +$KEEP_FULL | xargs rm fi
  • Otherwise, if the week number of the year of modification of the previously processed file ( breathes ) equals the current processed file one, then check if it was modified two weeks ago ( KEEP_DAILY =14) ;否则,如果之前处理过的文件( breaths )修改年份的周数等于当前处理过的文件的周数,则检查两周前是否修改过( KEEP_DAILY =14); if so, delete it:如果是这样,请删除它:

     else if [ $WEEK -eq $LAST_WEEK ]; then find $f -mtime +$KEEP_DAILY | xargs rm
  • Otherwise, if the month of modification of the previously processed file ( sips ) equals the current processed file one, then check if it was modified 30 days ago ( KEEP_WEEKLY =30) ;否则,如果先前处理的文件( sips )的修改月份等于当前处理的文件一,则检查它是否在 30 天前修改过( KEEP_WEEKLY =30 ); if so, delete it:如果是这样,请删除它:

     else if [ $MONTH -eq $LAST_MONTH ]; then find $f -mtime +$KEEP_WEEKLY | xargs rm fi fi fi
  • Retrieve the month, the week and the day of the month of the current file to process the next one with them (will be used as parts of the modification time of said "previously processed" file):检索当前文件的月份、星期和月份的日期以处理下一个文件(将用作所述“先前处理”文件的修改时间的一部分):

     export LAST_DAY=$DAY export LAST_WEEK=$WEEK export LAST_MONTH=$MONTH done

That said, to understand why such comparisons are performed, you need to know the order the first find outputs files injected to the standard input of the while loop.也就是说,要理解为什么要执行这样的比较,您需要知道第一个find注入到while循环标准输入的输出文件的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM