User Tools

Site Tools


linux:purging_script

This is an old revision of the document!


Description

Have you ever fight with logrotate configuration files? Did you survived? Cool, this document is not for you xD This is script is a very simple method to drop unnecessary files from your filesystems (logs?) and rotate them so they don't growth forever :-P

Script Code

purge_files.sh
#!/bin/bash
# 
# (C) dodger@ciberterminal.net
# 
# 
 
# Exit codes:
#  1 : No config file
#  2 : Wrong option in the config file
#  3 : 
#  4 : 
#  5 : 
#  6 : 
 
########################################################################
#
# CONSTANTS
#
########################################################################
 
# colors
LIGHTGREEN="\033[1;32m"
LIGHTRED="\033[1;31m"
WHITE="\033[0;37m"
RESET="\033[0;00m"
 
 
########################################################################
#
# / CONSTANTS
#
########################################################################
 
 
 
########################################################################
#
# VARIABLES
#
########################################################################
 
MYDATE=$(date +%Y%m%d%H%M)
CONFIGFILE=$(dirname $0)/$(basename $0 .sh).config
LOGDIR=CONFIG=$(dirname $0)/logs/
 
 
########################################################################
#
# / VARIABLES
#
########################################################################
 
 
########################################################################
#
# FUNCTIONS
#
########################################################################
 
 
usage()
{
    printf "%s${LIGHTRED}USAGE:${RESET}
    $0
 
    PLEASE READ
    https://sites.google.com/a/voxelgroup.net/sistemas/oracle/documentacion-tecnica/rotado-de-logs-y-trazas\n"
    # VERY INITIAL CHECKS
}
 
remove_files()
{
    local AUX=""
    local OPT="-${1}"
    local DIR="${2}"
    if [[ "${DIR,,}" = "dir" ]] ; then
        local TYPEOF="-type d"
        local FINDCMD="-print | xargs rm -fr"
        local MESSAGE="$(date +%Y%m%d%H%M%S) : Deleting folders inside ${FOLDER}, with filemask ${MASK}, ${OPT} ${PARAM1}"
    else
        local TYPEOF="-type f"
        local FINDCMD="-delete"
        local MESSAGE="$(date +%Y%m%d%H%M%S) : Deleting files on ${FOLDER}, with filemask ${MASK}, ${OPT} ${PARAM1}"
    fi
 
    [[ "${PARAM2}" ]] && AUX="-maxdepth ${PARAM2}" && MESSAGE="%s${MESSAGE} and maxdepth ${PARAM2}"
    printf "%s${MESSAGE}\n"
    # workaround for eval "bug"
#    local AUXDIR="/tmp/$(date +%s)"
#    mkdir ${AUXDIR}
#    cd ${AUXDIR}
    eval find ${FOLDER} ${AUX} -name "${MASK}" ${TYPEOF} ${OPT} ${PARAM1} ${FINDCMD}
#    cd ${OLDPWD}
#    rm -fr ${AUXDIR}
}
 
rotate_files()
{
    local USEZIP="$1"
    local MESSAGE="$(date +%Y%m%d%H%M%S) : Rotating files on ${FOLDER}, with filemask ${MASK}, -mtime ${PARAM1}"
    [[ "${PARAM2}" ]] && AUX="-maxdepth ${PARAM2}" && MESSAGE="${MESSAGE}, maxdepth ${PARAM2}"
    printf "%s${MESSAGE}\n"
#     printf "%s$(date +%Y%m%d%H%M%S) : Rotating files on ${FOLDER}, with filemask ${MASK}, -mtime ${PARAM1}"
#     [[ "${PARAM2}" ]] && AUX="-maxdepth ${PARAM2}" && printf "%s, maxdepth ${PARAM2}"
 
    case $USEZIP in
        "zip" )
            [[ ! "${PARAM3}" ]] && PARAM3="-9fv"
            local MOVECMD="gzip -c ${PARAM3}"
            local DSTFILE="${MYDATE}.gz"
            printf "%s and gzipping with ${PARAM3}\n"
        ;;
        "nozip" )
            local MOVECMD="cat"
            local DSTFILE="${MYDATE}"
            printf "%s\n"
        ;;
        * )
            printf "%s${LIGHTRED}ERROR ON rotate_files function${RESET}"
            return 1
        ;;
    esac
    while read FILE ; do
        echo ${FILE}
        ${MOVECMD} "${FILE}" > "${FILE}.${DSTFILE}"
         > ${FILE}
    done < <(find ${FOLDER} ${AUX} -name "${MASK}" -type f -size ${PARAM1^^})
}
 
