Skip to content

Instantly share code, notes, and snippets.

@steveh
Created October 7, 2009 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveh/203616 to your computer and use it in GitHub Desktop.
Save steveh/203616 to your computer and use it in GitHub Desktop.
Rotate Rankers logs
#!/opt/ruby-enterprise/bin/ruby
require "date"
require "rubygems"
require "activesupport"
require "open4"
log_bucket = "rankers-logs"
(2007..(Time.now.year)).each do |year|
`/opt/ruby-enterprise/bin/s3cmd createbucket #{log_bucket}-#{year.to_s}`
end
start_date = Date.new(2007, 11, 01)
end_date = Time.now.to_date - 2.days
start_date.upto(end_date) do |date|
old_filename = "/var/log/apache2/#{date.strftime('%Y/%m/%d_access.log')}"
new_filename = "#{date.strftime('%Y-%m-%d-access.log')}"
if File.exist?(old_filename)
cmd = "/opt/ruby-enterprise/bin/s3cmd put #{log_bucket}-#{date.year.to_s}:#{new_filename} #{old_filename}"
err = ""
status = Open4::popen4(cmd) do |pid, stdin, stdout, stderr|
err = stderr.read
end
if err === ""
puts "Uploaded #{new_filename}"
`rm #{old_filename}`
else
puts "Failed to upload #{old_filename} to #{new_filename}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment