summaryrefslogtreecommitdiff
path: root/bin/weblogs-rotate
blob: d4195c3de237563f68c50633c281503709985e02 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/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/"
forstats_dir = "/logs-for-stat/"
logs_dir = "/logs/"

Dir.chdir(root)

today = Time.new.strftime("%Y-%m-%d");
Dir.glob("*").each{ |client|
	root_logs_dir = root+"/"+client+logs_dir
	root_archive_dir = root+"/"+client+archive_dir
	root_forstats_dir = root+"/"+client+forstats_dir

	Dir.chdir(root)
	next unless File.exist?( client )
	next unless File.stat( client ).directory?
	next unless File.exist?( client + logs_dir )
	next unless File.stat( root_logs_dir ).directory?
	next unless File.stat( root_archive_dir ).directory?
	next unless File.stat( root_forstats_dir ).directory?

	Dir.chdir(root_logs_dir)
	Dir.glob("*.log").each{ |filename|
		targetname = filename +"."+ today
		if File.exist?(root_archive_dir+targetname) or
		   File.exist?(root_archive_dir+targetname+".gz") or
		   File.exist?(root_archive_dir+targetname+".bz2")
			STDERR.puts("Not moving #{filename}: Target exists.");
		else
			if filename =~ /access.log$/
				puts "Linking #{filename} to statsdir" if $VERBOSE
				File.link( filename , root_forstats_dir+targetname )
			end
			puts "Moving #{filename}" if $VERBOSE
			File.rename( filename , root_archive_dir+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