Find the answer to your Linux question:
Results 1 to 5 of 5
Hi I'm new to this and having a difficult time grasping how to write this script. What I need to do is write a script that will stop a webserver, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    5

    Log rotating script, need help!


    Hi

    I'm new to this and having a difficult time grasping how to write this script.

    What I need to do is write a script that will stop a webserver, move/rename the log files, restart the webserver, and then compress the log files. On the 1st of the month, take the previous months daily log files and put them in a tar file named with the month and year.

    So far, not looking good.

    Code:
    #!/bin/sh
    
    /usr/sbin/apache2ctl stop
    sleep 3
    mv /var/log/apache2/access.log /var/log/apache2/access.log-%m%d%Y
    /usr/sbin/apache2ctl start
    sleep 3
    gzip /var/log/apache2/access.log-%m%d%Y
    Any help would be hugely appreciated. Thanks

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    682
    Hi there,

    Do you know apache make a log rotation utility?
    https://httpd.apache.org/docs/2.2/pr...otatelogs.html

    Is there something specific you need that this won't do?

    Let us know how you get on.
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    Just Joined!
    Join Date
    Oct 2012
    Posts
    5
    Thanks for responding

    I've heard of the log rotation utility, but unfortunately I don't have the option of using it.

    What I'm really having a difficult time figuring out is how to: at the first of the month, take all the previous months daily log files and put them in a tar file, named with month/year.

    It seems like it should be fairily easy but I'm having a tough time wrapping my head around it.

    Any suggestions/scripting know how?

    Thanks
    Last edited by earthicle; 11-01-2012 at 10:22 PM.

  4. #4
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    682
    Hi there,

    I'm not sure where exactly you are having trouble, but you'd most likely want to schedule the job with cron, and you can find files modified within a set number of days using "find -mtime <days>".

    We would be able to help you more exactly if you could explain where exactly you're having problems.

    Let us know how you get on.
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  5. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by earthicle View Post
    What I'm really having a difficult time figuring out is how to: at the first of the month, take all the previous months daily log files and put them in a tar file, named with month/year.
    Hi,

    Why on earth can't you use a logrotate program? In addition to Apache's rotatelogs, there is also the more generic logrotate facility. It controls the logging of various daemons on Linux systems. Here is an example of the logrotate config file for Apache (/etc/logrotate.d/apache2) on my system:

    Code:
    /var/log/apache2/*.log {
            weekly
            missingok
            rotate 52
            compress
            delaycompress
            notifempty
            create 640 root adm
            sharedscripts
            postrotate
                    /etc/init.d/apache2 reload > /dev/null
            endscript
            prerotate
                    if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                            run-parts /etc/logrotate.d/httpd-prerotate; \
                    fi; \
            endscript
    }
    Anyway, I'll assume you can't use that logrotate program either.

    So here is an example of what you want to do, as a simple Bash script. Try it out. Note that it deletes log files after it tars them up. If you don't want it to do that, remove the --remove-files argument from the tar command near the bottom of the script. At the top of the script, you'll want to set the variable for the dir containing tarball (tarballdir) and make sure the variable pointing to the directory for apache log dirs (logdir) is correct.

    Code:
    #!/bin/bash
    
    end(){
      [ -n "$restart" ] && /usr/sbin/apache2ctl start
    }
    
    trap end EXIT
    
    # define the dir where tarballs will get made (don't use the logdir!)
    tarballdir='/tmp'
    
    # define the dir where apache keeps the logs
    logdir='/var/log/apache2'
    if ! [ -d "$logdir" ]; then
      echo "$logdir: No such directory"
      exit 1
    fi
    
    # see if the first of the month
    if [ $(date +%e) != 1 ]; then
      echo "Not the first of the month, goodbye"
      exit 0
    fi
    
    # generate a list of files in the log dir
    declare -a logs
    logs=($(find $logdir -mindepth 1 -maxdepth 1 -type f -printf "%f\n"))
    echo Found ${#logs[*]} files to tar up
    
    # quit if no files to tar up
    [ ${#logs[*]} == 0 ] && exit 0
    
    # tarball filename
    tarball="${tarballdir}/apache_logs_$(date +%m-%Y).tar.gz"
    if [ -f $tarball ]; then
      echo "Tarball $tarball exists, refusing to overwrite it"
      exit 1
    fi
    
    # see if apache is running
    pidof httpd apache2 >/dev/null 2>&1
    
    # if running, stop apache
    if [ $? -eq 0 ]; then
      restart=1
      /usr/sbin/apache2ctl stop || exit 1
    fi
    
    # tar up the files
    printf "Tarring up files in $logdir ... "
    out=$(tar --remove-files -C $logdir -zcf $tarball ${logs[*]} 2>&1)
    rc=$?
    if [ $rc -eq 0 ]; then
      echo OK
    else
      echo FAILED
      printf "$out\n"
    fi
    
    exit $rc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •