I run bash scripts from time to time on my servers, I am trying to write a script that monitors log folders and compress log files if folder exceeds defined capacity. I know there better ways of doing what I am currently trying to do, your suggestions are more that welcome. Below is my script.

 dir_base=$1
 size_ok=5000000
 cd $dir_base
 curr_size=`du -s -D | awk '{print $1}' | sed 's/%//g' `
 zipname=archive`date +%Y%m%d`

    if (( $curr_size > $size_ok ))
            then
            echo "Compressing and archiving files, Logs folder has grown above 5G"
                    echo "oldest to newest selected."
    targfiles=( `ls -1rt` )
    echo "rocess files."
     for tfile in ${targfiles[@]}
         do
             let `du -s -D | awk '{print $1}' | sed 's/%//g' | tail -1`
                    if [ $curr_size -lt $size_ok ];
                            then
                               echo "$size_ok has been reached. Stopping processes"
                                    break
                                    else  if [ $curr_size -gt $size_ok ];
    then
      zip -r $zipname $tfile
            rm -f $tfile
                    echo "Added ' $tfile ' to archive'date +%Y%m%d`'.zip and removed"
                            else [ $curr_size -le $size_ok ];
                                    echo "files in $dir_base are less than 5G, not archiving"

                                    fi
link|improve this question

45% accept rate
Have you considered using logrotate which should be able to do what you want. – Iain Oct 21 '10 at 11:09
Are you able to monitor folder size with logrotate? If not can you call logrotate from a script to do it? – Rungano Oct 21 '10 at 14:06
Logrotate it is – Rungano Nov 4 '10 at 11:43
feedback

3 Answers

It's completely broken, perhaps not all of it was pasted? Found a unclosed if and an unclosed for loop. Plus syntax error with a ">" (replaced with -lt)

This works: http://pastebin.com/aUC3xwa5

You can also add this to the top of it:

dir_base=$1
size_ok=5000000
cd $dir_base
# the sed is probably unneccessary
curr_size=du -s -D | awk '{print $1}' | sed 's/%//g'
date=`date +%Y%m%d`
zipname="archived$date"
link|improve this answer
The > is perfectly valid within (()) and -lt is not (it should have been -gt anyway). The operators such as -gt work within [] and [[]]. Please use code formatting when you post code. – Dennis Williamson Oct 21 '10 at 15:00
Thank you Dennis Williamson, but I will settle for logrotate. Just need to write a script that emails the zipped folder monthly to a systems admin for backups – Rungano Nov 4 '10 at 11:42
feedback
dir_base=$1
size_ok=5000000
cd $dir_base
curr_size=$(du -s -D | awk '{print $1}')
zipname=archive$(date +%Y%m%d)

if (( $curr_size > $size_ok ))
then
    echo "Compressing and archiving files, Logs folder has grown above 5G"
    echo "oldest to newest selected."
    targfiles=( $(ls -1rt) )
    echo "Process files."
    for tfile in ${targfiles[@]}
    do
        curr_size=$(du -s -D | awk '{print $1}')
        if (( $curr_size <= $size_ok ))
        then
            echo "$size_ok has been reached. Stopping processes"
            break
        else
            if (( $curr_size > $size_ok ))
            then
                zip -r "$zipname" "$tfile"
                rm -f "$tfile"
                echo "Added '$tfile' to archive '$zipname.zip' and removed"
            # else 
            #    if (( $curr_size <= $size_ok ))
            #    then
            #        echo "files in $dir_base are less than 5G, not archiving"
            #    fi
            fi
         fi
    done
fi

I removed unnecessary semicolons, replaced backticks with $() (and fixed a place where one was missing), added two missing fi and one missing done, fixed indentation, made the conditionals consistent, quoted variables, replaced a missing variable, removed an unnecessary let, removed an unnecessary re-execution of date, some minor general cleanup, commented out a section (with a missing if and then that I added) that will only be reached if the sizes are equal and moved the <= condition to the next higher if, removed unnecessary sed and tail.

link|improve this answer
feedback

If you are using a Red Hat based distro, you should take a look to logrotate program. You can add your custom script to /etc/logrotate.d/ directory. You can use any of the existing files as example:

/etc/logrotate.d/httpd

  /var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

You can specify compression, size, frequency, retention and many other options.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.