Skip to content

Instantly share code, notes, and snippets.

@aurelian
Created October 29, 2009 16:32
Show Gist options
  • Save aurelian/221576 to your computer and use it in GitHub Desktop.
Save aurelian/221576 to your computer and use it in GitHub Desktop.
script to backup my iPhoto pictures
require 'rubygems'
require 'right_aws'
require 'ruby-growl'
require 'mime/types'
require 'digest/sha1'
if ARGV.size < 1
puts "-- #{__FILE__} folder"
exit 1
end
folder= ARGV[0]
exit unless File.exists? folder
conf= YAML.load_file("#{ENV['HOME']}/.s3.yml").symbolize_keys
s3 = RightAws::S3.new(conf[:aws_access_key_id], conf[:aws_secret_access_key])
growl= Growl.new "localhost", "ruby-growl", [__FILE__]
bucket= s3.bucket(conf[:bucket])
base = "Pictures"
k=0 # there's a limit
Dir.chdir(folder)
Dir.foreach(folder) do | file |
next if file =~ /\.$/
next unless file =~ /JPG$/i || file =~ /AVI$/i
file.match(/([0-9]+)/)
number = $1
next if number.nil?
remote= "#{base}/#{number[0].chr}/#{number[1].chr}/#{number[2].chr}/#{file}"
key= RightAws::S3::Key.create bucket, remote
next if key.exists?
i= 0
size= File.size(file)
mime= MIME::Types.type_for(file)
puts "~> [#{k}] up #{file} [#{mime}|#{size}] to #{remote}"
begin
key.put(open(file), 'private', {'Content-Type' => mime})
rescue StandardError => error
puts " ~> retry: #{error.class.name}"
puts "~> [#{k}.#{i}] uploading [#{File.size(file)}] #{file} to #{remote}"
growl.notify __FILE__, "[#{k}.#{i}] Retry", remote
i= i+ 1
retry if i < 10
end
growl.notify __FILE__, "[#{k}.#{i}] Uploaded", remote
k= k+1
# break if k == 20
end
growl.notify __FILE__, "S3 session", "done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment