Skip to content

Instantly share code, notes, and snippets.

@sj26
Forked from mujtaba3B/dl_papertrail_archives
Last active August 24, 2018 23:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sj26/0f6fc3e84b0ffba01942c4c50d595e82 to your computer and use it in GitHub Desktop.
Save sj26/0f6fc3e84b0ffba01942c4c50d595e82 to your computer and use it in GitHub Desktop.
Backup old papertrail archive to s3
require "active_support/all"
require "aws-sdk"
require "concurrent"
papertrail_token = "..."
s3 = Aws::S3::Client.new(region: "us-east-1")
s3_bucket = "..."
s3_prefix = "papertrail/logs/[account-id]"
pool = Concurrent::FixedThreadPool.new(8)
(365 * 24).times.each do |i|
pool.post do
date = i.hours.ago
datename = date.strftime("%Y-%m-%d")
hourname = date.strftime("%Y-%m-%d-%H")
filename = "#{hourname}.tsv.gz"
papertrail_url = "https://papertrailapp.com/api/v1/archives/#{hourname}/download"
s3_key = "#{s3_prefix}/dt=#{datename}/#{filename}"
#s3_url = "s3://#{s3_bucket}/#{s3_key}"
begin
s3.head_object(bucket: s3_bucket, key: s3_key)
puts "#{hourname}: already on s3"
rescue Aws::S3::Errors::NotFound
puts "#{hourname}: downloading from papertrail"
system "wget", "--quiet",
"--header", "X-Papertrail-Token: #{papertrail_token}",
"--output-document", filename,
"--continue",
papertrail_url
unless $?.success?
puts "#{hourname}: no such archive, skipping"
next
end
puts "#{hourname}: uploading to s3"
File.open(filename, "rb") do |file|
s3.put_object(bucket: s3_bucket, key: s3_key, body: file)
end
end
if File.exists?(filename)
puts "#{hourname}: removing local file"
File.unlink(filename)
end
end
end
pool.wait_for_termination
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment