清理脚本

#!/bin/bash
# Author      : standby@gmail.com
# Date        : 2018-02-27

NGX_CONF="/usr/local/nginx/conf/nginx.conf"

# Get the dev which contains ./liveroot/ 
function get_point()
{
    arr=(`df -HT |grep -v 'Filesystem' |grep -E '[0-9]{1,2}T' |awk '{print $NF}'`)
    for dev in ${arr[*]}
    do
        [ -d $dev"/liveroot/live" ] && point=$dev && break
    done
    echo $point
}

# Start here...
point=$(grep '/liveroot;' $NGX_CONF |awk -F / '{print "/"$2}')
if [ ! -z "$point" ]
then
    res=$(echo $point |grep -v ' ' |wc -l)
    [ $res -eq 0 ] && point=`get_point`
else
    point=`get_point`
fi

# [live] Touch the stream-name.data file to avoiding deleted.
for i in $(find $point/liveroot/live -type d ! -name "*mon" |awk -F / '{print $5}')
do
    [ -f $point"/liveroot/live/"$i"/"$i".data" ] && touch $point/liveroot/live/$i/$i.data
done
# [tslive] Touch the stream-name.data file to avoiding deleted.
for i in $(find $point/liveroot/tslive -type d ! -name "*mon" |awk -F / '{print $5}')
do
    [ -f $point"/liveroot/tslive/"$i"/"$i".data" ] && touch $point/liveroot/tslive/$i/$i.data
done

# Clean the old files which contains [*.data | *.pht | *.ts | core.* | temp_* | *.m3u8 | *.log] and keep 3 days only.
find $point/liveroot/live -ctime +2 -type f -name "*.data" -exec /bin/rm -f {} ;
find $point/liveroot/live -ctime +2 -type f -name "*.pht" -exec /bin/rm -f {} ;
find $point/liveroot/live -ctime +2 -type f -name "*.m3u8" -exec /bin/rm -f {} ;
find $point/liveroot/live -ctime +2 -type f -name "*.log" -exec /bin/rm -f {} ;
find $point/liveroot/live -ctime +2 -type f -name "core.*" -exec /bin/rm -f {} ;
find $point/liveroot/live -ctime +2 -type f -name "temp_*" -exec /bin/rm -f {} ;
#find $point/liveroot/tslive -ctime +2 -type f -name "*.ts" -exec /bin/rm -f {} ;
find $point/liveroot/tslive -path $point"/liveroot/tslive/c1_mon" -prune -o -ctime +2 -type f -name "*.ts" -exec /bin/rm -f {} ;
find $point/liveroot/tslive -ctime +2 -type f -name "*.pht" -exec /bin/rm -f {} ;
find $point/liveroot/tslive -ctime +2 -type f -name "*.m3u8" -exec /bin/rm -f {} ;
find $point/liveroot/tslive -ctime +2 -type f -name "*.log" -exec /bin/rm -f {} ;
find $point/liveroot/tslive -ctime +2 -type f -name "core.*" -exec /bin/rm -f {} ;
find $point/liveroot/tslive -ctime +2 -type f -name "temp_*" -exec /bin/rm -f {} ;
# Clean the ffmpeg log and keep 3 days only.
find $point/liveroot/livelog -ctime +2 -type f -name "*.log" -exec /bin/rm -f {} ;

# [live] Clean the old ./liveroot/live/some_sub_dirs which cleated 5 days ago and has less 5 files contained.
for i in $(find $point/liveroot/live -ctime +4 -type d ! -name "*_mon")
do
    [ $i == $point"/liveroot/live" ] && continue
    #ls -ld --time-style=long-iso $i |awk '{print $6" "$7}'
    num=$(ls -l $i |grep '^-' |wc -l)
    [ $num -lt 5 ] && rm -rf $i
done
# [tslive] Clean the old ./liveroot/tslive/some_sub_dirs which cleated 5 days ago and has less 5 files contained.
for i in $(find $point/liveroot/tslive -ctime +4 -type d ! -name "*_mon")
do
    [ $i == $point"/liveroot/tslive" ] && continue
    num=$(ls -l $i |grep '^-' |wc -l)
    [ $num -lt 5 ] && rm -rf $i
done

  

作者:Standby一生热爱名山大川、草原沙漠,还有妹子
出处:http://www.cnblogs.com/standby/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/standby/p/6843500.html