########################################################################
#
# / FUNCTIONS
#
########################################################################
 
 
########################################################################
#
#  MAIN
#
########################################################################
 
 
 
 
 
# exec 1>> ${LOGDIR}/$(basename $0 .sh).log
# exec 2>> ${LOGDIR}/$(basename $0 .sh).err
 
 
if [ ! -f ${CONFIGFILE} ]; then
        echo "No configuration file found" 
        exit 1
fi
 
 
 
# workaround for eval "bug"
AUXDIR="/tmp/$(date +%s)"
mkdir ${AUXDIR}
cd ${AUXDIR}
#/ workaround for eval "bug"
 
while read LINE ; do
 
 
#     if [[ ! "${LINE,,}" =~ ^\/[a-z0-9]{1,}/[\/\_\-\ a-z0-9]{1,},[\*\.\-\_\ \#a-z0-9]{3,20},(bydate|bymin|rotate|rotatezip|zip),\+[0-9]{1,3},(|\-[a-z0-9]{1,10}),$ ]] ; then
    if [[ ! "${LINE,,}" =~ ^\/[a-z0-9]{1,}\/[\.\/\_\ a-z0-9\-]{1,},[\*\.\_\ \#a-z0-9\-]{3,20},(dirbydate|dirbymin|bydate|bymin|rotate|rotatezip|zip),\+[0-9]{1,3}(|[kmg]),(|[0-9]{1,3}),(|\-[a-z0-9]{1,10})$ ]] ; then
        printf "%sskipping ${LINE}\nnot matching the config pattern, read instructions\n"
        continue
    fi
 
    FOLDER=$(echo $LINE | cut -d',' -f1)
    MASK=$(echo $LINE | cut -d',' -f2)
    ACTION=$(echo $LINE | cut -d',' -f3)
    PARAM1=$(echo $LINE | cut -d',' -f4)
    PARAM2=$(echo $LINE | cut -d',' -f5)
    PARAM3=$(echo $LINE | cut -d',' -f6)
 
    case ${ACTION,,} in
        "bydate" )
            remove_files mtime
        ;;
        "bymin" )
            remove_files mmin
        ;;
       "dirbydate" )
            remove_files mtime dir
        ;;
        "dirbymin" )
            remove_files mmin dir
        ;;
        "rotate" )
            rotate_files nozip
        ;;
        "rotatezip" )
            rotate_files zip
        ;;
        "zip" )
            printf "%s$(date +%Y%m%d%H%M%S) : Gzipping files on ${FOLDER}, with filemask ${MASK}, ${OPT} ${PARAM1}"
            [[ "${PARAM2}" ]] && AUX="-maxdepth ${PARAM2}" && printf "%s and maxdepth ${PARAM2}\n" || printf "%s\n"
            [[ ! "${PARAM3}" ]] && PARAM3="-9fv"
            find ${FOLDER} ${AUX} -name "${MASK}" -type f -mtime ${PARAM1} -exec gzip -9fv {} \;
        ;;
        * )
            usage
            exit 2
        ;;
    esac
 
done < <(cat ${CONFIGFILE} | egrep -v "^#|^$")
 
 
 
# workaround for eval "bug"
cd ${OLDPWD}
rm -fr ${AUXDIR}
#/ workaround for eval "bug"
 
########################################################################
#
# / MAIN
#
########################################################################
linux/purging_script.1381397867.txt.gz · Last modified: 2013/10/10 09:37 by dodger