-1

I am currently using chronolog to set log file names for Apache with date. They are in the following format:

/WEB/LOGS/APACHE_ACCESS_YYYY-MM-DD.log /WEB/LOGS/APACHE_ERROR_YYYY-MM-DD.log

I would like to have a script that runs on the first of every month and compresses the log files from the previous month, transfers them to another host (via SCP) and then deletes the compressed file.

find . -name '*.log' -mtime +1 -type f

I've found several examples like the one above that allow you to select files x days old, but I need all files from the previous month. I am the first to admit my bash scripting skills are weak so would really appreciate any help and guidance.

4
  • 5
    Are you 100% certain "logrotate" won't do a job for you, before you go building your own? It's very powerful, and prerotate/postrotate scripts might aid your transfer
    – Tom Newton
    Aug 18, 2014 at 14:42
  • I would definitely use logrotate for this
    – gparent
    Aug 18, 2014 at 14:53
  • The advantage of using a pipe to chronolog or the similar rotatelogs above letting apache write to logfiles directly and using logrotate is that you don't need to reload the webserver every time you want to rotate logs files. If you use SIGHUP to reload apache you run the risk downloads/scripts will be interrupted and with SIGUSR1 the wait may be long and the daily log file may contain entries for requests completed on the next day.
    – HBruijn
    Aug 18, 2014 at 15:14
  • @HBruijn You don't need to HUP apache, you just need to use the copytruncate option to logrotate.
    – Jenny D
    Aug 19, 2014 at 9:25

1 Answer 1

0

In shell scripting you can call on the date command to generate time/date strings.

The manual man 1 date should show what sequences are supported, as GNU's date implementation is not quite the same as the default found on other platforms. The following should work from a monthly cron job:

LOG_BASE=$(date --date="1 month ago" +/WEB/LOGS/APACHE_ACCESS_%Y-%m)
for file in $LOG_BASE-*.LOG ; do 
      zip -m $file.zip $file
done
4
  • Thank you for the reply! OK, that code makes sense so I tried the following: #! /bin/bash LOG_BASE=$(date --date="1 month ago" +/WEB/LOGS/APACHE_ACCESS_%Y-%m) for file in $LOG_BASE-*.LOG ; do /usr/bin/zip TEST.zip done but get an error that says zip error: Nothing to do! (TEST.zip)
    – Jason
    Aug 18, 2014 at 15:16
  • zip needs two arguments, the archive-name (if no .zip is included that extension will be added) and a file name that gets added, consider using the -m switch as well, to remove the original file name after compression. See modification above. gzip is more common on Linux though.
    – HBruijn
    Aug 18, 2014 at 15:26
  • Thank you again - I really appreciate the help. OK, so those modifications worked. However, it is compressing each file individually. What I am trying to do is to create a single zip file that contains all of the individual log files from the previous month. So, for example, LOG.ZIP would contain APACHE_ACCESS_2014-07-01.log, APACHE_ACCESS_2014-07-02.log, etc. Is that possible or do I need to have a zip file for each day?
    – Jason
    Aug 18, 2014 at 15:36
  • Yes that is possible. The manual even contains an example of how to do that and more.
    – HBruijn
    Aug 18, 2014 at 15:45

Not the answer you're looking for? Browse other questions tagged .