码迷,mamicode.com
首页 > 系统相关 > 详细

shell脚本日志清理和压缩

时间:2015-05-20 02:17:09      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:crontab shell compress tar

1、日志结构
/opt/online/{123com,abccom}/log/online[1-9]/[a-z]

2、变量说明
#是否开启压缩功能(0:关闭,1:开启;若关闭压缩功能,则compressdayn无效)
needcompress=1
删除14天以前的日志
deldayn=14
前1天
lastdayn=1
服务类型
allsvrtype="online1 online2 online3"
压缩5天前的日志
compressdayn=5

3、计划任务使用方法:
将该脚本本身copy到/opt/online下;
crontab中配置成:
#30 0 * * * cd /opt/online && chmod +x cleanlog.sh && ./cleanlog.sh >/dev/null 2>&1

cat cleanlog.sh
#!/bin/bash
#--------------------------------------------------  
#Created:2015-05-18
#Author:jimmygong
#Mail:jimmygong@taomee.com
#Function:
#Version:1.0
#--------------------------------------------------
allsvrtype="online1 online2 online3"
deldayn=14
needcompress=1
compressdayn=5
lastdayn=1

alldomain="123com abccom"
allsubdir="a b c d e f g h i j k l m n o p q r s t u v w x y z"
rootdir="/opt/online"
dirtobedelname="tobedel"

lastday=`date -d "$lastdayn days ago" +%Y%m%d`
compressday=`date -d "$compressdayn days ago" +%Y%m%d`
delday=`date -d "$deldayn days ago" +%Y%m%d`

function cleandir ()
{
    local ldir="$1"
    local lfiles=
    local lfile=

    lfiles=`ls -1 $ldir | xargs`
    for lfile in $lfiles
    do
        rm -f $ldir/$lfile
    done
}

function compressdir ()
{
    local ldir="$1"
    local lfiles=
    local lfile=
    local lcompressedtarget="$ldir/$compressday.tar.gz"
    # to prevent duplicate-compress
    if [[ -f $lcompressedtarget ]]
    then
        return
    fi
    tar zcvf $ldir.tar.gz $ldir
    if [[ $? -ne 0 ]] ; then
        return
    fi
    lfiles=`ls -1 $ldir | xargs`
    for lfile in $lfiles
    do
        rm -f $ldir/$lfile
    done
    if [[ -f $ldir.tar.gz ]] ; then
        mv $ldir.tar.gz $lcompressedtarget
    fi
}

function handleonelogdir ()
{
    local ldir="$1"
    local lscandir="$ldir/$dirtobedelname"
    local lmvdir="$lscandir/$lastday"
    local lones=
    local lone=
    local lonefull=
    mkdir -p $lscandir
    # remove
    lones=`ls -1 $lscandir | grep $delday | xargs`
    for lone in $lones
    do
        lonefull="$lscandir/$lone"
        if [[ -d $lonefull ]] ; then
            cleandir $lonefull
            rm -rf $lonefull
        elif [[ -f $lonefull ]] ; then
            rm -f $lonefull
        fi
    done
    # compress
    if [[ $needcompress -eq 1 ]]
    then
        lones=`ls -1 $lscandir | grep $compressday | xargs`
        for lone in $lones
        do
            lonefull="$lscandir/$lone"
            if [[ -d $lonefull ]]
            then
                compressdir $lonefull
            elif [[ -f $lonefull ]]
            then
                tar zcvf $lonefull.tar.gz $lonefull
            fi
        done
    fi
    # move last day‘s data
    mkdir -p $lmvdir
    lones=`ls -1 $ldir | grep $lastday | xargs`
    for lone in $lones
    do
        mv $ldir/$lone $lmvdir
    done
}

for domain in $alldomain
do
    domaindir="$rootdir/$domain"
    if [[ ! -d $domaindir ]]
    then
        continue
    fi
    for svrtp in $allsvrtype
    do
        svrtplogdir="$domaindir/log/$svrtp"
        if [[ ! -d $svrtplogdir ]]
        then
            continue
        fi
        for sub in $allsubdir
        do
            svrtplogsubdir="$svrtplogdir/$sub"
            if [[ ! -d $svrtplogsubdir ]]
            then
                continue
            fi
            handleonelogdir "$svrtplogsubdir"
        done
    done
done

exit 0

本文出自 “7928217” 博客,请务必保留此出处http://7938217.blog.51cto.com/7928217/1652898

shell脚本日志清理和压缩

标签:crontab shell compress tar

原文地址:http://7938217.blog.51cto.com/7928217/1652898

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!