Class: NewBackup::MyS3

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging
Defined in:
lib/new_backup/mys3.rb

Constant Summary collapse

MAX_TRIES =

Maximum number of attempts to try to upload file

3

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MyS3

Initialize the MyS3 object

options

pass in the program options



31
32
33
# File 'lib/new_backup/mys3.rb', line 31

def initialize(options={})
  @options = options
end

Instance Method Details

#connect {|connection| ... } ⇒ Object

Connect to S3, cache the connection, and yield self

Yields:

  • (connection)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/new_backup/mys3.rb', line 36

def connect(&block)

  options = {
    :aws_access_key_id => @options[:aws][:access_key],
    :aws_secret_access_key => @options[:aws][:secret_key],
    :region => @options[:s3][:region],
    :provider => 'AWS',
    :scheme => 'https'}
  
  connection = Fog::Storage.new(options)

  yield connection
  
end

#connect_bucket(connection, bucket_name, &block) ⇒ Object

Connect to specific S3 bucket

inputs

connection

S3 connection to use

bucket_name

name of the bucket to use in S3

*&block*

block passed to evaluate in context of bucket

(If no block given simply return the bucket pointer.)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/new_backup/mys3.rb', line 58

def connect_bucket(connection, bucket_name, &block)
  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: connection: #{connection.inspect}"
  info "Connecting to #{bucket_name}"
  buckets = connection.directories
  raise "No buckets!" if buckets.nil? || buckets.empty?
  bucket = connection.directories.get(bucket_name)
  raise "No bucket #{bucket_name}" if bucket.nil?

  if block_given?
    yield bucket
  else
    bucket
  end
  
end

#prune(bucket, keep = 0) ⇒ Object

Prune extra files



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/new_backup/mys3.rb', line 102

def prune(bucket, keep=0)

  if keep > 0
    files = bucket.files.all('prefix' => @options[:s3][:prefix])

    return if files.nil? || files.empty?

    if files.count > keep
      info "Pruning down to #{keep} files"
      files.sort {|x,y| x.last_modified <=> y.last_modified}.
        take(files.count - keep).
        map(&:destroy)
    end

  end
  
  files = bucket.files.all('prefix' => @options[:s3][:prefix])
  files.count

end

#put_file(bucket, fn) ⇒ Object

Do the heavy lifing to put the file in the appropriate bucket

bucket

directory where to put the file

fn

name of file to upload (gzipped)



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/new_backup/mys3.rb', line 82

def put_file(bucket,fn)
  info "Saving #{fn} to #{bucket.key}"
  s3_fn = File.join(@options[:s3][:prefix], File.basename(fn))
  options = {
    :key => s3_fn,
    :body => File.open(fn,'r'),
    :acl => 'authenticated-read',
    :encryption => 'AES256',
    :content_type => 'application/x-gzip'}
  
  bucket.files.create(options)

  # Verify that file was uploaded
  files = bucket.files.all.select {|f| f.key == s3_fn }
  raise "#{fn} was not uploaded to #{s3_fn}!" unless files.count > 0
  "s3://#{bucket.key}/#{s3_fn}"

end