1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#!/usr/bin/ruby
#
# Copyright (c) 2004,2005 Florian Reitmeir <florian@reitmeir.org>
# based on a script from Peter Palfrader <peter@palfrader.org>
#
# Copyright (c) 2006 Peter Palfrader <peter@palfrader.org>
#
# 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)
}
}
|