1, log structure/opt/online/{123com,abccom}/log/online[1-9]/[a-z]2, variable description # Whether to turn on the compression function (0: OFF, 1: on; If the compression function is turned off, the Compressdayn is not valid) Needcompress=1 Delete 14 days ago Log deldayn=14 first 1 days lastdayn=1 service type allsvrtype= "Online1 online2 online3" Compress 5 days ago Log compressdayn=53, scheduled task use: Copy the script itself to/opt/online, crontab configured as: #30  0 * * * CD /opt/online && chmod +x cleanlog.sh && ./cleanlog.sh >/dev/null 2>&1cat cleanlog.sh#!/bin/bash#------------------------------------------------ -- #Created: 2015-05-18#author:jimmygong#mail:[email protected] #Function: #Version: 1.0#--------- -----------------------------------------allsvrtype= "Online1 online2 online3" deldayn=14needcompress= 1compressdayn=5lastdayn=1alldomain= "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= "$" local lfiles= local lfile= lfiles= ' ls -1 $ldir | xargs ' for lfile in $ lfiles do rm -f $ldir/$lfile done}function compressdir () { local ldir= "$" 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= "$" 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 $alldomaindo 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 donedoneexit 0
This article is from the "7928217" blog, please be sure to keep this source http://7938217.blog.51cto.com/7928217/1652898
Shell script log cleanup and compression