summaryrefslogtreecommitdiff
path: root/bin/weblogs-compress
blob: 65c3daba2e4ac9460f856b9724b8fb7086fd3586 (plain)
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
#!/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/"
Dir.chdir(root)

Dir.glob("*").each{ |client|
	Dir.chdir(root)
	next unless File.stat( client ).directory?
	next unless File.exist?( client +"/"+  archive_dir )
	next unless File.stat( client +"/"+ archive_dir).directory?

	logroot = root+"/"+client+"/logs"
	Dir.chdir(logroot)
	Dir.glob("*.log.*").each{ |filename|
		match = /^(.*)\.log\.[0-9]{4,4}-[0-9][0-9]-[0-9][0-9]$/.match(filename)
		next unless match

		if File.exist?(filename+".gz") or
		   File.exist?(filename+".bz2")
			STDERR.puts("Not compressing #{filename}: Target exists.");
		else
			puts "Compressing #{filename}" if $VERBOSE
			system("gzip -9 #{filename}") or throw("system(gzip -9 #{filename}) failed.")
		end
	}
}