summaryrefslogtreecommitdiff
path: root/bin/weblogs-rotate
blob: 98e969fedbe217ff3e132dd1aea53f8978c8a390 (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
46
#!/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'
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" )
	next unless File.stat( client + "/logs" ).directory?

	logroot = root+"/"+client+"/logs"
	Dir.chdir(logroot)
	Dir.glob("*.log").each{ |filename|
		targetname = filename +"."+ today

		if File.exist?(targetname) or
		   File.exist?(targetname+".gz") or
		   File.exist?(targetname+".bz2")
			STDERR.puts("Not moving #{filename}: Target exists.");
		else
			puts "Moving #{filename}" if $VERBOSE
			File.rename( filename , 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