#!/usr/bin/ruby # # Copyright (c) 2004,2005 Florian Reitmeir # based on a script from Peter Palfrader # # Copyright (c) 2006 Peter Palfrader # # All rights reserved. # # MIT license. # require 'optparse' require 'find' $VERBOSE = false $NOTHING = false READBLOCKSIZE = 4096 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 = true } opts.on("-n", "--do-nothing" , nil, "Do nothing") { $VERBOSE = true; $NOTHING = true } opts.parse! end root = '/srv/www/vhosts/' dir = "/logs-archive/" this_year = Time.new.year.to_s this_month = Time.new.month.to_s Dir.chdir(root) def append_and_remove(sourcefile, targetfile) puts " #{sourcefile} >> #{targetfile}" if $VERBOSE next if $NOTHING File.open( targetfile, "a" ) do |targetfh| File.open( sourcefile, "r" ) do |sourcefh| while r = sourcefh.read(READBLOCKSIZE) do targetfh.write r end end end File.unlink(sourcefile) end def getfilelist(dir) files = Array.new Find.find(dir) { |file| files.push(file) if File.file?(file) } files.sort end Dir.glob("*").each{ |client| Dir.chdir(root) next unless File.stat( client ).directory? next unless File.exist?( client + dir ) next unless File.stat( client + dir ).directory? Dir.chdir(client+dir) if $VERBOSE puts 'pwd :'+root+"/"+client+"/"+dir end if ! File.writable?(root+"/"+client+"/"+dir) then puts 'log dir not writeable' exit 1 end # move yyyy-mm-dd to yyyy-mm getfilelist(root+"/"+client+"/"+dir).each { |sourcefile| m = /(.*-(?:access|error).log.)(\d+)-(\d+)-(\d+).gz/.match(sourcefile) next unless m dummy, name, year, month, day = m.to_a next if this_year == year && this_month == month targetfile = "#{name}#{year}-#{month}.gz" append_and_remove(sourcefile, targetfile) } # move yyyy-mm to yyyy getfilelist(root+"/"+client+"/"+dir).each { |sourcefile| m = /(.*-(?:access|error).log.)(\d+)-(\d+).gz/.match(sourcefile) next unless m dummy, name, year, month = m.to_a next if this_year == year targetfile = "#{name}#{year}.gz" append_and_remove(sourcefile, targetfile) } }