#!/usr/bin/ruby # SYNOPSIS # Rolls over old logs of applications on Rails Machine. # This script is intended to be called by cron on your Rails Machine. # # VERSION # 0.2 # # HISTORY # 0.2 # - improved log file selection with glob (credit to Bill Siggelkow) # 0.1 # - first release # # AUTHOR # Andrew Stewart, AirBlade Software Ltd (boss at airbladesoftware dot com) # # FEEDBACK # Yes please! require 'rubygems' require 'logger' require 'yaml' # Configure the script here. You won't need to change anything # unless you have modified your Rails Machine setup. APPS_PATH = '/var/www/apps' # Path to all your apps EXCLUSIONS = %w( rails ) # Any directories or apps to exclude SIZE = 5 * 1024 * 1024 # Max size of a log file (bytes) TIMESTAMP = '%Y%m%d%H%M%S' # Timestamp for databases' backup files LOG_FILE = '/home/deploy/logs.log' # Where to log activity # The body of the script follows. log = Logger.new(LOG_FILE, 5, 10*1024) log.level = Logger::DEBUG log.info 'Starting log maintenance' Dir["#{APPS_PATH}/*"].each do |entry| app = File.basename entry next if File.file?(entry) || EXCLUSIONS.include?(app) Dir["#{File.join(entry, 'current', 'log')}/*.log"].each do |log_file| if File.size(log_file) > SIZE copy_cmd = "cp #{log_file} #{log_file}-#{Time.now.strftime(TIMESTAMP)}" log.debug "Executing: #{copy_cmd}" `#{copy_cmd}` log.debug "Truncating #{log_file}" File.truncate log_file, 0 end end log.info "Rolled over logs for #{app}" end log.info 'Finished log maintenance'