Class: Bard::Backup

Inherits:
Struct
  • Object
show all
Defined in:
lib/bard/backup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key

Returns:

  • (Object)

    the current value of access_key



9
10
11
# File 'lib/bard/backup.rb', line 9

def access_key
  @access_key
end

#s3_pathObject

Returns the value of attribute s3_path

Returns:

  • (Object)

    the current value of s3_path



9
10
11
# File 'lib/bard/backup.rb', line 9

def s3_path
  @s3_path
end

#secret_keyObject

Returns the value of attribute secret_key

Returns:

  • (Object)

    the current value of secret_key



9
10
11
# File 'lib/bard/backup.rb', line 9

def secret_key
  @secret_key
end

Class Method Details

.call(s3_path, access_key:, secret_key:) ⇒ Object



10
11
12
# File 'lib/bard/backup.rb', line 10

def self.call s3_path, access_key:, secret_key:
  new(s3_path, access_key, secret_key).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bard/backup.rb', line 14

def call
  @time = Time.now

  Backhoe.dump path

  self.full_s3_path = "/#{s3_path}/#{filename}"

  if s3_path.include?("/")
    s3_bucket = s3_path.split("/").first
    self.s3_path = s3_path.split("/")[1..].join("/")
    uri = URI("https://#{s3_bucket}.s3.amazonaws.com/#{s3_path}/#{filename}")
  else
    uri = URI("https://#{s3_path}.s3.amazonaws.com/#{filename}")
  end

  request = Net::HTTP::Put.new(uri, {
    "Content-Length": File.size(path).to_s,
    "Content-Type": content_type,
    "Date": date,
    "Authorization": "AWS #{access_key}:#{signature}",
    "x-amz-storage-class": "STANDARD",
    "x-amz-acl": "private",
  })
  request.body_stream = File.open(path)

  Net::HTTP.start(uri.hostname) do |http|
    response = http.request(request)
    response.value # raises if not success
  end
end