autoclear 自动清理过期文件

警告
本文最后更新于 2023-02-21,文中内容可能已过时。

自动清理超过一定时间的文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env bash
mkdir -p $HOME/log
LOG_FILE=$HOME/log/$(date +"%Y%m%d.MyLog.log")
exec &> >(tee -a "$LOG_FILE")

is_init=false
msg () {
    echo -e "------------------------------------------------------------------"
    if [ !is_init ]; then
        is_init=true
        echo -e ">> $(date +'%Y-%m-%d %H:%M:%S') << '$0'"
    fi

    echo -e ">> $(date +'%Y-%m-%d %H:%M:%S') << '$1'"
    echo -e  "-----------------------------------------------------------------"
}

msg "Run autoclear.sh"

#datadir=$HOME/data
datadir=/data
xday=$(date -d "-30 days" +"%Y%m%d")

for f in `ls $datadir`;
do
    tmpdate=`date -r $datadir/$f "+%Y%m%d"`

    if [[ $tmpdate < $xday ]];
    then
        echo "##==> now rm $f ==> [[ $tmpdate < $xday ]]"

        if [[ $f == *"tar.bz2"* ]];
        then
            rsync -avPzr $datadir/$f ops:/data/Xtp/FromZZ.SZ
        fi

        rm -rf $datadir/$f
    fi

done

msg "Done!"

相关内容

william 支付宝支付宝
william 微信微信
0%