summaryrefslogtreecommitdiff
path: root/bin/weblogs-compress
blob: b802b5fafe341c5234d6f57c7c94e513864f5e18 (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
43
44
45
#!/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.exist?( client )
	next unless File.stat( client ).directory?
	next unless File.exist?( client +"/"+  archive_dir )
	next unless File.stat( client +"/"+ archive_dir).directory?

	logroot = root+"/"+client+"/"+archive_dir
	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
			# the -f is necessary so that gzip will compress files even
			# if they have multiple links
			system("gzip -f -9 #{filename}") or throw("system(gzip -f -9 #{filename}) failed.")
		end
	}
}