#!/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 } }