#!/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/" logs_dir = "/logs/" Dir.chdir(root) today = Time.new.strftime("%Y-%m-%d"); Dir.glob("*").each{ |client| Dir.chdir(root) next unless File.stat( client ).directory? next unless File.exist?( client + logs_dir ) next unless File.stat( client + logs_dir ).directory? next unless File.stat( client + archive_dir ).directory? root_logs_dir = root+"/"+client+logs_dir root_archive_dir = root+"/"+client+archive_dir 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 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