summaryrefslogtreecommitdiff
path: root/bin/weblogs-rotate
blob: 594e0dc3f1959395bad729c113e42b9be63b65d4 (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
#!/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/"
logs_dir = "/logs/"

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_dir )
	next unless File.stat( client + logs_dir ).directory?
	next unless File.stat( client + archive_dir ).directory?
	
	root_logs_dir = root+"/"+client+logs_dir
	root_archive_dir = root+"/"+client+archive_dir

	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
			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