Last active
December 23, 2015 12:09
-
-
Save snarkyboojum/6633219 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem 'aws-sdk' | |
gem 'trollop' | |
gem "listen", "~> 2.0.0.beta.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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