#!/usr/bin/ruby require 'optparse' $VERBOSE = nil def show_help(parser, code=0, io=STDOUT) io.puts parser exit(code) end ARGV.options do |opts| opts.on_tail("-h", "--help", "Display this help screen") { show_help(opts) } opts.on("-v", "--verbose" , nil, "Be verbose") { $VERBOSE = 1 } opts.parse! end root = '/srv/www/vhosts/' archive_dir = "/logs-archive/" forstats_dir = "/logs-for-stat/" logs_dir = "/logs/" Dir.chdir(root) today = Time.new.strftime("%Y-%m-%d"); Dir.glob("*").each{ |client| root_logs_dir = root+"/"+client+logs_dir root_archive_dir = root+"/"+client+archive_dir root_forstats_dir = root+"/"+client+forstats_dir Dir.chdir(root) next unless File.exist?( client ) next unless File.stat( client ).directory? next unless File.exist?( client + logs_dir ) next unless File.stat( root_logs_dir ).directory? next unless File.stat( root_archive_dir ).directory? next unless File.stat( root_forstats_dir ).directory? Dir.chdir(root_logs_dir) Dir.glob("*.log").each{ |filename| targetname = filename +"."+ today if File.exist?(root_archive_dir+targetname) or File.exist?(root_archive_dir+targetname+".gz") or File.exist?(root_archive_dir+targetname+".bz2") STDERR.puts("Not moving #{filename}: Target exists."); else if filename =~ /access.log$/ puts "Linking #{filename} to statsdir" if $VERBOSE File.link( filename , root_forstats_dir+targetname ) end puts "Moving #{filename}" if $VERBOSE File.rename( filename , root_archive_dir+targetname ) end } } system ("/usr/sbin/apache2ctl graceful") or throw "system apache2ctl graceful failed" ## graceful means that apache might not close the old logfile immediately ## therefore we compress an hour later in a different script