Skip to content

Instantly share code, notes, and snippets.

@snarkyboojum
Last active December 23, 2015 12:09
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 snarkyboojum/6633219 to your computer and use it in GitHub Desktop.
Save snarkyboojum/6633219 to your computer and use it in GitHub Desktop.
source "https://rubygems.org"
gem 'aws-sdk'
gem 'trollop'
gem "listen", "~> 2.0.0.beta.2"
#!/usr/bin/env ruby
require 'Bundler'
Bundler.require(:default)
opts = Trollop::options do
opt :bucket, "Select the S3 bucket to archive to", :short => 'b', :type => String
opt :source, "Source directory to watch for archival", :short => 's', :type => String, :default => '.'
opt :target, "Target directory to write S3 archives to", :short => 't', :type => String, :default => 'backups'
end
Trollop::die :bucket, "must be given" if not opts[:bucket_given]
s3 = AWS::S3.new(
:access_key_id => '<access key id>',
:secret_access_key => '<secret access key>'
)
# TODO: this check is naive and insufficient
# check for and add a bucket lifecycle policy to transition to glacier "immediately"
bucket = s3.buckets[opts[:bucket]]
if not bucket.lifecycle_configuration.rules
bucket.lifecycle_configuration.update do
add_rule(opts[:target], :glacier_transition_time => 0)
end
end
# setup filesystem listener and implement "new" files -> S3
listener = Listen.to(opts[:source]) do |modified, added, removed|
if added
added.each do |path|
srcpath = Pathname.new(path)
putkey = File.join(opts[:target], srcpath.basename)
obj = bucket.objects[putkey].write(:file => srcpath.realpath)
end
end
end
listener.start
sleep
Example usage:
./slam.rb --bucket upload-bucket-name --source /path/to/staging/directory
Help:
./slam.rb --help
Options:
--bucket, -b <s>: Select the S3 bucket to archive to
--source, -s <s>: Source directory to watch for archival (default: .)
--target, -t <s>: Target directory to write S3 archives to (default: backups)
--help, -h: Show this message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment