Home > bash, CentOS, RHEL, Slackware > Linux bash: truncate custom logs not able to be moved

Linux bash: truncate custom logs not able to be moved

rotate.custom.logs.sh

#!/bin/sh
# Barcelona, Sat, 12 Feb 2011 23:06:03 +0100
# seikath@gmail.com
# 
CONF_FILE="${0/.sh/}.conf"
LOGDATE=$(date  +"%Y-%m-%d" -d yesterday)
DESTDIR_DEF="/backups"


if [ -f "${CONF_FILE}" ]
then 

while read settings
do

if [ "${settings:0:1}" == "#" ];then continue;fi

LOGTOROTATE=${settings/,*/}
DESTDIR=${settings/*,/}
if [ "${DESTDIR}" == "${LOGTOROTATE}"];then DESTDIR="";fi
if [ "${DESTDIR}" == ""];then DESTDIR="${DESTDIR_DEF}";fi

if [ ! -f "${LOGTOROTATE}" ]
then
    echo "$(date) : missing file to rotate: ${LOGTOROTATE}"
    continue
fi

if [ ! -d "${DESTDIR}" ]
then
    echo "$(date) : missing ${DESTDIR} - executing mkdir -p ${DESTDIR}"
    mkdir -p "${DESTDIR}"
fi


FILESIZE_ORIGINAL=$(du -sh "${LOGTOROTATE}" | awk '{gsub("[^[:digit:]]","",$1);print $1}')
LINENUMB_ORIGINAL=$(wc -l "${LOGTOROTATE}" | awk '{print $1}')

if [ -f "${LOGTOROTATE}" -a ${FILESIZE_ORIGINAL} -ge 0 -a ${LINENUMB_ORIGINAL} -ge 2 ]
then
    BCKP_NAME="${DESTDIR}/$(hostname).$(basename ${LOGTOROTATE}).${LOGDATE}"
    BCKP_NAME=${BCKP_NAME/$HOSTNAME.$HOSTNAME./$HOSTNAME.}
    echo "$(date) : start copying ${LOGTOROTATE} to ${BCKP_NAME}"
    if cp -vv "${LOGTOROTATE}" "${BCKP_NAME}"
    then
        echo "$(date) : end copy  ${LOGTOROTATE} to ${BCKP_NAME}"
        FILESIZE_BCKP=$(du -sh "${BCKP_NAME}" | awk '{gsub("[^[:digit:]]","",$1);print $1}')
        LINENUMB_BCKP=$(wc -l "${BCKP_NAME}" | awk '{print $1}')
        if [ -f "${BCKP_NAME}" -a ${FILESIZE_BCKP} -ge 0 -a ${LINENUMB_BCKP} -ge 2 ]
        then
            echo "$(date) : status : $(du -sh ${BCKP_NAME})"
            echo "$(date) : truncating ${LOGTOROTATE}"
            echo "$(date) : truncated by ${0}" > ${LOGTOROTATE}                                                                                  
        else
            echo "$(date) : ${BCKP_NAME} is non existing: $(file ${BCKP_NAME}) or size is ${FILESIZE_BCKP} or lines are ${LINENUMB_BCKP}"
        fi
    else
        echo "$(date) : error copying ${LOGTOROTATE} to ${BCKP_NAME}"
    fi
else
    echo "$(date) : ${LOGTOROTATE} is non existing: $(file ${LOGTOROTATE}) or size is ${FILESIZE_ORIGINAL} or lines are ${LINENUMB_ORIGINAL}"
fi

done < "${CONF_FILE}"

else
    echo "$(date) : ${CONF_FILE} is empry or missing"
fi

rotate.custom.logs.conf

root@xxxx:[Fri Feb 18 12:15:36]:[/apps/]$ cat rotate.custom.logs.conf
# file_name_to_rotate,directory_to_store_rotated
/var/log/xxxx.restart.apache.on.high.cpu.usage.log
/var/log/xxxx.monitor.api.log
/srv/www/web/force.log
/var/log/httpd/xxxx.wsgi.ssl_request_log,/var/log/httpd
/var/log/httpd/xxxx.x.xxxx-xxxx.com.wsgi.ssl_request_log,/var/log/httpd
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: