MongoDB备份清理脚本

清理脚本根据上一篇(MongoDB备份脚本)而来,用在其他地方需注意文件名,因为并不是用

find . -mtime +31 -type f -name *.* -exec rm -rf {} \;

只是为了练手


#!/bin/bash

cd /usr/backup/mongodb
year=`date '+%Y'`
month=`date '+%m'`
day=`date '+%d'`

for file_name in `ls`
do
    file_path=${file_name:0:7}

    if [[ $file_path == mongodb ]]
    then
        file_year=${file_name:8:4}
        file_month=${file_name:13:2}
        file_day=${file_name:16:2}

            if [[ $file_year -eq $year ]]
            then
                if [[ `expr $month - $file_month` -gt 2 || (`expr $month - $file_month` -eq 2 && $file_day -lt $day) ]]
                then
                    echo $file_name
                fi
           elif [[ $file_year -lt $year ]]
           then
               if [ $file_month -le $month ]
               then
                   rm $file_name
               elif [ $file_month -gt $month ]
               then
                   temp=`expr $month + 12`
                   if [[ `expr $temp - $file_month` -gt 2 || (`expr $temp - $file_month` -eq 2 && $file_day -lt $day) ]]
                   then
                       rm $file_name
                   fi
              fi
          fi
    fi
done

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注