maniu@securebrain.com:~/script# vi logbackup.sh



#!/bin/bash

#
# logbackup script check all your system's log file remove zero sized ones
# and store others in a tarball and finally put this tarball in ${LOGFOLDER}
# directory. Seems to work too... O_o
#
###############################################################
# WARNING: DO NOT USE IT IF YOU DON'T KNOW WHAT YOU ARE DOING!#
###############################################################
#
# Written by Carlo Mangani (a.k.a. karma)
# This software is GNU, use it at your own risk! ;-)
# Of course thou can copy modify and redistribute it under the terms of GPL General Public Licence
# visit www.gnu.org for further informations.
# Have a nice day!
#


# Variable Definition
# LOGDIR point to system log directory. Don't change!
# TMPDIR used to recurse into $LOGDIR subdirectories
# LOGFOLDER point to the folder in wich you want to store the final tarball, change it if you wish.
# SIZE logbackup work only if the size of entire LOGDIR is bigger than SIZE. Of course you can change it!
# LOGFILE is the format of tar file. By default is day-month-year_sizeB.tar
#   (day/month/year of file creation and LOGSIZE)
# REGENERATEDDIR and REGENERATE contain list of directories and files that need to be re-created after that are
#   been stored.
# Usually you have to check log and cron notification to know whick files and directories you should regenerate.
# DAEMON sysklogd...it will be restarted after logbackup finish his work.
# LOGSIZE size in bytes of LOGDIR.
# COMPRESSOR default program that will be used for compressing LOGFILE. Do not change, use -g option instead.
LOGDIR=/var/log
TMPDIR=${LOGDIR}
LOGFOLDER=/root/old_log/
SIZE=50000
REGENERATEDIR="linuxconf news"
REGENERATE="linuxconf/htmlaccess.log linuxconf/netconf.log linuxconf/boot.log wtmp news/slrnpull.log"
DAEMON=/etc/init.d/sysklogd
LOGSIZE=`du -s ${LOGDIR} | cut -f 1`
LOGFILE=${LOGDIR}/`date +%d`-`date +%m`-`date +%y`_${LOGSIZE}B.tar
COMPRESSOR=bzip2

dirtwork()
{
   for i in `ls ${LOGDIR}`;
        do
           if [ -d ${LOGDIR}/${i} ];
                 then
                        echo -e "\e[32;1mentering ${LOGDIR}/${i}\e[0m";
                        LOGDIR=${TMPDIR}/${i}
                        dirtwork;
                        echo -e "\e[32;1mleaving $i\e[0m";
                        LOGDIR=${TMPDIR}
                 else
                
                        if [ ! -s ${LOGDIR}/${i} ];
                            then
                                       echo -e "\e[31;1mremoving ${LOGDIR}/${i}\e[0m";
                                       rm ${LOGDIR}/${i}
                            else
                                       echo -e "\e[31;1mstoring ${LOGDIR}/${i}\e[0m";
                                       if [ -e ${LOGFILE} ];
                                                   then
                                                               tar rf ${LOGFILE} ${LOGDIR}/${i} &> /dev/null
                                                   else
                                                               tar cf ${LOGFILE} ${LOGDIR}/${i} &> /dev/null
                                       fi
                                       echo -e "\e[31;1mremoving ${LOGDIR}/${i}\e[0m";
                                       rm ${LOGDIR}/${i};
                        fi
           fi                     
        done
                       
}
logbackuphelp()
{
   echo -e "\e[31;1m\tLogBackup Shell Script v1.0\e[0m"
   echo -e "\e[32;1m\nUsage: # sh logbackup.sh \e[0m"
   echo -e "\e[32;1mOptions:\e[0m"
   echo -e "\e[33;1m\t-h = print this message and exit\e[0m"
   echo -e "\e[33;1m\t-f = force logbackup to work even if logdir isn't ${SIZE} bytes large\e[0m"
   echo -e "\e[33;1m\t-g = use gzip instead of ${COMPRESSOR} for compression\e[0m"
}
 
 
if [ "${1}" = "-h" ];
   then
        logbackuphelp;
        exit 0;
fi
 
if [ "`whoami`" != "root" ];
   then
        logbackuphelp;
        echo -e "\e[31;1mYou must be root to use this script!\e[0m"
        exit 0;
fi
 
if [ ! $LOGFOLDER ];
   then
        mkdir $LOGFOLDER;
fi
 
if [ ${LOGSIZE} -gt ${SIZE} -o "${1}" = "-f" ];
   then
        if [ "${1}" = "-f" ];
           then
                 echo -e"\e[33;1mYou have chose to force logbackup!\e[0m"
           else     
                 echo -e "\e[33;1m${LOGDIR} is ${LOGSIZE}. ${SIZE} reached, processing ${LOGDIR}.\e[0m"
        fi
        dirtwork;
        echo -e "\e[34;1mstart compression\e[0m"
        if [ "${1}" = "-g" -o "${2}" = "-g" ];
           then
                 COMPRESSOR=gzip;
        fi
        ${COMPRESSOR} ${LOGFILE};
        LOGFILE=${LOGFILE}.bz2
        mv ${LOGFILE} ${LOGFOLDER}
        echo -e "\e[35;1m"
        ${DAEMON} restart
        echo -e "\e[35;1mregenerating user defined directory and log file...\e[34;1m"
        for i in ${REGENERATEDIR}
           do
                 echo "${LOGDIR}/${i}";
                 mkdir ${LOGDIR}/${i} 2> /dev/null;
           done
        for i in ${REGENERATE}
           do
                 echo "${LOGDIR}/${i}";
                 touch ${LOGDIR}/${i} 2> /dev/null;
           done
        echo -e "\e[33;1mwork finished!\e[0m"
   else
        echo -e "\e[32;1m${LOGDIR} is less than ${SIZE} bytes (${LOGSIZE}). Nothing to do.\e[0m"
   TOTALLOG=0
   TOTALSTORE=`du -s ${LOGFOLDER} | cut -f 1`
   for i in `ls ${LOGFOLDER}`;
        do
           TMP=`echo ${i} | cut -d _ -f 2 | cut -d B -f 1`;
           let TOTALLOG=${TOTALLOG}+${TMP}
        done
   echo -e "\e[32;1mThere's currently ${TOTALLOG} bytes of log files compressed in \
   ${TOTALSTORE} bytes stored in ${LOGFOLDER}.\e[0m"
   echo -e "\n"
   logbackuphelp;
   echo " ";
fi
echo -e "\e[36;1mhave a nice day!\e[0m"





:q!